All files / src middleware.js

100% Statements 12/12
100% Branches 2/2
100% Functions 4/4
100% Lines 10/10
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              2x   3x 12x   12x   12x   12x 36x 22x 22x     14x              
import { Subject } from 'rxjs/Subject';
import composeAdapters from './composeAdapters.js';
import createActionStream from './createActionStream'
import { CALL_API } from './constants';
import fetch from 'redux-api-call-adapter-fetch';
import parseJSON from 'redux-api-call-adapter-json';
 
const defaultAdapter = composeAdapters(parseJSON, fetch);
 
export const createAPIMiddleware = (adapter) => ({ dispatch, getState }) => {
  const apiCallsAction$ = new Subject();
 
  const finalAdapter = adapter(getState);
 
  createActionStream(apiCallsAction$, { getState }, finalAdapter).subscribe(dispatch);
 
  return next => action => {
    if (!action[CALL_API]) {
      next(action);
      return;
    }
 
    apiCallsAction$.next(action);
  };
}
 
 
// middleware
export default createAPIMiddleware(defaultAdapter)