redux-middleware-fetch
Redux Whatwg Fetch Middleware
Usage
Bind Middleware
// ...;;; const store = ; // ...
Call on action
;; const PRODUCT_FETCHED = Symbol'PRODUCT_FETCHED';const PRODUCT_CREATED = Symbol'PRODUCT_CREATED';const PRODUCT_CREATE_FAILED = Symbol'PRODUCT_CREATE_FAILED';const PRODUCT_CREATING = Symbol'PRODUCT_CREATING'; const fetchProducts = API_REQUEST: types: PRODUCT_FETCHED entrypoint: '/products' auth: true method: 'GET' ; // Thunkify Actionconst createProducts = ;;
Customize Storage
In default, redux-middleware-fetch use localStorage for storage accessToken (in key accessToken). You can change any other storage system implemented getItem and setItem functions for it.
import {
SimpleStorage,
setStorage,
} from 'redux-middleware-fetch';
const storage = new SimpleStorage();
setStorage(storage);
Options
- types[Array]: Array of dispatch action, support every types (eg. String, Symbol), action will be called on [REQUEST_SUCCESS, (REQUEST_FAILED), (REQUEST_SENT)].
- entrypoint[String]: API resource url, fetch will add host declared in global
API_HOST
constant automatically. - auth[Boolean]: If true, JWT Token will add to fetch request in headers
Authorization
value. Token should be storage inlocalStorage.accessToken
. - json[Boolean]: If true, body will be JSON stringified text. (Don't use json, urlEncoded and formData at same time)
- formData[Boolean]: If true, body will append to FormData by assigned keys. (Don't use json, urlEncoded and formData at same time)
- urlEncoded[Boolean]: If true, body will be x-www-form-urlencoded and stringify by querystring module. (Don't use json, urlEncoded and formData at same time)
- body[Object]: Key-value paired post data.
- method[String]: HTTP Method, default: GET.
- onSuccess[Function]: Success callback function, you can do something without store dispatch (eg. dispatch redirect react-router-redux action).
- onFailed[Function]: Failed callback function.
- headers[Object]: Custom headers in object.
Event Listener
import {
requestListener,
EVENT_REQUESTED,
EVENT_REQUEST_SUCCESSED,
EVENT_REQUEST_FAILED,
} from 'redux-middleware-fetch';
requestListener.on(EVENT_REQUESTED, (reqOptions) => {
// do something on request sent
});
requestListener.on(EVENT_REQUEST_SUCCESSED, (response) => {
// do something on request successed
});
requestListener.on(EVENT_REQUEST_FAILED, (response) => {
// do something on request failed
});
To-Do
- Mocks Service / API Simulator