File tree Expand file tree Collapse file tree 6 files changed +1273
-450
lines changed Expand file tree Collapse file tree 6 files changed +1273
-450
lines changed Original file line number Diff line number Diff line change 1
-
2
1
{
3
- "presets" : [" @babel/preset-env" ],
4
- "plugins" : [" transform-runtime" ]
5
- }
2
+ "presets" : [
3
+ " @babel/preset-env"
4
+ ],
5
+ "plugins" : [
6
+ " @babel/transform-runtime"
7
+ ]
8
+ }
Original file line number Diff line number Diff line change 29
29
}
30
30
},
31
31
"dependencies" : {
32
- "@babel/preset-env" : " ^7.0.0" ,
33
- "@vuex-orm/core" : " ^0.26.2" ,
32
+ "@vuex-orm/core" : " ^0.30.0" ,
34
33
"axios" : " ^0.18.0" ,
35
34
"lodash" : " ^4.17.11"
36
35
},
37
36
"devDependencies" : {
38
- "@babel/core" : " ^7.0.0 " ,
37
+ "@babel/core" : " ^7.1.6 " ,
39
38
"babel-core" : " 7.0.0-bridge.0" ,
40
- "babel-jest" : " ^23.4.2" ,
41
- "babel-loader" : " ^8.0.0" ,
39
+ "@babel/runtime" : " ^7.1.5" ,
40
+ "@babel/plugin-transform-runtime" : " ^7.1.0" ,
41
+ "@babel/preset-env" : " ^7.1.6" ,
42
+ "babel-jest" : " ^23.6.0" ,
43
+ "babel-loader" : " ^8.0.4" ,
42
44
"babel-plugin-transform-runtime" : " ^6.23.0" ,
43
45
"babel-preset-env" : " ^1.7.0" ,
44
46
"eslint" : " ^5.4.0" ,
49
51
"webpack" : " ^4.17.1" ,
50
52
"webpack-cli" : " ^3.1.0"
51
53
}
52
- }
54
+ }
Original file line number Diff line number Diff line change @@ -3,10 +3,7 @@ import axios from 'axios';
3
3
export default class Axios {
4
4
constructor ( http ) {
5
5
this . instance = axios . create ( http ) ;
6
-
7
- if ( http . access_token ) {
8
- this . instance . defaults . headers . common [ 'Authorization' ] = `Bearer ${ http . access_token } ` ;
9
- }
6
+ this . setAuthentication ( http . access_token ) ;
10
7
11
8
this . instance . interceptors . response . use (
12
9
response => http . onResponse ( response ) ,
@@ -16,6 +13,14 @@ export default class Axios {
16
13
return this . instance ;
17
14
}
18
15
16
+ setAuthentication ( token ) {
17
+ if ( ! token ) return ;
18
+ const isFunction = typeof token === "function" ;
19
+ const tokenStr = isFunction ? token ( ) : token ;
20
+
21
+ this . instance . defaults . headers . common [ 'Authorization' ] = `Bearer ${ tokenStr } ` ;
22
+ }
23
+
19
24
/**
20
25
* Head Request
21
26
* @param {string } url
Original file line number Diff line number Diff line change @@ -130,24 +130,18 @@ export const AxiosRequestConfig = {
130
130
* @param {object } error
131
131
*/
132
132
onError ( error ) {
133
- switch ( error . response . status ) {
134
- case 401 :
135
- this . onUnauthorised ( error ) ;
136
- break ;
137
- case 404 :
138
- this . onNotFound ( error ) ;
139
- break ;
140
- case 422 :
141
- this . onValidationError ( error ) ;
142
- break ;
143
- case 500 :
144
- this . onServerError ( error ) ;
145
- break ;
146
- default :
147
- this . onGenericError ( error ) ;
148
- break ;
133
+ const { response } = error ;
134
+ const errorTypes = {
135
+ 401 : this . onUnauthorised ,
136
+ 404 : this . onNotFound ,
137
+ 422 : this . onValidationError ,
138
+ 500 : this . onServerError
139
+ }
140
+ if ( response && response . status in errorTypes ) {
141
+ errorTypes [ response . status ] ( error ) ;
142
+ } else {
143
+ this . onGenericError ( error ) ;
149
144
}
150
-
151
145
return Promise . reject ( error ) ;
152
146
} ,
153
147
} ;
You can’t perform that action at this time.
0 commit comments