Skip to content

Commit 8a1b130

Browse files
author
Artur Charaev
committed
add batch requests and success / failure interceptors
1 parent 8ac0253 commit 8a1b130

File tree

1 file changed

+45
-17
lines changed

1 file changed

+45
-17
lines changed

src/index.js

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ export const parseResponse = (response) => {
2323
return response;
2424
};
2525

26+
const successInterceptor = (...args) => {
27+
const promise = Promise.resolve(...args);
28+
return promise.then(checkStatus).then(parseResponse);
29+
};
30+
const failureInterceptor = error => Promise.reject(error);
31+
32+
export const callApi = (endpoint, options, success, failure) => (
33+
fetch(endpoint, options).then(success, failure)
34+
);
35+
2636
export const actionWith = (actionType, args, payload) => {
2737
let nextAction;
2838
if (typeof actionType === 'function') {
@@ -46,40 +56,58 @@ export const actionWith = (actionType, args, payload) => {
4656
return nextAction;
4757
};
4858

49-
export const createMiddleware = ({ status = checkStatus, parse = parseResponse }) => (
59+
const normalize = (item, apiAction, getState) => {
60+
if (typeof item === 'function') {
61+
return item(apiAction, getState());
62+
}
63+
return item;
64+
};
65+
66+
export const createMiddleware = ({
67+
responseSuccess = successInterceptor,
68+
responseFailure = failureInterceptor,
69+
}) => (
5070
({ getState }) => next => (action) => {
5171
if (!action[CALL_API]) {
5272
return next(action);
5373
}
5474

5575
const apiAction = action[CALL_API];
5676

57-
// make request endpoint
58-
if (typeof apiAction.endpoint === 'function') {
59-
apiAction.endpoint = apiAction.endpoint(action, getState());
60-
}
61-
62-
// make request opts
63-
if (typeof apiAction.options === 'function') {
64-
apiAction.options = apiAction.options(action, getState());
77+
let { batch, endpoint, options } = apiAction;
78+
const batchMode = Array.isArray(batch);
79+
if (batchMode) {
80+
// prepare requests params
81+
batch = batch.map(request => ({
82+
endpoint: normalize(request.endpoint, apiAction, getState),
83+
options: normalize(request.options, apiAction, getState),
84+
}));
85+
} else {
86+
// prepare request endpoint
87+
endpoint = normalize(endpoint, apiAction, getState);
88+
// prepare request options
89+
options = normalize(options, apiAction, getState);
6590
}
6691

67-
const { endpoint, options, types } = apiAction;
68-
6992
// action types
70-
const [requestType, successType, failureType] = types;
93+
const [requestType, successType, failureType] = apiAction.types;
7194

7295
// dispatch request type
7396
next(actionWith(
7497
requestType, [apiAction, getState()]
7598
));
7699

77-
return fetch(endpoint, options)
78-
.then(status)
79-
.then(parse)
100+
const promises = (batchMode ?
101+
batch.map(request =>
102+
callApi(request.endpoint, request.options, responseSuccess, responseFailure)
103+
) :
104+
[callApi(endpoint, options, responseSuccess, responseFailure)]
105+
);
106+
107+
return Promise.all(promises)
80108
.then(
81-
response => next(actionWith(
82-
successType, [apiAction, getState()], response
109+
responses => next(actionWith(
110+
successType, [apiAction, getState()], batchMode ? responses : responses[0]
83111
)),
84112
error => next(actionWith(
85113
failureType, [apiAction, getState()], error

0 commit comments

Comments
 (0)