Skip to content

Commit f675f19

Browse files
committed
optimize: 优化代码
1 parent c5e59ab commit f675f19

File tree

1 file changed

+72
-84
lines changed
  • packages/hooks/src/use-request

1 file changed

+72
-84
lines changed
Lines changed: 72 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import type { AxiosError } from 'axios';
2-
31
import type { FlatResponseData } from '@sa/axios';
2+
import type { AxiosError } from 'axios';
43

54
import type Fetch from './Fetch';
65
import type { CachedData } from './utils/cache';
@@ -11,102 +10,91 @@ export type Subscribe = () => void;
1110
// for Fetch
1211

1312
export interface FetchState<TData extends FlatResponseData, TParams extends any[]> {
14-
  loading: boolean;
15-
  params?: TParams;
16-
  response: TData['response'];
17-
  data: NonNullable<TData['data']>;
18-
  error: AxiosError | null;
13+
data: NonNullable<TData['data']>;
14+
error: AxiosError | null;
15+
loading: boolean;
16+
params?: TParams;
17+
response: TData['response'];
1918
}
2019

2120
export interface PluginReturn<TData extends FlatResponseData, TParams extends any[]> {
22-
  onBefore?: (params: TParams) =>
23-
    | ({
24-
        stopNow?: boolean;
25-
        returnNow?: boolean;
26-
      } & Partial<FetchState<FlatResponseData, TParams>>)
27-
    | null;
28-
29-
  onRequest?: (
30-
    service: Service<TData, TParams>,
31-
    params: TParams
32-
  ) => {
33-
    servicePromise?: Promise<TData>;
34-
  };
35-
36-
  onSuccess?: (data: TData['data'], params: TParams) => void;
37-
  onError?: (e: AxiosError, params: TParams) => void;
38-
  onFinally?: (params: TParams, data?: TData, e?: AxiosError) => void;
39-
  onCancel?: () => void;
40-
  onMutate?: (data: TData['data']) => void;
21+
onBefore?: (params: TParams) =>
22+
| ({
23+
returnNow?: boolean;
24+
stopNow?: boolean;
25+
} & Partial<FetchState<FlatResponseData, TParams>>)
26+
| null;
27+
28+
onCancel?: () => void;
29+
30+
onError?: (e: AxiosError, params: TParams) => void;
31+
onFinally?: (params: TParams, data?: TData, e?: AxiosError) => void;
32+
onMutate?: (data: TData['data']) => void;
33+
onRequest?: (
34+
service: Service<TData, TParams>,
35+
params: TParams
36+
) => {
37+
servicePromise?: Promise<TData>;
38+
};
39+
onSuccess?: (data: TData['data'], params: TParams) => void;
4140
}
4241

4342
// for useRequestImplement
4443

4544
export interface Options<TData extends FlatResponseData, TParams extends any[]> {
46-
  manual?: boolean;
47-
  onBefore?: (params: TParams) => void;
48-
  onSuccess?: (data: TData['data'], params: TParams) => void;
49-
  onError?: (e: Error, params: TParams) => void;
50-
  defaultData?: TData['data'];
51-
  onFinally?: (params: TParams, data: TData['data'] | null, e: Error | null) => void;
52-
  defaultParams?: TParams;
53-
  // refreshDeps
54-
  params?: TParams[0];
55-
  refreshDepsAction?: () => void;
56-
57-
  // loading delay
58-
  loadingDelay?: number;
59-
60-
  // polling
61-
  pollingInterval?: number;
62-
  pollingWhenHidden?: boolean;
63-
  pollingErrorRetryCount?: number;
64-
65-
  // refresh on window focus
66-
  refreshOnWindowFocus?: boolean;
67-
  focusTimespan?: number;
68-
69-
  // debounce
70-
  debounceWait?: number;
71-
  debounceLeading?: boolean;
72-
  debounceTrailing?: boolean;
73-
  debounceMaxWait?: number;
74-
75-
  // throttle
76-
  throttleWait?: number;
77-
  throttleLeading?: boolean;
78-
  throttleTrailing?: boolean;
79-
80-
  // cache
81-
  cacheKey?: string;
82-
  cacheTime?: number;
83-
  staleTime?: number;
84-
  setCache?: (data: CachedData<TData, TParams>) => void;
85-
  getCache?: (params: TParams) => CachedData<TData, TParams> | undefined;
86-
87-
  // retry
88-
  retryCount?: number;
89-
  retryInterval?: number;
90-
91-
  // ready
92-
  ready?: boolean;
93-
94-
  // [key: string]: any;
45+
// cache
46+
cacheKey?: string;
47+
cacheTime?: number;
48+
debounceLeading?: boolean;
49+
debounceMaxWait?: number;
50+
debounceTrailing?: boolean; // debounce
51+
debounceWait?: number;
52+
defaultData?: TData['data'];
53+
defaultParams?: TParams;
54+
focusTimespan?: number;
55+
56+
getCache?: (params: TParams) => CachedData<TData, TParams> | undefined; // loading delay
57+
58+
loadingDelay?: number;
59+
manual?: boolean;
60+
onBefore?: (params: TParams) => void;
61+
62+
onError?: (e: Error, params: TParams) => void;
63+
onFinally?: (params: TParams, data: TData['data'] | null, e: Error | null) => void;
64+
65+
onSuccess?: (data: TData['data'], params: TParams) => void; // refreshDeps
66+
params?: TParams[0];
67+
pollingErrorRetryCount?: number; // polling
68+
pollingInterval?: number;
69+
70+
pollingWhenHidden?: boolean; // ready
71+
ready?: boolean;
72+
refreshDepsAction?: () => void; // refresh on window focus
73+
74+
refreshOnWindowFocus?: boolean; // retry
75+
retryCount?: number;
76+
retryInterval?: number;
77+
setCache?: (data: CachedData<TData, TParams>) => void;
78+
staleTime?: number;
79+
80+
throttleLeading?: boolean;
81+
throttleTrailing?: boolean; // throttle
82+
83+
throttleWait?: number; // [key: string]: any;
9584
}
9685

9786
export type Plugin<TData extends FlatResponseData, TParams extends any[]> = {
98-
  (fetchInstance: Fetch<TData, TParams>, options: Options<TData, TParams>): PluginReturn<TData, TParams>;
99-
  onInit?: (options: Options<TData, TParams>) => Partial<FetchState<TData, TParams>>;
87+
(fetchInstance: Fetch<TData, TParams>, options: Options<TData, TParams>): PluginReturn<TData, TParams>;
88+
onInit?: (options: Options<TData, TParams>) => Partial<FetchState<TData, TParams>>;
10089
};
10190

10291
export interface Result<TData extends FlatResponseData, TParams extends any[]> extends FetchState<TData, TParams> {
103-
  cancel: Fetch<TData, TParams>['cancel'];
104-
  refresh: Fetch<TData, TParams>['refresh'];
105-
  refreshAsync: Fetch<TData, TParams>['refreshAsync'];
106-
  run: Fetch<TData, TParams>['run'];
107-
  runAsync: Fetch<TData, TParams>['runAsync'];
108-
  mutate: Fetch<TData['data'], TParams>['mutate'];
92+
cancel: Fetch<TData, TParams>['cancel'];
93+
mutate: Fetch<TData['data'], TParams>['mutate'];
94+
refresh: Fetch<TData, TParams>['refresh'];
95+
refreshAsync: Fetch<TData, TParams>['refreshAsync'];
96+
run: Fetch<TData, TParams>['run'];
97+
runAsync: Fetch<TData, TParams>['runAsync'];
10998
}
11099

111100
export type Timeout = ReturnType<typeof setTimeout>;
112-

0 commit comments

Comments
 (0)