Skip to content

Commit 8736aab

Browse files
committed
Add interceptor support to plugin instantiation
1 parent 992062e commit 8736aab

File tree

3 files changed

+75
-7
lines changed

3 files changed

+75
-7
lines changed

docs/started.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ $ npm install vue-rest
1616

1717
To add to your project, import it and the use `Vue.use(VueRest)` to properly use it.
1818

19-
> Optionally you can add `axiosOptions` that is responsible for any options related to [axios]('https://github.com/mzabriskie/axios%29/')
19+
> Optionally you can add `axiosOptions` that is responsible for any options related to [axios]('https://github.com/mzabriskie/axios%29/').
20+
21+
> You can also use `interceptors` to add interceptors alongside your config.
2022
2123
```js
2224
...
@@ -26,6 +28,16 @@ import VueRest from 'vue-rest';
2628
Vue.use(VueRest, {
2729
axiosOptions: {
2830
baseURL: 'http://localhost/api/',
31+
},
32+
interceptors: {
33+
responses: [
34+
[fulfilled, rejected],
35+
{
36+
fulfilled,
37+
rejected
38+
}
39+
],
40+
requests: []
2941
}
3042
});
3143
```
@@ -52,4 +64,4 @@ export default {
5264
mixins: [ApiList]
5365
...
5466
}
55-
```
67+
```

index.js

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,49 @@ const VueRest = {
1919
let api = null;
2020
if (options && options.axiosOptions) {
2121
api = axios.create(options.axiosOptions);
22+
if (options.interceptors) {
23+
const {
24+
requests: requestInterceptors,
25+
responses: responseInterceptors,
26+
} = options.interceptors;
27+
if (requestInterceptors) {
28+
requestInterceptors.forEach((interceptor) => {
29+
if (Array.isArray(interceptor)) {
30+
api.interceptors.request.use(...interceptor);
31+
} else {
32+
const { fulfilled, rejected } = interceptor;
33+
api.interceptors.request.use(fulfilled, rejected);
34+
}
35+
});
36+
}
37+
if (responseInterceptors) {
38+
responseInterceptors.forEach((interceptor) => {
39+
if (Array.isArray(interceptor)) {
40+
api.interceptors.response.use(...interceptor);
41+
} else {
42+
const { fulfilled, rejected } = interceptor;
43+
api.interceptors.response.use(fulfilled, rejected);
44+
}
45+
});
46+
}
47+
}
2248
if (options.axiosOptions.localStorageAuthorization) {
23-
const localStorageAuthorization = options.axiosOptions.localStorageAuthorization;
49+
const localStorageAuthorization =
50+
options.axiosOptions.localStorageAuthorization;
2451
api.interceptors.request.use((config) => {
25-
const token = localStorage.getItem(localStorageAuthorization.tokenItem);
52+
const token = localStorage.getItem(
53+
localStorageAuthorization.tokenItem,
54+
);
2655
const prefix = localStorageAuthorization.prefix;
2756
if (!localStorageAuthorization.tokenItem || !prefix) {
28-
console.error('[ERR - VueRest]: Miss configuration at localStorageAuthorization.');
57+
console.error(
58+
'[ERR - VueRest]: Miss configuration at localStorageAuthorization.',
59+
);
2960
}
3061
if (token) {
31-
Object.assign(config.headers, { Authorization: `${prefix} ${token}` });
62+
Object.assign(config.headers, {
63+
Authorization: `${prefix} ${token}`,
64+
});
3265
}
3366
return config;
3467
});

test/unit/mixins/Install.spec.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,37 @@ import Vue from 'vue';
22
import VueRest from '../../../index.js';
33

44
describe('Testing install function', () => {
5+
const interceptor1 = [config => config, error => Promise.reject(error)];
6+
const interceptor2 = [request => request, error => Promise.reject(error)];
7+
const interceptor3 = {
8+
fulfilled: config => config,
9+
rejected: error => Promise.reject(error),
10+
};
11+
const interceptor1AsObj = {
12+
fulfilled: interceptor1[0],
13+
rejected: interceptor1[1],
14+
};
515

616
Vue.use(VueRest, {
717
axiosOptions: {
818
baseURL: 'http://localhost:8000',
919
},
20+
interceptors: {
21+
requests: [interceptor1, interceptor2, interceptor3],
22+
responses: [interceptor3],
23+
},
1024
});
1125

1226
test('Should apply axios settings when axiosOptions is set', () => {
1327
expect(Vue.api.defaults.baseURL).toBe('http://localhost:8000');
14-
});
28+
});
29+
30+
test('can receive interceptors from axiosOptions', () => {
31+
expect(Vue.api.interceptors.request.handlers).toEqual(
32+
expect.arrayContaining([interceptor1AsObj, interceptor3]),
33+
);
34+
expect(Vue.api.interceptors.response.handlers).toEqual(
35+
expect.arrayContaining([interceptor3]),
36+
);
37+
});
1538
});

0 commit comments

Comments
 (0)