Skip to content

Instantly share code, notes, and snippets.

@confiks
Created August 11, 2015 01:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save confiks/f402e3a865bb685985aa to your computer and use it in GitHub Desktop.
Save confiks/f402e3a865bb685985aa to your computer and use it in GitHub Desktop.
export default function httpClientMiddleware(clientImpl = window.fetch) {
return (store) => {
return (next) => (action) => {
const { httpClient, types, ...rest } = action;
if (!httpClient)
return next(action);
const [REQUEST, SUCCESS, FAILURE] = types;
httpClient(clientImpl).then(
(result) => result.json()
).then(
(result) => store.dispatch({result, type: SUCCESS}),
(error) => store.dispatch({error, type: FAILURE})
);
return next({...rest, type: REQUEST});
};
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment