File tree Expand file tree Collapse file tree 11 files changed +47
-6386
lines changed Expand file tree Collapse file tree 11 files changed +47
-6386
lines changed Original file line number Diff line number Diff line change 1
1
{
2
2
"name" : " @vuex-orm/plugin-axios" ,
3
- "version" : " 0.2.3 " ,
3
+ "version" : " 0.5.0 " ,
4
4
"description" : " Vuex-ORM Plugin to sync the data against a RESTful API." ,
5
5
"main" : " dist/index.js" ,
6
6
"scripts" : {
29
29
}
30
30
},
31
31
"dependencies" : {
32
- "@vuex-orm/core" : " ^0.26.2 " ,
32
+ "@vuex-orm/core" : " ^0.31.6 " ,
33
33
"axios" : " ^0.18.0" ,
34
34
"lodash" : " ^4.17.11"
35
35
},
Original file line number Diff line number Diff line change @@ -61,4 +61,15 @@ export default class Action {
61
61
if ( config . query ) endpoint += `?${ Object . keys ( config . query ) . map ( k => `${ encodeURIComponent ( k ) } =${ encodeURIComponent ( config . query [ k ] ) } ` ) . join ( '&' ) } ` ;
62
62
return endpoint ;
63
63
}
64
+
65
+ /**
66
+ * Get appropriate methods
67
+ * @param {string } type
68
+ * @param {object } model
69
+ * @param {string } defaultMethod
70
+ */
71
+ static getMethod ( type , model , defaultMethod ) {
72
+ const customMethod = model . methodConf . methods [ type ] . http . method ;
73
+ return ( customMethod ) ? customMethod : defaultMethod ;
74
+ }
64
75
}
Original file line number Diff line number Diff line change @@ -17,7 +17,8 @@ export default class Create extends Action {
17
17
const model = context . getModelFromState ( state ) ;
18
18
const endpoint = Action . transformParams ( '$create' , model , params ) ;
19
19
const axios = new Axios ( model . methodConf . http ) ;
20
- const request = axios . post ( endpoint , params . data ) ;
20
+ const method = Action . getMethod ( '$create' , model , 'post' ) ;
21
+ const request = axios [ method ] ( endpoint , params . data ) ;
21
22
22
23
this . onRequest ( commit ) ;
23
24
request
Original file line number Diff line number Diff line change @@ -13,7 +13,8 @@ export default class Delete extends Action {
13
13
const model = context . getModelFromState ( state ) ;
14
14
const endpoint = Action . transformParams ( '$delete' , model , params ) ;
15
15
const axios = new Axios ( model . methodConf . http ) ;
16
- const request = axios . delete ( endpoint ) ;
16
+ const method = Action . getMethod ( '$delete' , model , 'delete' ) ;
17
+ const request = axios [ method ] ( endpoint ) ;
17
18
18
19
this . onRequest ( model , params ) ;
19
20
request
Original file line number Diff line number Diff line change @@ -13,7 +13,8 @@ export default class Fetch extends Action {
13
13
const model = context . getModelFromState ( state ) ;
14
14
const endpoint = Action . transformParams ( '$fetch' , model , params ) ;
15
15
const axios = new Axios ( model . methodConf . http ) ;
16
- const request = axios . get ( endpoint ) ;
16
+ const method = Action . getMethod ( '$fetch' , model , 'get' ) ;
17
+ const request = axios [ method ] ( endpoint ) ;
17
18
18
19
this . onRequest ( commit ) ;
19
20
request
Original file line number Diff line number Diff line change @@ -13,7 +13,8 @@ export default class Get extends Action {
13
13
const model = context . getModelFromState ( state ) ;
14
14
const endpoint = Action . transformParams ( '$get' , model , params ) ;
15
15
const axios = new Axios ( model . methodConf . http ) ;
16
- const request = axios . get ( endpoint ) ;
16
+ const method = Action . getMethod ( '$get' , model , 'get' ) ;
17
+ const request = axios [ method ] ( endpoint ) ;
17
18
18
19
this . onRequest ( commit ) ;
19
20
request
Original file line number Diff line number Diff line change @@ -18,7 +18,8 @@ export default class Update extends Action {
18
18
const model = context . getModelFromState ( state ) ;
19
19
const endpoint = Action . transformParams ( '$update' , model , params ) ;
20
20
const axios = new Axios ( model . methodConf . http ) ;
21
- const request = axios . put ( endpoint , params . data ) ;
21
+ const method = Action . getMethod ( '$update' , model , 'put' ) ;
22
+ const request = axios [ method ] ( endpoint , params . data ) ;
22
23
23
24
this . onRequest ( model , params ) ;
24
25
request
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 = http . axios || 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 , this . instance ) ,
@@ -16,6 +13,15 @@ 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
19
+ "function" ;
20
+ const tokenStr = isFunction ? token ( ) : token ;
21
+
22
+ this . instance . defaults . headers . common [ 'Authorization' ] = `Bearer ${ tokenStr } ` ;
23
+ }
24
+
19
25
/**
20
26
* Head Request
21
27
* @param {string } url
Original file line number Diff line number Diff line change @@ -136,24 +136,18 @@ export const AxiosRequestConfig = {
136
136
* @param {object } error
137
137
*/
138
138
onError ( error ) {
139
- switch ( error . response . status ) {
140
- case 401 :
141
- this . onUnauthorised ( error ) ;
142
- break ;
143
- case 404 :
144
- this . onNotFound ( error ) ;
145
- break ;
146
- case 422 :
147
- this . onValidationError ( error ) ;
148
- break ;
149
- case 500 :
150
- this . onServerError ( error ) ;
151
- break ;
152
- default :
153
- this . onGenericError ( error ) ;
154
- break ;
139
+ const { response } = error ;
140
+ const errorTypes = {
141
+ 401 : this . onUnauthorised ,
142
+ 404 : this . onNotFound ,
143
+ 422 : this . onValidationError ,
144
+ 500 : this . onServerError
145
+ }
146
+ if ( response && response . status in errorTypes ) {
147
+ errorTypes [ response . status ] ( error ) ;
148
+ } else {
149
+ this . onGenericError ( error ) ;
155
150
}
156
-
157
151
return Promise . reject ( error ) ;
158
152
} ,
159
153
} ;
You can’t perform that action at this time.
0 commit comments