Skip to content

Commit

Permalink
Merge pull request #8 from Akhail/feature/function-token
Browse files Browse the repository at this point in the history
Allow set token with a function and fix a error
  • Loading branch information
KaniRobinson authored Mar 15, 2019
2 parents bd2caa8 + 2e1c39e commit 6e91564
Show file tree
Hide file tree
Showing 6 changed files with 1,273 additions and 450 deletions.
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

0 comments on commit 6e91564

Please sign in to comment.