All files / src/store/middleware api.js

100% Statements 17/17
87.5% Branches 7/8
100% Functions 3/3
100% Lines 11/11

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;