Skip to content

Commit 1792259

Browse files
authored
Merge branch 'master' into master
2 parents 669e93a + 94ecbe9 commit 1792259

File tree

11 files changed

+47
-6386
lines changed

11 files changed

+47
-6386
lines changed

dist/index.js

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@vuex-orm/plugin-axios",
3-
"version": "0.2.3",
3+
"version": "0.5.0",
44
"description": "Vuex-ORM Plugin to sync the data against a RESTful API.",
55
"main": "dist/index.js",
66
"scripts": {
@@ -29,7 +29,7 @@
2929
}
3030
},
3131
"dependencies": {
32-
"@vuex-orm/core": "^0.26.2",
32+
"@vuex-orm/core": "^0.31.6",
3333
"axios": "^0.18.0",
3434
"lodash": "^4.17.11"
3535
},

src/actions/Action.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,15 @@ export default class Action {
6161
if (config.query) endpoint += `?${Object.keys(config.query).map(k => `${encodeURIComponent(k)}=${encodeURIComponent(config.query[k])}`).join('&')}`;
6262
return endpoint;
6363
}
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+
}
6475
}

src/actions/Create.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ export default class Create extends Action {
1717
const model = context.getModelFromState(state);
1818
const endpoint = Action.transformParams('$create', model, params);
1919
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);
2122

2223
this.onRequest(commit);
2324
request

src/actions/Delete.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ export default class Delete extends Action {
1313
const model = context.getModelFromState(state);
1414
const endpoint = Action.transformParams('$delete', model, params);
1515
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);
1718

1819
this.onRequest(model, params);
1920
request

src/actions/Fetch.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ export default class Fetch extends Action {
1313
const model = context.getModelFromState(state);
1414
const endpoint = Action.transformParams('$fetch', model, params);
1515
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);
1718

1819
this.onRequest(commit);
1920
request

src/actions/Get.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ export default class Get extends Action {
1313
const model = context.getModelFromState(state);
1414
const endpoint = Action.transformParams('$get', model, params);
1515
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);
1718

1819
this.onRequest(commit);
1920
request

src/actions/Update.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ export default class Update extends Action {
1818
const model = context.getModelFromState(state);
1919
const endpoint = Action.transformParams('$update', model, params);
2020
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);
2223

2324
this.onRequest(model, params);
2425
request

src/orm/axios.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ import axios from 'axios';
33
export default class Axios {
44
constructor(http) {
55
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);
107

118
this.instance.interceptors.response.use(
129
response => http.onResponse(response, this.instance),
@@ -16,6 +13,15 @@ export default class Axios {
1613
return this.instance;
1714
}
1815

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+
1925
/**
2026
* Head Request
2127
* @param {string} url

src/support/interfaces.js

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -136,24 +136,18 @@ export const AxiosRequestConfig = {
136136
* @param {object} error
137137
*/
138138
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);
155150
}
156-
157151
return Promise.reject(error);
158152
},
159153
};

0 commit comments

Comments
 (0)