Created
April 16, 2018 07:35
-
-
Save 197291/a1ac93a42889f9a96385a0925501a3d3 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
import axios from 'axios'; | |
import * as consts from '../constants'; | |
export default store => next => action => { | |
const {api, type, ...rest} = action; | |
if (!api) { | |
return next(action); | |
} | |
next({...rest, type: type + consts.START}); | |
api.url = consts.BASE_URL + api.url; | |
let promise = null; | |
switch (api.method) { | |
case 'GET': | |
promise = axios.get(api.url); | |
break; | |
case 'PUT': | |
promise = axios.put(api.url, api.data); | |
break; | |
case 'POST': | |
promise = axios.post(api.url, api.data); | |
break; | |
default: | |
} | |
promise | |
.then(({data}) => { | |
if (api.actionToCall) { | |
return store.dispatch(api.actionToCall); | |
} | |
next({...rest, type: type + consts.SUCCESS, data: data.data}); | |
}) | |
.catch( error => { | |
next({...rest, type: type + consts.FAIL, error}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment