@@ -21,54 +21,41 @@ function bindInterceptors(client, getState, middlewareInterceptors = {}, clientI
2121export const multiClientMiddleware = ( clients , customMiddlewareOptions ) => {
2222 const middlewareOptions = { ...defaultOptions , ...customMiddlewareOptions } ;
2323 const setupedClients = { } ;
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 ) ;
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, dispatch, action } ,
36+ middlewareOptions . interceptors , clients [ clientName ] . options . interceptors ) ;
3837 }
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- } ;
38+ setupedClients [ clientName ] = {
39+ client : clients [ clientName ] . client ,
40+ options : clientOptions
41+ } ;
42+ }
43+ const setupedClient = setupedClients [ clientName ] ;
44+ const actionOptions = { ...setupedClient . options , ...setupedClient . options . getRequestOptions ( action ) } ;
45+ const [ REQUEST ] = getActionTypes ( action , actionOptions ) ;
46+ next ( { ...action , type : REQUEST } ) ;
47+ return setupedClient . client . request ( actionOptions . getRequestConfig ( action ) )
48+ . then (
49+ ( response ) => {
50+ const newAction = actionOptions . onSuccess ( { action, next, response, getState, dispatch } , actionOptions ) ;
51+ actionOptions . onComplete ( { action : newAction , next, getState, dispatch } , actionOptions ) ;
52+ return newAction ;
53+ } ,
54+ ( error ) => {
55+ const newAction = actionOptions . onError ( { action, next, error, getState, dispatch } , actionOptions ) ;
56+ actionOptions . onComplete ( { action : newAction , next, getState, dispatch } , actionOptions ) ;
57+ return actionOptions . returnRejectedPromiseOnError ? Promise . reject ( newAction ) : newAction ;
58+ } ) ;
7259 } ;
7360} ;
7461
0 commit comments