Skip to content

Commit dbaf0e3

Browse files
author
Michal Svrček
committed
removed enhanced getState
1 parent c68784c commit dbaf0e3

File tree

4 files changed

+39
-50
lines changed

4 files changed

+39
-50
lines changed

CHANGELOG.md

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

3+
## 2.0.0 ( Aug 10, 2016 )
4+
- changes to support axios@0.13
5+
- removed enhanced getState function as first interceptor argument
6+
37
## 1.3.0 ( Jun 14, 2016 )
48
- added warning to not use first interceptor argument as `getState` function
59
- added `dispatch` and `action` to first interceptor argument - [#26](https://github.com/svrcekmichal/redux-axios-middleware/pull/26)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "redux-axios-middleware",
3-
"version": "1.3.0",
3+
"version": "2.0.0",
44
"description": "Redux middleware for fetching data with axios HTTP client",
55
"main": "lib/index.js",
66
"scripts": {

src/middleware.js

Lines changed: 34 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,54 +21,41 @@ function bindInterceptors(client, getState, middlewareInterceptors = {}, clientI
2121
export 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

test/middleware.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ import MockAdapter from 'axios-mock-adapter';
55

66
import middleware from './../src/middleware';
77

8-
const BASE_URL = 'mockapi';
98
const client = axios.create({
10-
baseURL: BASE_URL,
119
responseType: 'json'
1210
});
1311

0 commit comments

Comments
 (0)