Skip to content

Commit 6e91564

Browse files
authored
Merge pull request #8 from Akhail/feature/function-token
Allow set token with a function and fix a error
2 parents bd2caa8 + 2e1c39e commit 6e91564

File tree

6 files changed

+1273
-450
lines changed

6 files changed

+1273
-450
lines changed

.babelrc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
21
{
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+
}

dist/index.js

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

package.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,18 @@
2929
}
3030
},
3131
"dependencies": {
32-
"@babel/preset-env": "^7.0.0",
33-
"@vuex-orm/core": "^0.26.2",
32+
"@vuex-orm/core": "^0.30.0",
3433
"axios": "^0.18.0",
3534
"lodash": "^4.17.11"
3635
},
3736
"devDependencies": {
38-
"@babel/core": "^7.0.0",
37+
"@babel/core": "^7.1.6",
3938
"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",
4244
"babel-plugin-transform-runtime": "^6.23.0",
4345
"babel-preset-env": "^1.7.0",
4446
"eslint": "^5.4.0",
@@ -49,4 +51,4 @@
4951
"webpack": "^4.17.1",
5052
"webpack-cli": "^3.1.0"
5153
}
52-
}
54+
}

src/orm/axios.js

Lines changed: 9 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 = 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),
@@ -16,6 +13,14 @@ export default class Axios {
1613
return this.instance;
1714
}
1815

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+
1924
/**
2025
* Head Request
2126
* @param {string} url

src/support/interfaces.js

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -130,24 +130,18 @@ export const AxiosRequestConfig = {
130130
* @param {object} error
131131
*/
132132
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);
149144
}
150-
151145
return Promise.reject(error);
152146
},
153147
};

0 commit comments

Comments
 (0)