redux-effects-fetch-fixture

0.1.0 • Public • Published

Build Status NPM Version

redux-effects-fetch-fixture

This is an extension for redux-effects-fetch, which lets you define fixtures for your FETCH actions. Now you are able to develop completely without any REST backend.

Installation

npm install redux-effects-fetch-fixture

Usage

This package is designed to be used in conjunction with redux-effects. Install it like this:

  import effects from 'redux-effects';
  import fetch from 'redux-effects-fetch';
  import fetchFixture from 'redux-effects-fetch-fixture';
 
  const fixtures = { /* your fixtures */ };
  const fetchMiddleware = isProduction ? fetch : fetchFixture(fixtures);
  applyMiddleware(effects, fetchMiddleware)(createStore);

Define fixtures

The fixture definition is structured like this:

'<path>': {
  '<HTTP-METHOD>': (body[, delegate[, params]]) => httpResponsePromise
}

The path-string supports URL parameters using the :param syntax.

response helpers

There are some response helpers to remove some boilerplate.

import {responses} from 'redux-effects-fetch-fixture';
 
// empty 200 response
responses.ok();
 
// 200 response with body
responses.ok({userId: 123});
 
// delayed 200 response
responses.okDelayed({})

The error helpers define a message and kind field in the response body. This will get more flexible in future releases.

import {responses} from 'redux-effects-fetch-fixture';
 
// rejected promise with Error(message)
responses.error('something went wrong');
 
// 500 response
responses.internalServerError;
 
// delayed 404 response
responses.notFound('user.notFound', 'The requested user was not found')
 
// delayed 401 response
responses.unauthorized('user.unauthorized', 'Authorize first to see this site')
 
// forbidden 403 response
responses.forbidden('user.unauthorized', 'You are not allowed to access this page')

Examples

A fixture could look like this

import {responses} from 'redux-effects-fetch-fixture';
 
const fixture = {
  '/foo': {
    // this delegates to another fixture
    'GET': (body, delegate) => delegate('/test', 'GET', body)
  },
  // simple definition
  '/test': {
    'GET': () => responses.ok({found: true})
  },
  // define responses for different http methods
  'user/:id': {
    'GET': (body, delegate, params) => responses.ok({ id: params.id }),
    'POST': () => responses.ok({created: true})
    'DELETE': () => responses.unauthorized('user.unauthorized', 'not allowed to delete this user')
  },
  // simulate exceptions
  'user/2': {
    'GET': () => responses.internalServerError
  }
};

Build

To build the library

npm run build

Release

npm version [patch|minor|major]
npm publish
git push
git push --tags

Package Sidebar

Install

npm i redux-effects-fetch-fixture

Weekly Downloads

1

Version

0.1.0

License

Apache-2.0

Last publish

Collaborators

  • andi5
  • lunaryorn
  • muuki88