Skip to content

Commit d9dba80

Browse files
authored
Merge pull request #38 from tlenclos/feature/interceptor-get-source-action
Thanks for the hard work @tlenclos
2 parents c114d87 + c2bcc3e commit d9dba80

File tree

4 files changed

+71
-32
lines changed

4 files changed

+71
-32
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# CHANGELOG
22

3+
## 3.1.3 ( March 15, 2017 )
4+
- in interceptors, you can use `getAction` to receive action which triggered interceptors, for now, bug with `action` will not be removed, but it'll be removed in 4.0 as it is breaking change
5+
36
## 3.1.2 ( Feb 20, 2017 )
47
- exclude our .babelrc from our npm build.
58

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ getRequestConfig | function | return con
242242
getClientName | function | returns `action.payload.client` OR `'default'` | `M` `C` `A` | attempts to resolve used client name or use defaultClientName
243243
defaultClientName | every possible object key type | `'default'` | `M` | key which define client used if `getClienName` returned false value
244244
getRequestOptions | function | return `action.payload.options` | `M` `C` | returns options object from action to override some values
245-
interceptors | object `{request: [], response: []}` | | `M` `C` | You can pass axios request and response interceptors. Take care, first argument of interceptor is different from default axios interceptor, first received argument is object with `getState`, `dispatch` and `action` keys
245+
interceptors | object `{request: [], response: []}` | | `M` `C` | You can pass axios request and response interceptors. Take care, first argument of interceptor is different from default axios interceptor, first received argument is object with `getState`, `dispatch` and `getAction` keys
246246

247247
## License
248248

dist/bundle.js

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,41 @@
1111
return /******/ (function(modules) { // webpackBootstrap
1212
/******/ // The module cache
1313
/******/ var installedModules = {};
14-
14+
/******/
1515
/******/ // The require function
1616
/******/ function __webpack_require__(moduleId) {
17-
17+
/******/
1818
/******/ // Check if module is in cache
1919
/******/ if(installedModules[moduleId])
2020
/******/ return installedModules[moduleId].exports;
21-
21+
/******/
2222
/******/ // Create a new module (and put it into the cache)
2323
/******/ var module = installedModules[moduleId] = {
2424
/******/ i: moduleId,
2525
/******/ l: false,
2626
/******/ exports: {}
2727
/******/ };
28-
28+
/******/
2929
/******/ // Execute the module function
3030
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
31-
31+
/******/
3232
/******/ // Flag the module as loaded
3333
/******/ module.l = true;
34-
34+
/******/
3535
/******/ // Return the exports of the module
3636
/******/ return module.exports;
3737
/******/ }
38-
39-
38+
/******/
39+
/******/
4040
/******/ // expose the modules object (__webpack_modules__)
4141
/******/ __webpack_require__.m = modules;
42-
42+
/******/
4343
/******/ // expose the module cache
4444
/******/ __webpack_require__.c = installedModules;
45-
45+
/******/
4646
/******/ // identity function for calling harmony imports with the correct context
4747
/******/ __webpack_require__.i = function(value) { return value; };
48-
48+
/******/
4949
/******/ // define getter function for harmony exports
5050
/******/ __webpack_require__.d = function(exports, name, getter) {
5151
/******/ if(!__webpack_require__.o(exports, name)) {
@@ -56,7 +56,7 @@ return /******/ (function(modules) { // webpackBootstrap
5656
/******/ });
5757
/******/ }
5858
/******/ };
59-
59+
/******/
6060
/******/ // getDefaultExport function for compatibility with non-harmony modules
6161
/******/ __webpack_require__.n = function(module) {
6262
/******/ var getter = module && module.__esModule ?
@@ -65,13 +65,13 @@ return /******/ (function(modules) { // webpackBootstrap
6565
/******/ __webpack_require__.d(getter, 'a', getter);
6666
/******/ return getter;
6767
/******/ };
68-
68+
/******/
6969
/******/ // Object.prototype.hasOwnProperty.call
7070
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
71-
71+
/******/
7272
/******/ // __webpack_public_path__
7373
/******/ __webpack_require__.p = "";
74-
74+
/******/
7575
/******/ // Load entry module and return exports
7676
/******/ return __webpack_require__(__webpack_require__.s = 4);
7777
/******/ })
@@ -137,28 +137,33 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
137137

138138
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
139139

140-
function addInterceptor(target, candidate, getState) {
140+
function addInterceptor(target, candidate, injectedParameters) {
141141
if (!candidate) return;
142142
var successInterceptor = typeof candidate === 'function' ? candidate : candidate.success;
143143
var errorInterceptor = candidate && candidate.error;
144-
target.use(successInterceptor && successInterceptor.bind(null, getState), errorInterceptor && errorInterceptor.bind(null, getState));
144+
target.use(successInterceptor && successInterceptor.bind(null, injectedParameters), errorInterceptor && errorInterceptor.bind(null, injectedParameters));
145145
}
146146

147-
function bindInterceptors(client, getState) {
147+
function bindInterceptors(client, injectedParameters) {
148148
var middlewareInterceptors = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
149149
var clientInterceptors = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
150150

151151
[].concat(_toConsumableArray(middlewareInterceptors.request || []), _toConsumableArray(clientInterceptors.request || [])).forEach(function (interceptor) {
152-
addInterceptor(client.interceptors.request, interceptor, getState);
152+
addInterceptor(client.interceptors.request, interceptor, injectedParameters);
153153
});
154154
[].concat(_toConsumableArray(middlewareInterceptors.response || []), _toConsumableArray(clientInterceptors.response || [])).forEach(function (interceptor) {
155-
addInterceptor(client.interceptors.response, interceptor, getState);
155+
addInterceptor(client.interceptors.response, interceptor, injectedParameters);
156156
});
157157
}
158158

159+
function getSourceAction(config) {
160+
return config.reduxSourceAction;
161+
}
162+
159163
var multiClientMiddleware = exports.multiClientMiddleware = function multiClientMiddleware(clients, customMiddlewareOptions) {
160164
var middlewareOptions = _extends({}, defaultOptions, customMiddlewareOptions);
161165
var setupedClients = {};
166+
162167
return function (_ref) {
163168
var getState = _ref.getState,
164169
dispatch = _ref.dispatch;
@@ -167,20 +172,29 @@ var multiClientMiddleware = exports.multiClientMiddleware = function multiClient
167172
if (!middlewareOptions.isAxiosRequest(action)) {
168173
return next(action);
169174
}
175+
170176
var clientName = middlewareOptions.getClientName(action) || middlewareOptions.defaultClientName;
177+
171178
if (!clients[clientName]) {
172179
throw new Error('Client with name "' + clientName + '" has not been defined in middleware');
173180
}
181+
174182
if (!setupedClients[clientName]) {
175183
var clientOptions = _extends({}, middlewareOptions, clients[clientName].options);
184+
176185
if (clientOptions.interceptors) {
177-
bindInterceptors(clients[clientName].client, { getState: getState, dispatch: dispatch, action: action }, middlewareOptions.interceptors, clients[clientName].options.interceptors);
186+
var middlewareInterceptors = middlewareOptions.interceptors;
187+
var clientInterceptors = clients[clientName].options && clients[clientName].options.interceptors;
188+
var injectToInterceptor = { getState: getState, dispatch: dispatch, action: action, getSourceAction: getSourceAction };
189+
bindInterceptors(clients[clientName].client, injectToInterceptor, middlewareInterceptors, clientInterceptors);
178190
}
191+
179192
setupedClients[clientName] = {
180193
client: clients[clientName].client,
181194
options: clientOptions
182195
};
183196
}
197+
184198
var setupedClient = setupedClients[clientName];
185199
var actionOptions = _extends({}, setupedClient.options, setupedClient.options.getRequestOptions(action));
186200

@@ -189,7 +203,11 @@ var multiClientMiddleware = exports.multiClientMiddleware = function multiClient
189203
REQUEST = _getActionTypes2[0];
190204

191205
next(_extends({}, action, { type: REQUEST }));
192-
return setupedClient.client.request(actionOptions.getRequestConfig(action)).then(function (response) {
206+
207+
var requestConfig = _extends({}, actionOptions.getRequestConfig(action), {
208+
reduxSourceAction: action
209+
});
210+
return setupedClient.client.request(requestConfig).then(function (response) {
193211
var newAction = actionOptions.onSuccess({ action: action, next: next, response: response, getState: getState, dispatch: dispatch }, actionOptions);
194212
actionOptions.onComplete({ action: newAction, next: next, getState: getState, dispatch: dispatch }, actionOptions);
195213
return newAction;

src/middleware.js

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,68 @@
11
import * as defaultOptions from './defaults';
22
import { getActionTypes } from './getActionTypes';
33

4-
function addInterceptor(target, candidate, getState) {
4+
function addInterceptor(target, candidate, injectedParameters) {
55
if (!candidate) return;
66
const successInterceptor = typeof candidate === 'function' ? candidate : candidate.success;
77
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));
1010
}
1111

12-
function bindInterceptors(client, getState, middlewareInterceptors = {}, clientInterceptors = {}) {
12+
function bindInterceptors(client, injectedParameters, middlewareInterceptors = {}, clientInterceptors = {}) {
1313
[...middlewareInterceptors.request || [], ...clientInterceptors.request || []].forEach((interceptor) => {
14-
addInterceptor(client.interceptors.request, interceptor, getState);
14+
addInterceptor(client.interceptors.request, interceptor, injectedParameters);
1515
});
1616
[...middlewareInterceptors.response || [], ...clientInterceptors.response || []].forEach((interceptor) => {
17-
addInterceptor(client.interceptors.response, interceptor, getState);
17+
addInterceptor(client.interceptors.response, interceptor, injectedParameters);
1818
});
1919
}
2020

21+
function getSourceAction(config) {
22+
return config.reduxSourceAction;
23+
}
24+
2125
export const multiClientMiddleware = (clients, customMiddlewareOptions) => {
2226
const middlewareOptions = { ...defaultOptions, ...customMiddlewareOptions };
2327
const setupedClients = {};
28+
2429
return ({ getState, dispatch }) => next => action => {
2530
if (!middlewareOptions.isAxiosRequest(action)) {
2631
return next(action);
2732
}
33+
2834
const clientName = middlewareOptions.getClientName(action) || middlewareOptions.defaultClientName;
35+
2936
if (!clients[clientName]) {
3037
throw new Error(`Client with name "${clientName}" has not been defined in middleware`);
3138
}
39+
3240
if (!setupedClients[clientName]) {
3341
const clientOptions = { ...middlewareOptions, ...clients[clientName].options };
42+
3443
if (clientOptions.interceptors) {
35-
bindInterceptors(clients[clientName].client, { getState, dispatch, action },
36-
middlewareOptions.interceptors, clients[clientName].options.interceptors);
44+
const middlewareInterceptors = middlewareOptions.interceptors;
45+
const clientInterceptors = clients[clientName].options && clients[clientName].options.interceptors;
46+
const injectToInterceptor = { getState, dispatch, action, getSourceAction };
47+
bindInterceptors(clients[clientName].client, injectToInterceptor, middlewareInterceptors, clientInterceptors);
3748
}
49+
3850
setupedClients[clientName] = {
3951
client: clients[clientName].client,
4052
options: clientOptions
4153
};
4254
}
55+
4356
const setupedClient = setupedClients[clientName];
4457
const actionOptions = { ...setupedClient.options, ...setupedClient.options.getRequestOptions(action) };
4558
const [REQUEST] = getActionTypes(action, actionOptions);
4659
next({ ...action, type: REQUEST });
47-
return setupedClient.client.request(actionOptions.getRequestConfig(action))
60+
61+
const requestConfig = {
62+
...actionOptions.getRequestConfig(action),
63+
reduxSourceAction: action
64+
};
65+
return setupedClient.client.request(requestConfig)
4866
.then(
4967
(response) => {
5068
const newAction = actionOptions.onSuccess({ action, next, response, getState, dispatch }, actionOptions);

0 commit comments

Comments
 (0)