Last active
March 23, 2016 15:38
-
-
Save mbensch/ada358f79b1077d7fe42 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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