Skip to content

Commit 369c0e1

Browse files
authored
Merge pull request #9 from tarikhamilton/#8-enable-interceptors-on-vue.$api
Add interceptor support to plugin instantiation
2 parents 87af2e4 + 663fd8b commit 369c0e1

File tree

3 files changed

+79
-7
lines changed

3 files changed

+79
-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: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,33 @@ import axios from 'axios';
88
import ApiForm from './mixins/ApiForm';
99
import ApiList from './mixins/ApiList';
1010

11+
function addInterceptors(interceptors, api) {
12+
const {
13+
requests: requestInterceptors,
14+
responses: responseInterceptors,
15+
} = interceptors;
16+
if (requestInterceptors) {
17+
requestInterceptors.forEach((interceptor) => {
18+
if (Array.isArray(interceptor)) {
19+
api.interceptors.request.use(...interceptor);
20+
} else {
21+
const { fulfilled, rejected } = interceptor;
22+
api.interceptors.request.use(fulfilled, rejected);
23+
}
24+
});
25+
}
26+
if (responseInterceptors) {
27+
responseInterceptors.forEach((interceptor) => {
28+
if (Array.isArray(interceptor)) {
29+
api.interceptors.response.use(...interceptor);
30+
} else {
31+
const { fulfilled, rejected } = interceptor;
32+
api.interceptors.response.use(fulfilled, rejected);
33+
}
34+
});
35+
}
36+
}
37+
1138
const VueRest = {
1239
install(Vue, options) {
1340
if (Vue.vueRestInstalled) {
@@ -19,16 +46,26 @@ const VueRest = {
1946
let api = null;
2047
if (options && options.axiosOptions) {
2148
api = axios.create(options.axiosOptions);
49+
if (options.interceptors) {
50+
addInterceptors(options.interceptors, api);
51+
}
2252
if (options.axiosOptions.localStorageAuthorization) {
23-
const localStorageAuthorization = options.axiosOptions.localStorageAuthorization;
53+
const localStorageAuthorization =
54+
options.axiosOptions.localStorageAuthorization;
2455
api.interceptors.request.use((config) => {
25-
const token = localStorage.getItem(localStorageAuthorization.tokenItem);
56+
const token = localStorage.getItem(
57+
localStorageAuthorization.tokenItem,
58+
);
2659
const prefix = localStorageAuthorization.prefix;
2760
if (!localStorageAuthorization.tokenItem || !prefix) {
28-
console.error('[ERR - VueRest]: Miss configuration at localStorageAuthorization.');
61+
console.error(
62+
'[ERR - VueRest]: Miss configuration at localStorageAuthorization.',
63+
);
2964
}
3065
if (token) {
31-
Object.assign(config.headers, { Authorization: `${prefix} ${token}` });
66+
Object.assign(config.headers, {
67+
Authorization: `${prefix} ${token}`,
68+
});
3269
}
3370
return config;
3471
});

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)