项目作者: gretro

项目描述 :
Redux-Observable fetch
高级语言: TypeScript
项目地址: git://github.com/gretro/robs-fetch.git
创建时间: 2017-03-28T02:30:37Z
项目社区:https://github.com/gretro/robs-fetch

开源协议:MIT License

下载


Build Status
robs-fetch

Redux-Observable fetch (robs-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.

Building the project

  1. Perform an npm install or yarn install
  2. Run npm run build to build the project.
  3. Enjoy!

All tests will be run as part of the build.

Roadmap

  • Setup tests using Jest.
  • Create a basic fetchEpic.
  • Create options for fetchEpic.
  • Introduce the concept of busy action.
  • Allow to define options on a per-request basis.
  • Ability to define custom serializers.

How to install in your project

  1. npm install robs-fetch --save

or

  1. yarn add robs-fetch

How to setup

  1. Follow the redux-observable documentation for setting up the middleware.
  2. Import the restEpic from the robs-fetch package.
    1. var restEpic = require('robs-fetch').restEpic;
    ES6 Modules
    1. import { restEpic } from 'robs-fetch';
  3. Insert the restEpic in the rootEpic.
    1. const rootEpic = combineEpics(
    2. pingEpic,
    3. restEpic
    4. );

Advanced setup

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.

  1. import { createRestEpic } from 'robs-fetch';
  2. import { combineEpics, createEpicMiddleware } from 'redux-observable';
  3. const fetchEpic = createRestEpic(options);
  4. // Apply epic to the redux-observable middleware (see redux-observable documentation).

Available Options

  • credentials: Strategy for the credentials (cookies). See fetch documentation for possible values.
  • headers: An object containing the header values.

Typescript typings

The Typescript typings are included inside the module. No need for external typings.

How to dispatch an action

  1. Import the restActions from the robs-fetch module.
    1. var restActions = require('robs-fetch').restActions;
    ES6 Modules
    1. import { restActions } from 'robs-fetch';
  2. Create an action using the restActions.
    1. const action = restActions.fetchPost({
    2. url: 'path/to/resource',
    3. onCompleteAction: 'ACTION_TO_DISPATCH_WHEN_COMPLETE',
    4. body: {
    5. // Object to send in the body of the Request (when available).
    6. }
    7. });
  3. Dispatch the action in the redux store.
    1. store.dispatch(action);
  4. Setup a reducer to handle the response.

    1. const reducer = (state, action) => {
    2. if (action.type === 'ACTION_TO_DISPATCH_WHEN_COMPLETE') {
    3. // Alter state here.
    4. return newState;
    5. }
    6. return state;
    7. };

Examples

Examples of usage for robs-fetch can be found under the samples folder.

License

MIT License. See LICENSE file for more details.