Skip to content

Commit 8bb3589

Browse files
authored
Merge pull request #45 from SebastianSmolorz/feature/request-interceptor
Feat: Axios request interceptor + Fix: setAuthentication error on string
2 parents d793bf4 + 5bff6cd commit 8bb3589

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

src/orm/axios.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ export default class Axios {
55
this.instance = http.axios || axios.create(http);
66
this.setAuthentication(http.access_token);
77

8+
this.instance.interceptors.request.use(
9+
config => http.onRequest(config, this.instance),
10+
error => http.onError(error, this.instance),
11+
);
12+
813
this.instance.interceptors.response.use(
914
response => http.onResponse(response, this.instance),
1015
error => http.onError(error, this.instance),
@@ -15,8 +20,7 @@ export default class Axios {
1520

1621
setAuthentication(token) {
1722
if (!token) return;
18-
const isFunction = typeof token
19-
"function";
23+
const isFunction = typeof token === 'function';
2024
const tokenStr = isFunction ? token() : token;
2125

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

src/support/interfaces.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,21 @@ export const AxiosRequestConfig = {
8383
*/
8484
proxy: {},
8585

86+
/**
87+
* Default onRequest
88+
* @param {object} config
89+
* @param {Axios} Axios instance
90+
*/
91+
onRequest(config, axios) {
92+
return config;
93+
},
94+
8695
/**
8796
* Default on Response
8897
* @param {object} response
98+
* @param {Axios} axios instance
8999
*/
90-
onResponse(response) {
100+
onResponse(response, axios) {
91101
return response.data;
92102
},
93103

@@ -134,8 +144,9 @@ export const AxiosRequestConfig = {
134144
/**
135145
* Default on Error
136146
* @param {object} error
147+
* @param {Axios} axios instance
137148
*/
138-
onError(error) {
149+
onError(error, axios) {
139150
const { response } = error;
140151
const errorTypes = {
141152
401: this.onUnauthorised,

0 commit comments

Comments
 (0)