@@ -8,6 +8,33 @@ import axios from 'axios';
88import ApiForm from './mixins/ApiForm' ;
99import 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+
1138const 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 } ) ;
0 commit comments