import type { FlatResponseData } from '@sa/axios'; import type { AxiosError } from 'axios'; import type Fetch from './Fetch'; import type { CachedData } from './utils/cache'; export type Service = (...args: TParams) => Promise; export type Subscribe = () => void; // for Fetch export interface FetchState { data: NonNullable; error: AxiosError | null; loading: boolean; params?: TParams; response: TData['response']; } export interface PluginReturn { onBefore?: (params: TParams) => | ({ returnNow?: boolean; stopNow?: boolean; } & Partial>) | null; onCancel?: () => void; onError?: (e: AxiosError, params: TParams) => void; onFinally?: (params: TParams, data?: TData, e?: AxiosError) => void; onMutate?: (data: TData['data']) => void; onRequest?: ( service: Service, params: TParams ) => { servicePromise?: Promise; }; onSuccess?: (data: TData['data'], params: TParams) => void; } // for useRequestImplement export interface Options { // cache cacheKey?: string; cacheTime?: number; debounceLeading?: boolean; debounceMaxWait?: number; debounceTrailing?: boolean; // debounce debounceWait?: number; defaultData?: TData['data']; defaultParams?: TParams; focusTimespan?: number; getCache?: (params: TParams) => CachedData | undefined; // loading delay loadingDelay?: number; manual?: boolean; onBefore?: (params: TParams) => void; onError?: (e: Error, params: TParams) => void; onFinally?: (params: TParams, data: TData['data'] | null, e: Error | null) => void; onSuccess?: (data: TData['data'], params: TParams) => void; // refreshDeps params?: TParams[0]; pollingErrorRetryCount?: number; // polling pollingInterval?: number; pollingWhenHidden?: boolean; // ready ready?: boolean; refreshDepsAction?: () => void; // refresh on window focus refreshOnWindowFocus?: boolean; // retry retryCount?: number; retryInterval?: number; setCache?: (data: CachedData) => void; staleTime?: number; throttleLeading?: boolean; throttleTrailing?: boolean; // throttle throttleWait?: number; // [key: string]: any; } export type Plugin = { (fetchInstance: Fetch, options: Options): PluginReturn; onInit?: (options: Options) => Partial>; }; export interface Result extends FetchState { cancel: Fetch['cancel']; mutate: Fetch['mutate']; refresh: Fetch['refresh']; refreshAsync: Fetch['refreshAsync']; run: Fetch['run']; runAsync: Fetch['runAsync']; } export type Timeout = ReturnType;