mappersmith-redux-middleware

0.1.0 • Public • Published

Build Status

Mappersmith Redux Middleware

MappersmithReduxMiddleware is a middleware for mappersmith which dispatches your request lifecycle to a redux store

Installation

NPM

npm install mappersmith-redux-middleware --save
# yarn add mappersmith-redux-middleware 

Browser

Download the tag/latest version from the dist folder.

Usage

Configure the middleware with your redux store and add it to your manifest, like:

import forge from 'mappersmith'
import ReduxMiddleware, { setStore } from 'mappersmith-redux-middleware'
 
import store from 'my-store'
setStore(store)
 
const client = forge({
  middlewares: [ReduxMiddleware],
  resources: {
    User: {
      all: { path: '/users' },
      byId: { path: '/users/{id}' }
    }
  }
})

The events will follow the pattern:

{
  type: 'mappersmith/<phase>/<resource-name>/<resource-method>',
  payload: { /* */ }
}

where phase can be: request, response or failure. Examples:

client.User.all({ admin: 'true' })
 
// request event
// {
//   type: 'mappersmith/request/User/all',
//   payload: {
//     params: { admin: true },
//     headers: {},
//     body: null
//   }
// }
//
// response event
// {
//   type: 'mappersmith/response/User/all',
//   payload: {
//     status: 200,
//     headers: { 'content-type': 'application/json' },
//     data: [
//       { id: 1, name: 'John Doe' },
//       /* ... */
//     ]
//   }
// }

In case of failure:

client.User.all({ admin: 'true' })
 
// request event
// {
//   type: 'mappersmith/request/User/all',
//   payload: {
//     params: { admin: true },
//     headers: {},
//     body: null
//   }
// }
//
// response event
// {
//   type: 'mappersmith/failure/User/all',
//   payload: {
//     status: 503,
//     headers: { 'content-type': 'application/json' },
//     data: {
//       error: 'Critical error!'
//     }
//   }
// }

Running all tests

yarn test

Compile and release

NODE_ENV=production yarn build

License

See LICENSE for more details.

Readme

Keywords

none

Package Sidebar

Install

npm i mappersmith-redux-middleware

Weekly Downloads

2

Version

0.1.0

License

MIT

Last publish

Collaborators

  • tulios