@@ -4,7 +4,7 @@ import { noop, until } from '@vueuse/shared'
4
4
import axios , { AxiosError } from 'axios'
5
5
import { ref , shallowRef } from 'vue'
6
6
7
- export interface UseAxiosReturn < T , R = AxiosResponse < T > , _D = any > {
7
+ export interface UseAxiosReturn < T , R = AxiosResponse < T > , _D = any , O extends UseAxiosOptions = UseAxiosOptions < T > > {
8
8
/**
9
9
* Axios Response
10
10
*/
@@ -13,7 +13,7 @@ export interface UseAxiosReturn<T, R = AxiosResponse<T>, _D = any> {
13
13
/**
14
14
* Axios response data
15
15
*/
16
- data : Ref < T | undefined >
16
+ data : O extends UseAxiosOptionsWithInitialData < T > ? Ref < T > : Ref < T | undefined >
17
17
18
18
/**
19
19
* Indicates if the request has finished
@@ -50,19 +50,19 @@ export interface UseAxiosReturn<T, R = AxiosResponse<T>, _D = any> {
50
50
*/
51
51
isCanceled : Ref < boolean >
52
52
}
53
- export interface StrictUseAxiosReturn < T , R , D > extends UseAxiosReturn < T , R , D > {
53
+ export interface StrictUseAxiosReturn < T , R , D , O extends UseAxiosOptions = UseAxiosOptions < T > > extends UseAxiosReturn < T , R , D , O > {
54
54
/**
55
55
* Manually call the axios request
56
56
*/
57
- execute : ( url ?: string | AxiosRequestConfig < D > , config ?: AxiosRequestConfig < D > ) => Promise < StrictUseAxiosReturn < T , R , D > >
57
+ execute : ( url ?: string | AxiosRequestConfig < D > , config ?: AxiosRequestConfig < D > ) => Promise < StrictUseAxiosReturn < T , R , D , O > >
58
58
}
59
59
export interface EasyUseAxiosReturn < T , R , D > extends UseAxiosReturn < T , R , D > {
60
60
/**
61
61
* Manually call the axios request
62
62
*/
63
63
execute : ( url : string , config ?: AxiosRequestConfig < D > ) => Promise < EasyUseAxiosReturn < T , R , D > >
64
64
}
65
- export interface UseAxiosOptions < T = any > {
65
+ export interface UseAxiosOptionsBase < T = any > {
66
66
/**
67
67
* Will automatically run axios request when `useAxios` is used
68
68
*
@@ -93,11 +93,6 @@ export interface UseAxiosOptions<T = any> {
93
93
*/
94
94
onSuccess ?: ( data : T ) => void
95
95
96
- /**
97
- * Initial data to use
98
- */
99
- initialData ?: T
100
-
101
96
/**
102
97
* Sets the state to initialState before executing the promise.
103
98
*/
@@ -108,11 +103,24 @@ export interface UseAxiosOptions<T = any> {
108
103
*/
109
104
onFinish ?: ( ) => void
110
105
}
106
+
107
+ export interface UseAxiosOptionsWithInitialData < T > extends UseAxiosOptionsBase < T > {
108
+ /**
109
+ * Initial data
110
+ */
111
+ initialData : T
112
+ }
113
+
114
+ export type UseAxiosOptions < T = any > = UseAxiosOptionsBase < T > | UseAxiosOptionsWithInitialData < T >
115
+
111
116
type OverallUseAxiosReturn < T , R , D > = StrictUseAxiosReturn < T , R , D > | EasyUseAxiosReturn < T , R , D >
112
117
113
- export function useAxios < T = any , R = AxiosResponse < T > , D = any > ( url : string , config ?: AxiosRequestConfig < D > , options ?: UseAxiosOptions ) : StrictUseAxiosReturn < T , R , D > & Promise < StrictUseAxiosReturn < T , R , D > >
114
- export function useAxios < T = any , R = AxiosResponse < T > , D = any > ( url : string , instance ?: AxiosInstance , options ?: UseAxiosOptions ) : StrictUseAxiosReturn < T , R , D > & Promise < StrictUseAxiosReturn < T , R , D > >
115
- export function useAxios < T = any , R = AxiosResponse < T > , D = any > ( url : string , config : AxiosRequestConfig < D > , instance : AxiosInstance , options ?: UseAxiosOptions ) : StrictUseAxiosReturn < T , R , D > & Promise < StrictUseAxiosReturn < T , R , D > >
118
+ export function useAxios < T = any , R = AxiosResponse < T > , D = any , O extends UseAxiosOptionsWithInitialData < T > = UseAxiosOptionsWithInitialData < T > > ( url : string , config ?: AxiosRequestConfig < D > , options ?: O ) : StrictUseAxiosReturn < T , R , D , O > & Promise < StrictUseAxiosReturn < T , R , D , O > >
119
+ export function useAxios < T = any , R = AxiosResponse < T > , D = any , O extends UseAxiosOptionsWithInitialData < T > = UseAxiosOptionsWithInitialData < T > > ( url : string , instance ?: AxiosInstance , options ?: O ) : StrictUseAxiosReturn < T , R , D , O > & Promise < StrictUseAxiosReturn < T , R , D , O > >
120
+ export function useAxios < T = any , R = AxiosResponse < T > , D = any , O extends UseAxiosOptionsWithInitialData < T > = UseAxiosOptionsWithInitialData < T > > ( url : string , config : AxiosRequestConfig < D > , instance : AxiosInstance , options ?: O ) : StrictUseAxiosReturn < T , R , D , O > & Promise < StrictUseAxiosReturn < T , R , D , O > >
121
+ export function useAxios < T = any , R = AxiosResponse < T > , D = any , O extends UseAxiosOptionsBase < T > = UseAxiosOptionsBase < T > > ( url : string , config ?: AxiosRequestConfig < D > , options ?: O ) : StrictUseAxiosReturn < T , R , D , O > & Promise < StrictUseAxiosReturn < T , R , D , O > >
122
+ export function useAxios < T = any , R = AxiosResponse < T > , D = any , O extends UseAxiosOptionsBase < T > = UseAxiosOptionsBase < T > > ( url : string , instance ?: AxiosInstance , options ?: O ) : StrictUseAxiosReturn < T , R , D , O > & Promise < StrictUseAxiosReturn < T , R , D , O > >
123
+ export function useAxios < T = any , R = AxiosResponse < T > , D = any , O extends UseAxiosOptionsBase < T > = UseAxiosOptionsBase < T > > ( url : string , config : AxiosRequestConfig < D > , instance : AxiosInstance , options ?: O ) : StrictUseAxiosReturn < T , R , D , O > & Promise < StrictUseAxiosReturn < T , R , D , O > >
116
124
export function useAxios < T = any , R = AxiosResponse < T > , D = any > ( config ?: AxiosRequestConfig < D > ) : EasyUseAxiosReturn < T , R , D > & Promise < EasyUseAxiosReturn < T , R , D > >
117
125
export function useAxios < T = any , R = AxiosResponse < T > , D = any > ( instance ?: AxiosInstance ) : EasyUseAxiosReturn < T , R , D > & Promise < EasyUseAxiosReturn < T , R , D > >
118
126
export function useAxios < T = any , R = AxiosResponse < T > , D = any > ( config ?: AxiosRequestConfig < D > , instance ?: AxiosInstance ) : EasyUseAxiosReturn < T , R , D > & Promise < EasyUseAxiosReturn < T , R , D > >
@@ -160,14 +168,14 @@ export function useAxios<T = any, R = AxiosResponse<T>, D = any>(...args: any[])
160
168
}
161
169
162
170
const {
163
- initialData,
164
171
shallow,
165
172
onSuccess = noop ,
166
173
onError = noop ,
167
174
immediate,
168
175
resetOnExecute = false ,
169
176
} = options
170
177
178
+ const initialData = ( options as UseAxiosOptionsWithInitialData < T > ) . initialData
171
179
const response = shallowRef < AxiosResponse < T > > ( )
172
180
const data = ( shallow ? shallowRef : ref ) < T > ( initialData ! ) as Ref < T >
173
181
const isFinished = ref ( false )
0 commit comments