MappersmithReduxMiddleware is a middleware for mappersmith which dispatches your request lifecycle to a redux store
npm install mappersmith-redux-middleware --save
# yarn add mappersmith-redux-middleware
Download the tag/latest version from the dist folder.
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!'
// }
// }
// }
yarn test
NODE_ENV=production yarn build
See LICENSE for more details.