Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow set token with a function and fix a error #8

Merged
merged 3 commits into from
Mar 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@

{
"presets": ["@babel/preset-env"],
"plugins": ["transform-runtime"]
}
"presets": [
"@babel/preset-env"
],
"plugins": [
"@babel/transform-runtime"
]
}
6 changes: 3 additions & 3 deletions dist/index.js

Large diffs are not rendered by default.

14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,18 @@
}
},
"dependencies": {
"@babel/preset-env": "^7.0.0",
"@vuex-orm/core": "^0.26.2",
"@vuex-orm/core": "^0.30.0",
"axios": "^0.18.0",
"lodash": "^4.17.11"
},
"devDependencies": {
"@babel/core": "^7.0.0",
"@babel/core": "^7.1.6",
"babel-core": "7.0.0-bridge.0",
"babel-jest": "^23.4.2",
"babel-loader": "^8.0.0",
"@babel/runtime": "^7.1.5",
"@babel/plugin-transform-runtime": "^7.1.0",
"@babel/preset-env": "^7.1.6",
"babel-jest": "^23.6.0",
"babel-loader": "^8.0.4",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.7.0",
"eslint": "^5.4.0",
Expand All @@ -49,4 +51,4 @@
"webpack": "^4.17.1",
"webpack-cli": "^3.1.0"
}
}
}
13 changes: 9 additions & 4 deletions src/orm/axios.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import axios from 'axios';
export default class Axios {
constructor(http) {
this.instance = axios.create(http);

if(http.access_token) {
this.instance.defaults.headers.common['Authorization'] = `Bearer ${http.access_token}`;
}
this.setAuthentication(http.access_token);

this.instance.interceptors.response.use(
response => http.onResponse(response),
Expand All @@ -16,6 +13,14 @@ export default class Axios {
return this.instance;
}

setAuthentication(token) {
if (!token) return;
const isFunction = typeof token === "function";
const tokenStr = isFunction ? token() : token;

this.instance.defaults.headers.common['Authorization'] = `Bearer ${tokenStr}`;
}

/**
* Head Request
* @param {string} url
Expand Down
28 changes: 11 additions & 17 deletions src/support/interfaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,24 +130,18 @@ export const AxiosRequestConfig = {
* @param {object} error
*/
onError(error) {
switch (error.response.status) {
case 401:
this.onUnauthorised(error);
break;
case 404:
this.onNotFound(error);
break;
case 422:
this.onValidationError(error);
break;
case 500:
this.onServerError(error);
break;
default:
this.onGenericError(error);
break;
const { response } = error;
const errorTypes = {
401: this.onUnauthorised,
404: this.onNotFound,
422: this.onValidationError,
500: this.onServerError
}
if (response && response.status in errorTypes) {
errorTypes[response.status](error);
} else {
this.onGenericError(error);
}

return Promise.reject(error);
},
};
Expand Down
Loading