Skip to content

Instantly share code, notes, and snippets.

@mbensch
Last active March 23, 2016 15:38
Show Gist options
  • Select an option

  • Save mbensch/ada358f79b1077d7fe42 to your computer and use it in GitHub Desktop.

Select an option

Save mbensch/ada358f79b1077d7fe42 to your computer and use it in GitHub Desktop.
export default () => next => action => {
const callAPI = action[CALL_API];
if (typeof callAPI === 'undefined') {
return next(action);
}
const { url, types } = callAPI;
// const url = action.url;
// const types = action.types;
const [requestType, successType, failureType] = types;
if (typeof callAPI === 'undefined') {
return next(action);
}
function actionWith(data) {
const finalAction = Object.assign({}, action, data);
delete finalAction[CALL_API];
return finalAction;
}
next(actionWith({ type: requestType }));
return axios.get(url).then(
(response) => {
// normalize your response
const normalizedResponse = response.body;
return next(actionWith({
normalizedResponse,
type: successType,
}));
}
).catch(
(error) => {
// do something with your error
return next(actionWith({
type: failureType,
error: error.message || 'Something bad happened',
}));
}
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment