import { apiMiddleware as reduxApiMiddleware, ApiError, isRSAA, CALL_API }
  from 'redux-api-middleware'

const middleware = store => next => action => {
  const isApiAction = isRSAA(action)

  return isApiAction
    ? reduxApiMiddleware(store)(receiveApiResponse(store)(next))(action)
    : next(action)
}

const receiveApiResponse = store => next => action => {
  //Intercept specific API actions here and set specific case behavior
  //Chance to check for ApiError and use CALL_API if necessary
  return next(action)
}

export default middleware
