UNPKG

ym-userequest

Version:
191 lines (179 loc) 7.03 kB
import { Ref, WatchSource, ShallowRef } from 'vue'; type Timer = ReturnType<typeof setTimeout> | undefined; interface CacheData<D = any, p = any> { data: D; params: p; time: number; } interface CacheMapValue extends CacheData { timer: Timer; } type CachedKey = string; declare const setCache: (key: CachedKey, value: CacheData, cacheTime: number) => void; declare const getCache: (key: CachedKey) => CacheMapValue; declare const clearCache: (key?: CachedKey | CachedKey[]) => void; interface DebounceOptionsBase { leading?: boolean; trailing?: boolean; maxWait?: number; } interface ThrottleOptionsBase { leading?: boolean; trailing?: boolean; } type Params<P extends any[]> = Ref<P> | P | P[0] | Ref<P[0]>; type MaybePromise<T> = T | Promise<T>; type UseRequest<R, P extends unknown[] = any> = (service: Service<R, P>, options?: Options<R, P>, plugins?: Plugin<R, P>[]) => UseRequestResult<R, P>; type UseRequestMiddleware<R, P extends unknown[]> = (next: UseRequest<R, P>) => (service: Service<R, P>, options?: Options<R, P>, plugins?: Plugin<R, P>[]) => UseRequestResult<R, P>; interface Options<R, P extends any[]> { id?: string; manual?: boolean; defaultData?: R | Ref<R>; defaultParams?: Params<P>; refreshDeps?: WatchSource<any>[] | WatchSource<any>; refreshDepsParams?: Params<P> | ((value: P, oldValue: P) => void | Params<P>); retryCount?: number; retryInterval?: number; loadingDelay?: number; pollingInterval?: Ref<number> | number; pollingErrorRetryCount?: number; ready?: (() => Ref<boolean> | boolean) | (Ref<boolean> | boolean); debounceWait?: Ref<number> | number; debounceOptions?: Ref<DebounceOptionsBase> | DebounceOptionsBase; throttleWait?: Ref<number> | number; throttleOptions?: Ref<ThrottleOptionsBase> | ThrottleOptionsBase; cacheKey?: string | ((params?: P) => string); cacheTime?: number; staleTime?: number; getCache?: (cacheKey: string) => CacheData; setCache?: (cacheKey: string, cacheData: CacheData) => void; refreshOnWindowFocus?: Ref<boolean> | boolean; cancelOnWindowBlur?: Ref<boolean> | boolean; focusTimespan?: Ref<number> | number; onCache?: (response: R) => void; onBefore?: (params: P) => void; onRequest?: ({ params, response, error, abort }: { params: P; response: R; error: any; abort: boolean; }) => void; onSuccess?: (response: R, params: P) => MaybePromise<void | R>; onError?: (err: any, params: P) => void; onFinally?: () => void; onCancel?: () => void; use?: UseRequestMiddleware<R, P>[]; [key: string]: any; } type PluginHooks<R, P extends unknown[]> = { onBefore: (params: P) => onBeforePlugin | void; onInit: (service: (...args: P) => Promise<R>) => { servicePromise: Promise<R>; }; onSuccess(data: R, params: P): void; onError(error: Error, params: P): void; onFinally(params: P, data: R, error: Error): void; onCancel(): void; onMutate(data: R): void; }; interface Instance<R, P extends unknown[]> extends State<R, P> { functionContext: FunctionContext<R, P>; plugins: Ref<Partial<PluginHooks<R, P>>[]>; } type Plugin<R, P extends unknown[]> = (instance: Instance<R, P>, options: Options<R, P>) => Partial<PluginHooks<R, P>>; type State<R, P> = { status: Ref<'pending' | 'settled'>; data: Ref<R>; loading: Ref<boolean>; error: ShallowRef<any>; params: Ref<P>; pollingCount: Ref<number>; requestTick: (callback?: (context: { params: P; data: R; }) => void) => Promise<{ params: P; data: R; }>; [key: string]: any; }; type MutateData<R> = (newData: R) => void; type MutateFunction<R> = (arg: (oldData: R) => R) => void; interface Mutate<R> extends MutateData<R>, MutateFunction<R> { } type FunctionContext<R, P extends unknown[]> = { runAsync: (...arg: P) => Promise<R>; run: (...arg: P) => void; cancel: () => void; refresh: () => void; refreshAsync: () => Promise<R>; mutate: Mutate<R>; }; interface UseRequestResult<R, P extends unknown[]> extends State<R, P>, FunctionContext<R, P> { } type Service<R, P extends unknown[]> = (...args: P) => Promise<R>; type onBeforePlugin = { returnNow?: boolean; returnData?: any; returnType?: 'cache'; }; type CallPlugin<R> = { servicePromise?: Promise<R>; } & onBeforePlugin; declare function getRequest<R, P extends unknown[] = any>(id: string): UseRequestResult<R, P> | undefined; declare function debounce(func: (...args: any[]) => any, wait: number, options?: { leading?: boolean; trailing?: boolean; maxWait?: number; }): { (this: any, ...args: any[]): any; cancel: () => void; flush: () => any; pending: () => boolean; }; declare function throttle(func: (...args: any[]) => any, wait: number, options?: { leading?: boolean; trailing?: boolean; maxWait?: number; }): { (this: any, ...args: any[]): any; cancel: () => void; flush: () => any; pending: () => boolean; }; /** * 类型检查器集合 */ declare const TypeChecker: { isNull: (v: unknown) => v is null; isUndefined: (v: unknown) => v is undefined; isObject: (v: unknown) => v is object; isArray: (v: unknown) => v is unknown[]; isString: (v: unknown) => v is string; isNumber: (v: unknown) => v is number; isBoolean: (v: unknown) => v is boolean; isFunction: (v: unknown) => v is Function; isRegExp: (v: unknown) => v is RegExp; isDate: (v: unknown) => v is Date; isError: (v: unknown) => v is Error; isSymbol: (v: unknown) => v is symbol; isBigInt: (v: unknown) => v is bigint; isPromise: <T = any>(v: any) => v is Promise<T>; isEmpty: (v: unknown) => boolean; }; /** * 包装 Promise 函数 * @param service 返回 Promise 的服务函数 * @param call 处理结果的回调函数 * @returns 包装后的异步函数 */ declare function wrappedPromise<T, R, P extends unknown[]>(service: (...args: P) => Promise<T>, call: (result: T, params: P) => R | Promise<R>): (...args: P) => Promise<R>; declare const trigger: (key: string, data: any) => void; declare function useRequestConfig<R, P extends any[]>(options: Options<R, P>): void; interface PluginPriorityItem { name: string; priority: number; } declare function definePlugins(plugins?: Plugin<any, any[]>[], priorityItems?: PluginPriorityItem[]): void; declare function useRequest<R, P extends unknown[] = any>(service: Service<R, P>, options?: Options<R, P>, plugins?: Plugin<R, P>[]): any; export { type CallPlugin, type FunctionContext, type Instance, type MaybePromise, type Mutate, type Options, type Params, type Plugin, type PluginHooks, type Service, type State, TypeChecker, type UseRequest, type UseRequestMiddleware, type UseRequestResult, clearCache, debounce, definePlugins, getCache, getRequest, type onBeforePlugin, setCache, throttle, trigger, useRequest, useRequestConfig, wrappedPromise };