Skip to content

Instantly share code, notes, and snippets.

@197291
Created April 16, 2018 07:35
Show Gist options
  • Save 197291/a1ac93a42889f9a96385a0925501a3d3 to your computer and use it in GitHub Desktop.
Save 197291/a1ac93a42889f9a96385a0925501a3d3 to your computer and use it in GitHub Desktop.
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