Skip to content

Commit c2f86c0

Browse files
committed
Update for interceptors to be used without axiosOptions set
Also refactor for conciseness
1 parent 8736aab commit c2f86c0

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

index.js

Lines changed: 28 additions & 24 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) {
@@ -20,30 +47,7 @@ const VueRest = {
2047
if (options && options.axiosOptions) {
2148
api = axios.create(options.axiosOptions);
2249
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-
}
50+
addInterceptors(options.interceptors, api);
4751
}
4852
if (options.axiosOptions.localStorageAuthorization) {
4953
const localStorageAuthorization =

0 commit comments

Comments
 (0)