1
1
import Axios from 'axios'
2
+ import defu from 'defu'
2
3
< % if ( options . retry ) { % > import axiosRetry from 'axios-retry' < % } % >
3
4
4
5
// Axios.prototype cannot be modified
@@ -34,6 +35,9 @@ const axiosExtra = {
34
35
onError ( fn ) {
35
36
this . onRequestError ( fn )
36
37
this . onResponseError ( fn )
38
+ } ,
39
+ create ( options ) {
40
+ return createAxiosInstance ( defu ( options , this . defaults ) )
37
41
}
38
42
}
39
43
@@ -48,6 +52,24 @@ const extendAxiosInstance = axios => {
48
52
}
49
53
}
50
54
55
+ const createAxiosInstance = axiosOptions => {
56
+ // Create new axios instance
57
+ const axios = Axios . create ( axiosOptions )
58
+ axios . CancelToken = Axios . CancelToken
59
+ axios . isCancel = Axios . isCancel
60
+
61
+ // Extend axios proto
62
+ extendAxiosInstance ( axios )
63
+
64
+ // Setup interceptors
65
+ < % if ( options . debug ) { % > setupDebugInterceptor ( axios ) < % } % >
66
+ < % if ( options . credentials ) { % > setupCredentialsInterceptor ( axios ) < % } % >
67
+ < % if ( options . progress ) { % > setupProgress ( axios ) < % } % >
68
+ < % if ( options . retry ) { % > axiosRetry ( axios , < %= serialize ( options . retry ) % > ) < % } % >
69
+
70
+ return axios
71
+ }
72
+
51
73
< % if ( options . debug ) { % >
52
74
const log = ( level , ...messages ) => console [ level ] ( '[Axios]' , ...messages )
53
75
@@ -91,7 +113,7 @@ const setupCredentialsInterceptor = axios => {
91
113
} < % } % >
92
114
93
115
< % if ( options . progress ) { % >
94
- const setupProgress = ( axios , ctx ) => {
116
+ const setupProgress = ( axios ) => {
95
117
if ( process . server ) {
96
118
return
97
119
}
@@ -182,19 +204,7 @@ export default (ctx, inject) => {
182
204
axiosOptions . headers . common [ 'accept-encoding' ] = 'gzip, deflate'
183
205
}
184
206
185
- // Create new axios instance
186
- const axios = Axios . create ( axiosOptions )
187
- axios . CancelToken = Axios . CancelToken
188
- axios . isCancel = Axios . isCancel
189
-
190
- // Extend axios proto
191
- extendAxiosInstance ( axios )
192
-
193
- // Setup interceptors
194
- < % if ( options . debug ) { % > setupDebugInterceptor ( axios ) < % } % >
195
- < % if ( options . credentials ) { % > setupCredentialsInterceptor ( axios ) < % } % >
196
- < % if ( options . progress ) { % > setupProgress ( axios , ctx ) < % } % >
197
- < % if ( options . retry ) { % > axiosRetry ( axios , < %= serialize ( options . retry ) % > ) < % } % >
207
+ const axios = createAxiosInstance ( axiosOptions )
198
208
199
209
// Inject axios to the context as $axios
200
210
ctx . $axios = axios
0 commit comments