|
1 | 1 | import * as defaultOptions from './defaults'; |
2 | 2 | import { getActionTypes } from './getActionTypes'; |
3 | 3 |
|
4 | | -function addInterceptor(target, candidate, getState) { |
| 4 | +function addInterceptor(target, candidate, injectedParameters) { |
5 | 5 | if (!candidate) return; |
6 | 6 | const successInterceptor = typeof candidate === 'function' ? candidate : candidate.success; |
7 | 7 | const errorInterceptor = candidate && candidate.error; |
8 | | - target.use(successInterceptor && successInterceptor.bind(null, getState), |
9 | | - errorInterceptor && errorInterceptor.bind(null, getState)); |
| 8 | + target.use(successInterceptor && successInterceptor.bind(null, injectedParameters), |
| 9 | + errorInterceptor && errorInterceptor.bind(null, injectedParameters)); |
10 | 10 | } |
11 | 11 |
|
12 | | -function bindInterceptors(client, getState, middlewareInterceptors = {}, clientInterceptors = {}) { |
| 12 | +function bindInterceptors(client, injectedParameters, middlewareInterceptors = {}, clientInterceptors = {}) { |
13 | 13 | [...middlewareInterceptors.request || [], ...clientInterceptors.request || []].forEach((interceptor) => { |
14 | | - addInterceptor(client.interceptors.request, interceptor, getState); |
| 14 | + addInterceptor(client.interceptors.request, interceptor, injectedParameters); |
15 | 15 | }); |
16 | 16 | [...middlewareInterceptors.response || [], ...clientInterceptors.response || []].forEach((interceptor) => { |
17 | | - addInterceptor(client.interceptors.response, interceptor, getState); |
| 17 | + addInterceptor(client.interceptors.response, interceptor, injectedParameters); |
18 | 18 | }); |
19 | 19 | } |
20 | 20 |
|
21 | 21 | export const multiClientMiddleware = (clients, customMiddlewareOptions) => { |
22 | 22 | const middlewareOptions = { ...defaultOptions, ...customMiddlewareOptions }; |
23 | 23 | const setupedClients = {}; |
| 24 | + let storedAction; |
24 | 25 | return ({ getState, dispatch }) => next => action => { |
25 | 26 | if (!middlewareOptions.isAxiosRequest(action)) { |
26 | 27 | return next(action); |
27 | 28 | } |
| 29 | + storedAction = action; |
28 | 30 | const clientName = middlewareOptions.getClientName(action) || middlewareOptions.defaultClientName; |
29 | 31 | if (!clients[clientName]) { |
30 | 32 | throw new Error(`Client with name "${clientName}" has not been defined in middleware`); |
31 | 33 | } |
32 | 34 | if (!setupedClients[clientName]) { |
33 | 35 | const clientOptions = { ...middlewareOptions, ...clients[clientName].options }; |
34 | 36 | if (clientOptions.interceptors) { |
35 | | - bindInterceptors(clients[clientName].client, { getState, dispatch, action }, |
| 37 | + const getAction = () => storedAction; |
| 38 | + const injectToInterceptor = { getState, dispatch, action, getAction }; |
| 39 | + bindInterceptors(clients[clientName].client, injectToInterceptor, |
36 | 40 | middlewareOptions.interceptors, clients[clientName].options.interceptors); |
37 | 41 | } |
38 | 42 | setupedClients[clientName] = { |
|
0 commit comments