Skip to content

Commit c5e59ab

Browse files
committed
fix: 解决ts报错
1 parent 9a2f35a commit c5e59ab

File tree

14 files changed

+114
-137
lines changed

14 files changed

+114
-137
lines changed

.env

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ VITE_ICON_LOCAL_PREFIX=icon-local
1414
# constant route & auth route mode: static | dynamic
1515
VITE_AUTH_ROUTE_MODE=static
1616

17-
VITE_CONSTANT_ROUTE_MODE=static
18-
1917
# static auth route home path
2018
VITE_ROUTE_HOME=/home
2119

packages/hooks/src/use-request/Fetch.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export default class Fetch<TData extends FlatResponseData, TParams extends any[]
1313

1414
count: number = 0;
1515

16+
options: Options<TData, TParams>;
17+
1618
state: FetchState<TData, TParams> = {
1719
data: undefined,
1820
error: null,
@@ -21,25 +23,25 @@ export default class Fetch<TData extends FlatResponseData, TParams extends any[]
2123
response: null
2224
};
2325

24-
// eslint-disable-next-line max-params
2526
constructor(
2627
public serviceRef: MutableRefObject<Service<TData, TParams>>,
2728
public options: Options<TData, TParams>,
28-
public subscribe: Subscribe,
29-
public initState: Partial<FetchState<TData, TParams>> = {}
29+
public subscribe: Subscribe
3030
) {
3131
this.state = {
3232
...this.state,
33-
loading: !options.manual,
34-
...initState
33+
loading: !options.manual
3534
};
35+
36+
this.options = options;
3637
}
3738

3839
setState(s: Partial<FetchState<TData, TParams>> = {}) {
3940
this.state = {
4041
...this.state,
4142
...s
4243
};
44+
4345
this.subscribe();
4446
}
4547

@@ -143,6 +145,7 @@ export default class Fetch<TData extends FlatResponseData, TParams extends any[]
143145

144146
cancel() {
145147
this.count += 1;
148+
146149
this.setState({
147150
loading: false
148151
});

packages/hooks/src/use-request/plugins/useAutoRunPlugin.ts

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,21 @@
1-
import { useUpdateEffect } from 'ahooks';
2-
import { useRef } from 'react';
1+
import { useEffect } from 'react';
32

43
import type { Plugin } from '../type';
54

65
// support refreshDeps & ready
76
const useAutoRunPlugin: Plugin<any, any[]> = (
87
fetchInstance,
9-
{ defaultParams = [], manual, ready = true, refreshDeps = [], refreshDepsAction }
8+
{ manual, params = {}, ready = true, refreshDepsAction }
109
) => {
11-
const hasAutoRun = useRef(false);
12-
hasAutoRun.current = false;
13-
14-
useUpdateEffect(() => {
10+
useEffect(() => {
1511
if (!manual && ready) {
16-
hasAutoRun.current = true;
17-
fetchInstance.run(...defaultParams);
18-
}
19-
}, [ready]);
20-
21-
useUpdateEffect(() => {
22-
if (hasAutoRun.current) {
23-
return;
24-
}
25-
if (!manual) {
26-
hasAutoRun.current = true;
2712
if (refreshDepsAction) {
2813
refreshDepsAction();
2914
} else {
30-
fetchInstance.refresh();
15+
fetchInstance.runAsync(params);
3116
}
3217
}
33-
}, [...refreshDeps]);
18+
}, [JSON.stringify(params), ready]);
3419

3520
return {
3621
onBefore: () => {

packages/hooks/src/use-request/plugins/useCachePlugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ const useCachePlugin: Plugin<any, any[]> = (
1111
fetchInstance,
1212
{ cacheKey, cacheTime = 5 * 60 * 1000, getCache: customGetCache, setCache: customSetCache, staleTime = 0 }
1313
) => {
14-
const unSubscribeRef = useRef<() => void>();
14+
const unSubscribeRef = useRef<() => void>(null);
1515

16-
const currentPromiseRef = useRef<Promise<any>>();
16+
const currentPromiseRef = useRef<Promise<any>>(null);
1717

1818
const _setCache = (key: string, cachedData: CachedData) => {
1919
if (customSetCache) {

packages/hooks/src/use-request/plugins/useDebouncePlugin.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { DebounceSettings, DebouncedFunc } from 'lodash';
2-
import debounce from 'lodash/debounce';
1+
import type { DebounceSettings, DebouncedFunc } from 'lodash-es';
2+
import { debounce } from 'lodash-es';
33
import { useEffect, useMemo, useRef } from 'react';
44

55
import type { Plugin } from '../type';
@@ -8,7 +8,7 @@ const useDebouncePlugin: Plugin<any, any[]> = (
88
fetchInstance,
99
{ debounceLeading, debounceMaxWait, debounceTrailing, debounceWait }
1010
) => {
11-
const debouncedRef = useRef<DebouncedFunc<any>>();
11+
const debouncedRef = useRef<DebouncedFunc<any>>(null);
1212

1313
const options = useMemo(() => {
1414
const ret: DebounceSettings = {};

packages/hooks/src/use-request/plugins/useLoadingDelayPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useRef } from 'react';
33
import type { Plugin, Timeout } from '../type';
44

55
const useLoadingDelayPlugin: Plugin<any, any[]> = (fetchInstance, { loadingDelay, ready }) => {
6-
const timerRef = useRef<Timeout>();
6+
const timerRef = useRef<Timeout>(null);
77

88
if (!loadingDelay) {
99
return {};

packages/hooks/src/use-request/plugins/usePollingPlugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ const usePollingPlugin: Plugin<any, any[]> = (
99
fetchInstance,
1010
{ pollingErrorRetryCount = -1, pollingInterval, pollingWhenHidden = true }
1111
) => {
12-
const timerRef = useRef<Timeout>();
13-
const unsubscribeRef = useRef<() => void>();
12+
const timerRef = useRef<Timeout>(null);
13+
const unsubscribeRef = useRef<() => void>(null);
1414
const countRef = useRef<number>(0);
1515

1616
const stopPolling = () => {

packages/hooks/src/use-request/plugins/useRefreshOnWindowFocusPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const useRefreshOnWindowFocusPlugin: Plugin<any, any[]> = (
99
fetchInstance,
1010
{ focusTimespan = 5000, refreshOnWindowFocus }
1111
) => {
12-
const unsubscribeRef = useRef<() => void>();
12+
const unsubscribeRef = useRef<() => void>(null);
1313

1414
const stopSubscribe = () => {
1515
unsubscribeRef.current?.();

packages/hooks/src/use-request/plugins/useRetryPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useRef } from 'react';
33
import type { Plugin, Timeout } from '../type';
44

55
const useRetryPlugin: Plugin<any, any[]> = (fetchInstance, { retryCount, retryInterval }) => {
6-
const timerRef = useRef<Timeout>();
6+
const timerRef = useRef<Timeout>(null);
77
const countRef = useRef(0);
88

99
const triggerByRetry = useRef(false);

packages/hooks/src/use-request/plugins/useThrottlePlugin.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import type { DebouncedFunc, ThrottleSettings } from 'lodash';
2-
import throttle from 'lodash/throttle';
1+
import type { DebouncedFunc, ThrottleSettings } from 'lodash-es';
2+
import { throttle } from 'lodash-es';
33
import { useEffect, useRef } from 'react';
44

55
import type { Plugin } from '../type';
66

77
const useThrottlePlugin: Plugin<any, any[]> = (fetchInstance, { throttleLeading, throttleTrailing, throttleWait }) => {
8-
const throttledRef = useRef<DebouncedFunc<any>>();
8+
const throttledRef = useRef<DebouncedFunc<any>>(null);
99

1010
const options: ThrottleSettings = {};
1111
if (throttleLeading !== undefined) {
Lines changed: 85 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { FlatResponseData } from '@sa/axios';
21
import type { AxiosError } from 'axios';
3-
import type { DependencyList } from 'react';
2+
3+
import type { FlatResponseData } from '@sa/axios';
44

55
import type Fetch from './Fetch';
66
import type { CachedData } from './utils/cache';
@@ -11,100 +11,102 @@ export type Subscribe = () => void;
1111
// for Fetch
1212

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

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

4343
// for useRequestImplement
4444

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

9697
export type Plugin<TData extends FlatResponseData, TParams extends any[]> = {
97-
(fetchInstance: Fetch<TData, TParams>, options: Options<TData, TParams>): PluginReturn<TData, TParams>;
98-
onInit?: (options: Options<TData, TParams>) => Partial<FetchState<TData, TParams>>;
98+
  (fetchInstance: Fetch<TData, TParams>, options: Options<TData, TParams>): PluginReturn<TData, TParams>;
99+
  onInit?: (options: Options<TData, TParams>) => Partial<FetchState<TData, TParams>>;
99100
};
100101

101102
export interface Result<TData extends FlatResponseData, TParams extends any[]> extends FetchState<TData, TParams> {
102-
cancel: Fetch<TData, TParams>['cancel'];
103-
mutate: Fetch<TData, TParams>['mutate'];
104-
refresh: Fetch<TData, TParams>['refresh'];
105-
refreshAsync: Fetch<TData, TParams>['refreshAsync'];
106-
run: Fetch<TData, TParams>['run'];
107-
runAsync: Fetch<TData, TParams>['runAsync'];
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'];
108109
}
109110

110111
export type Timeout = ReturnType<typeof setTimeout>;
112+

0 commit comments

Comments
 (0)