-
-
Save mishazawa/7842f9d90a78eaefbe6f0dd0f367d268 to your computer and use it in GitHub Desktop.
Redux + fetch interceptors
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
/* | |
* WARNING: Mutates the fetchContext argument (by default the window or global context). | |
* | |
* A crude way to intercept fetch responses, and dispatch actions as needed. Using this | |
* for more global app concerns, where I may want to dispatch actions depending on the response | |
* status or body. e.g. When seeing a 401, dispatch a logout action. | |
* | |
* In most cases, I'd recommend using a middlware as shown in redux's real-world example. | |
* (https://github.com/reactjs/redux/blob/master/examples/real-world/middleware/api.js) | |
* | |
* Example: | |
* | |
* import { applyMiddleware, compose, createStore } from 'redux'; | |
* | |
* // ... | |
* | |
* const enhancer = compose( | |
* httpInterceptor({ | |
* 200: (dispatch, response) => console.log('Got ', response), | |
* 429: (dispatch, response) => dispatch({ type: 'TOO_MANY_REQUESTS', { payload: response } }), | |
* 500: (dispatch, response) => dispatch({ type: 'SERVER_ERROR', { payload: response } }) | |
* }), | |
* applyMiddleware(...middlewares) | |
* ); | |
* | |
* const store = createStore(rootReducer, initialState, enhancer); | |
* | |
* | |
* If you have full control over service modules, it's probably better | |
* to use AOP and wrap the service module instead of mutating the context. :) | |
*/ | |
const httpInterceptor = (statusHandlers = {}, fetchContext = global) => { | |
const _fetch = fetchContext.fetch; | |
return (createStore) => (reducer, initialState, enhancer) => { | |
const store = createStore(reducer, initialState, enhancer); | |
fetchContext.fetch = async (url, options) => { | |
const resp = await _fetch(url, options); | |
const maybeHandler = statusHandlers[`${resp.status}`]; | |
maybeHandler && maybeHandler(store.dispatch, resp); | |
return resp; | |
}; | |
return store; | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment