Ben Lin
2024-07-23 d9e0d8f77f29c00b14925f8398bc526682c9dd38
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import type { MaybeRef, Ref, WatchSource } from 'vue';
 
import type Fetch from './Fetch';
import type { CachedData } from './utils/cache';
 
export type Service<TData, TParams extends any[]> = (...args: TParams) => Promise<TData>;
export type Subscribe = () => void;
 
// for Fetch
export interface FetchState<TData, TParams extends any[]> {
  loading: boolean;
  params?: TParams;
  data?: TData;
  error?: Error;
}
 
export interface PluginReturn<TData, TParams extends any[]> {
  onBefore?: (params: TParams) =>
    | ({
        stopNow?: boolean;
        returnNow?: boolean;
      } & Partial<FetchState<TData, TParams>>)
    | void;
 
  onRequest?: (
    service: Service<TData, TParams>,
    params: TParams,
  ) => {
    servicePromise?: Promise<TData>;
  };
 
  onSuccess?: (data: TData, params: TParams) => void;
  onError?: (e: Error, params: TParams) => void;
  onFinally?: (params: TParams, data?: TData, e?: Error) => void;
  onCancel?: () => void;
  onMutate?: (data: TData) => void;
}
 
// for useRequestImplement
export interface UseRequestOptions<TData, TParams extends any[]> {
  manual?: MaybeRef<boolean>;
 
  onBefore?: (params: TParams) => void;
  onSuccess?: (data: TData, params: TParams) => void;
  onError?: (e: Error, params: TParams) => void;
  // formatResult?: (res: any) => TData;
  onFinally?: (params: TParams, data?: TData, e?: Error) => void;
 
  defaultParams?: TParams;
 
  // refreshDeps
  refreshDeps?: WatchSource<any>[];
  refreshDepsAction?: () => void;
 
  // loading delay
  loadingDelay?: number;
 
  // polling
  pollingInterval?: number;
  pollingWhenHidden?: boolean;
  pollingErrorRetryCount?: number;
 
  // refresh on window focus
  refreshOnWindowFocus?: boolean;
  focusTimespan?: number;
 
  // debounce
  debounceWait?: number;
  debounceLeading?: boolean;
  debounceTrailing?: boolean;
  debounceMaxWait?: number;
 
  // throttle
  throttleWait?: number;
  throttleLeading?: boolean;
  throttleTrailing?: boolean;
 
  // cache
  cacheKey?: string;
  cacheTime?: number;
  staleTime?: number;
  setCache?: (data: CachedData<TData, TParams>) => void;
  getCache?: (params: TParams) => CachedData<TData, TParams> | undefined;
 
  // retry
  retryCount?: number;
  retryInterval?: number;
 
  // ready
  ready?: MaybeRef<boolean>;
 
  // [key: string]: any;
}
 
export interface UseRequestPlugin<TData, TParams extends any[]> {
  // eslint-disable-next-line prettier/prettier
  (
    fetchInstance: Fetch<TData, TParams>,
    options: UseRequestOptions<TData, TParams>,
  ): PluginReturn<TData, TParams>;
  onInit?: (options: UseRequestOptions<TData, TParams>) => Partial<FetchState<TData, TParams>>;
}
 
// for index
// export type OptionsWithoutFormat<TData, TParams extends any[]> = Omit<Options<TData, TParams>, 'formatResult'>;
 
// export interface OptionsWithFormat<TData, TParams extends any[], TFormated, TTFormated extends TFormated = any> extends Omit<Options<TTFormated, TParams>, 'formatResult'> {
//   formatResult: (res: TData) => TFormated;
// };
 
export interface UseRequestResult<TData, TParams extends any[]> {
  loading: Ref<boolean>;
  data: Ref<TData>;
  error: Ref<Error>;
  params: Ref<TParams | []>;
  cancel: Fetch<TData, TParams>['cancel'];
  refresh: Fetch<TData, TParams>['refresh'];
  refreshAsync: Fetch<TData, TParams>['refreshAsync'];
  run: Fetch<TData, TParams>['run'];
  runAsync: Fetch<TData, TParams>['runAsync'];
  mutate: Fetch<TData, TParams>['mutate'];
}
 
export type UseRequestTimeout = ReturnType<typeof setTimeout>;