Redux-Observable fetch
Redux-observable fetch (robs-fetch) is a set of redux actions as well as an Epic to allow you to make REST requests easily in a redux-observable setup.
It standardise the approach one may use to make fetch requests in an elegant fashion.
npm install
or yarn install
npm run build
to build the project.All tests will be run as part of the build.
fetchEpic
.fetchEpic
.
npm install robs-fetch --save
or
yarn add robs-fetch
redux-observable
documentation for setting up the middleware.restEpic
from the robs-fetch
package.ES6 Modules
var restEpic = require('robs-fetch').restEpic;
import { restEpic } from 'robs-fetch';
restEpic
in the rootEpic
.
const rootEpic = combineEpics(
pingEpic,
restEpic
);
The standard setup is quick and easy for simple cases. However, in real apps, you’ll probably need more advanced options. These options can be set up at the epic construction for a global effect on all fetch requests. In a near future, you’ll be able to set some options for a single request. Here is how to set it up.
import { createRestEpic } from 'robs-fetch';
import { combineEpics, createEpicMiddleware } from 'redux-observable';
const fetchEpic = createRestEpic(options);
// Apply epic to the redux-observable middleware (see redux-observable documentation).
credentials
: Strategy for the credentials (cookies). See fetch documentation for possible values.headers
: An object containing the header values.The Typescript typings are included inside the module. No need for external typings.
restActions
from the robs-fetch
module.ES6 Modules
var restActions = require('robs-fetch').restActions;
import { restActions } from 'robs-fetch';
restActions
.
const action = restActions.fetchPost({
url: 'path/to/resource',
onCompleteAction: 'ACTION_TO_DISPATCH_WHEN_COMPLETE',
body: {
// Object to send in the body of the Request (when available).
}
});
store.dispatch(action);
Setup a reducer to handle the response.
const reducer = (state, action) => {
if (action.type === 'ACTION_TO_DISPATCH_WHEN_COMPLETE') {
// Alter state here.
return newState;
}
return state;
};
Examples of usage for robs-fetch
can be found under the samples folder.
MIT License. See LICENSE file for more details.