Last active
August 24, 2016 03:04
-
-
Save jacobp100/7d8ddad59dd717a33fa1c127ebecbf8a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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