Skip to content

Instantly share code, notes, and snippets.

@jacobp100
Last active August 24, 2016 03:04
Show Gist options
  • Save jacobp100/7d8ddad59dd717a33fa1c127ebecbf8a to your computer and use it in GitHub Desktop.
Save jacobp100/7d8ddad59dd717a33fa1c127ebecbf8a to your computer and use it in GitHub Desktop.
const fetch = (url, params) => ({
type: 'FETCH',
url,
params,
});
const fetchMiddleware = fetchImplementation => store => next => action => {
if (action.type === 'FETCH') {
const { url, params } = action;
const token = store.getState().token;
_.set(params, 'headers.token', token);
return fetchImplementation(url, params);
} else {
return next(action);
}
};
const middleware = applyMiddleware(fetchMiddleware(window.fetch));
const store = createStore(reducers, middleware);
// Example action
const getUser = id => async ({ dispatch }) => {
const result = await dispatch(fetch(`http://api.website.com/${id}`, { method: 'GET' }));
...
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment