UNPKG

uniapp-use-request

Version:

一个功能强大且高度可配置的 Vue 3 Composition API 请求 Hook,灵感来源于 [ahooks](https://ahooks.js.org/hooks/request/use-request) 和 [SWR](https://swr.vercel.app/)。它内置了缓存、防抖、节流、轮询、重试、请求队列和并发控制等特性。

68 lines (62 loc) 1.99 kB
// types/request.d.ts import { Ref } from 'vue'; export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD' | 'OPTIONS'; export interface RequestOptions { method?: HttpMethod; headers?: Record<string, string>; data?: any; params?: Record<string, any>; timeout?: number; showLoading?: boolean; loadingText?: string; showError?: boolean; withToken?: boolean; retryCount?: number; retryInterval?: number; /** * 缓存时间(毫秒) * 0: 不缓存 * >0: 缓存时间,同时使用内存缓存(LRU)和持久化缓存 */ cacheTime?: number; throttle?: boolean; throttleTime?: number; debounce?: boolean; debounceTime?: number; pollingInterval?: number; pollingMaxCount?: number; manual?: boolean; ready?: boolean | Ref<boolean>; priority?: number; requestId?: string; } export interface RequestResult<T = any> { data: Ref<T | undefined>; loading: Ref<boolean>; error: Ref<Error | null>; polling: Ref<boolean>; run: (params?: any) => Promise<T>; cancel: () => void; refresh: () => Promise<T>; clearCache: () => void; startPolling: () => void; stopPolling: () => void; updateParams: (params: any) => void; updateOptions: (options: Partial<RequestOptions>) => void; reset: () => void; } export interface CacheData<T = any> { data: T; timestamp: number; params: any; } export interface RequestTask { abort: () => void; } export interface RequestPlugin { onInit?: (options: RequestOptions) => RequestOptions; onRequest?: (options: RequestOptions) => RequestOptions | Promise<RequestOptions>; onSuccess?: <T>(data: T, options: RequestOptions) => T | Promise<T>; onError?: (error: Error, options: RequestOptions) => Error | Promise<Error> | void; onFinally?: (options: RequestOptions) => void | Promise<void>; }