Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 10x 36x 11x 11x 11x 11x 11x 8x 8x 3x 3x | import axios from "axios";
import * as actions from "../api";
const api = ({ dispatch }) => next => async action => {
if (action.type !== actions.apiCallBegan.type) return next(action);
const { url, method, data, onStart, onSuccess, onError } = action.payload;
if (onStart) dispatch({ type: onStart });
next(action);
try {
const response = await axios.request({
baseURL: "http://localhost:9001/api",
url,
method,
data
});
// General
dispatch(actions.apiCallSuccess(response.data));
// Specific
Eif (onSuccess) dispatch({ type: onSuccess, payload: response.data });
} catch (error) {
// General
dispatch(actions.apiCallFailed(error.message));
// Specific
if (onError) dispatch({ type: onError, payload: error.message });
}
};
export default api;
|