1
1
import { useEffect , useCallback , useContext , useReducer , useMemo } from "react" ;
2
- import type { Canceler } from "axios" ;
2
+ import type { Canceler , AxiosResponse } from "axios" ;
3
3
import { useRequest } from "./useRequest" ;
4
4
import type {
5
5
Payload ,
@@ -8,7 +8,6 @@ import type {
8
8
Request ,
9
9
RequestDispatcher ,
10
10
RequestCallbackFn ,
11
- AxiosRestResponse ,
12
11
Resource ,
13
12
} from "./request" ;
14
13
import type {
@@ -25,9 +24,12 @@ const REQUEST_CLEAR_MESSAGE =
25
24
26
25
type RequestState < TRequest extends Request > = {
27
26
data ?: Payload < TRequest > ;
28
- other ?: AxiosRestResponse < CData < TRequest > > ;
27
+ response ?: AxiosResponse < CData < TRequest > > ;
29
28
error ?: RequestError < Payload < TRequest > , CData < TRequest > > ;
30
29
isLoading ?: boolean ;
30
+
31
+ /** @deprecated Use `response` instead */
32
+ other ?: AxiosResponse < CData < TRequest > > ;
31
33
} ;
32
34
33
35
export type UseResourceResult < TRequest extends Request > = [
@@ -60,19 +62,23 @@ function getDefaultStateLoading<T extends Request>(
60
62
}
61
63
62
64
type Action < T , D = any > =
63
- | { type : "success" ; data : T ; other : AxiosRestResponse < D > }
65
+ | { type : "success" ; data : T ; response : AxiosResponse < D > }
64
66
| { type : "error" ; error : RequestError < T , D > }
65
67
| { type : "reset" | "start" } ;
66
68
67
69
function getNextState < TRequest extends Request > (
68
70
state : RequestState < TRequest > ,
69
71
action : Action < Payload < TRequest > , CData < TRequest > > ,
70
72
) : RequestState < TRequest > {
73
+ const response = action . type === "success" ? action . response : state . response ;
74
+
71
75
return {
72
76
data : action . type === "success" ? action . data : state . data ,
73
- other : action . type === "success" ? action . other : state . other ,
77
+ response ,
74
78
error : action . type === "error" ? action . error : undefined ,
75
79
isLoading : action . type === "start" ? true : false ,
80
+ // will be deleted
81
+ other : response ,
76
82
} ;
77
83
}
78
84
@@ -148,9 +154,9 @@ export function useResource<TRequest extends Request>(
148
154
void ( async ( ) => {
149
155
try {
150
156
getMountedState ( ) && dispatch ( { type : "start" } ) ;
151
- const [ data , other ] = await ready ( ) ;
157
+ const [ data , response ] = await ready ( ) ;
152
158
if ( getMountedState ( ) ) {
153
- dispatch ( { type : "success" , data, other } ) ;
159
+ dispatch ( { type : "success" , data, response } ) ;
154
160
155
161
cacheKey &&
156
162
requestCache &&
0 commit comments