@@ -6,7 +6,7 @@ function addInterceptor(target, candidate, getState) {
66 const successInterceptor = typeof candidate === 'function' ? candidate : candidate . success ;
77 const errorInterceptor = candidate && candidate . error ;
88 target . use ( successInterceptor && successInterceptor . bind ( null , getState ) ,
9- errorInterceptor && errorInterceptor . bind ( null , getState ) ) ;
9+ errorInterceptor && errorInterceptor . bind ( null , getState ) ) ;
1010}
1111
1212function bindInterceptors ( client , getState , middlewareInterceptors = { } , clientInterceptors = { } ) {
@@ -21,40 +21,54 @@ function bindInterceptors(client, getState, middlewareInterceptors = {}, clientI
2121export const multiClientMiddleware = ( clients , customMiddlewareOptions ) => {
2222 const middlewareOptions = { ...defaultOptions , ...customMiddlewareOptions } ;
2323 const setupedClients = { } ;
24- return ( { getState, dispatch } ) => next => action => {
25- if ( ! middlewareOptions . isAxiosRequest ( action ) ) {
26- return next ( action ) ;
27- }
28- const clientName = middlewareOptions . getClientName ( action ) || middlewareOptions . defaultClientName ;
29- if ( ! clients [ clientName ] ) {
30- throw new Error ( `Client with name "${ clientName } " has not been defined in middleware` ) ;
31- }
32- if ( ! setupedClients [ clientName ] ) {
33- const clientOptions = { ...middlewareOptions , ...clients [ clientName ] . options } ;
34- if ( clientOptions . interceptors ) {
35- bindInterceptors ( clients [ clientName ] . client , getState , middlewareOptions . interceptors , clients [ clientName ] . options . interceptors ) ;
24+ return function ( { getState, dispatch } ) {
25+ const enhancedGetState = function ( ) {
26+ console . log ( `
27+ Warning, getState as function in interceptor will be removed in version 2 of middleware.
28+ Stop: interceptor(getState,config) { ... }
29+ Do: interceptor({getState}, config) { ... }
30+ ` ) ;
31+ return getState ( ) ;
32+ } ;
33+ enhancedGetState . getState = getState ;
34+ enhancedGetState . dispatch = dispatch ;
35+ return next => action => {
36+ if ( ! middlewareOptions . isAxiosRequest ( action ) ) {
37+ return next ( action ) ;
3638 }
37- setupedClients [ clientName ] = {
38- client : clients [ clientName ] . client ,
39- options : clientOptions
40- } ;
41- }
42- const setupedClient = setupedClients [ clientName ] ;
43- const actionOptions = { ...setupedClient . options , ...setupedClient . options . getRequestOptions ( action ) } ;
44- const [ REQUEST ] = getActionTypes ( action , actionOptions ) ;
45- next ( { ...action , type : REQUEST } ) ;
46- return setupedClient . client . request ( actionOptions . getRequestConfig ( action ) )
47- . then (
48- ( response ) => {
49- const newAction = actionOptions . onSuccess ( { action, next, response, getState, dispatch } , actionOptions ) ;
50- actionOptions . onComplete ( { action : newAction , next, getState, dispatch } , actionOptions ) ;
51- return newAction ;
52- } ,
53- ( error ) => {
54- const newAction = actionOptions . onError ( { action, next, error, getState, dispatch } , actionOptions ) ;
55- actionOptions . onComplete ( { action : newAction , next, getState, dispatch } , actionOptions ) ;
56- return actionOptions . returnRejectedPromiseOnError ? Promise . reject ( newAction ) : newAction ;
57- } ) ;
39+ const clientName = middlewareOptions . getClientName ( action ) || middlewareOptions . defaultClientName ;
40+ if ( ! clients [ clientName ] ) {
41+ throw new Error ( `Client with name "${ clientName } " has not been defined in middleware` ) ;
42+ }
43+ if ( ! setupedClients [ clientName ] ) {
44+ const clientOptions = { ...middlewareOptions , ...clients [ clientName ] . options } ;
45+ if ( clientOptions . interceptors ) {
46+ enhancedGetState . action = action ;
47+ bindInterceptors ( clients [ clientName ] . client , enhancedGetState ,
48+ middlewareOptions . interceptors , clients [ clientName ] . options . interceptors ) ;
49+ }
50+ setupedClients [ clientName ] = {
51+ client : clients [ clientName ] . client ,
52+ options : clientOptions
53+ } ;
54+ }
55+ const setupedClient = setupedClients [ clientName ] ;
56+ const actionOptions = { ...setupedClient . options , ...setupedClient . options . getRequestOptions ( action ) } ;
57+ const [ REQUEST ] = getActionTypes ( action , actionOptions ) ;
58+ next ( { ...action , type : REQUEST } ) ;
59+ return setupedClient . client . request ( actionOptions . getRequestConfig ( action ) )
60+ . then (
61+ ( response ) => {
62+ const newAction = actionOptions . onSuccess ( { action, next, response, getState, dispatch } , actionOptions ) ;
63+ actionOptions . onComplete ( { action : newAction , next, getState, dispatch } , actionOptions ) ;
64+ return newAction ;
65+ } ,
66+ ( error ) => {
67+ const newAction = actionOptions . onError ( { action, next, error, getState, dispatch } , actionOptions ) ;
68+ actionOptions . onComplete ( { action : newAction , next, getState, dispatch } , actionOptions ) ;
69+ return actionOptions . returnRejectedPromiseOnError ? Promise . reject ( newAction ) : newAction ;
70+ } ) ;
71+ } ;
5872 } ;
5973} ;
6074
0 commit comments