redux-sync-promise
Middleware for writing asynchronous actions in synchronous style
Installation
$ npm install --save redux-sync-promise
Usage
Add middleware
; const api = ;const createMiddleware = ;const store = reducer;
Action Examples
Every action will dispatch type name from types plus postfix.
// This example will dispatch PEOPLE_PENDING, PEOPLE_SUCCESS or PEOPLE_FAILURE/* * Full example * */ { return types: 'PEOPLE' // this data will add in all functions like arguments data: country age // disable dispatch PENDING event in this action off: pending: true name: getPeopleName work: getPeopleWork food: getPeopleFood } /* * Get people info * @param {object} state - store state * @param {function} dispatch - action dispatch function * @param {array} props - props in the data key in the action * */ { let people: entriesOnPage = state; let requestString = `people/?rows=`; const data: people total = await ; return people total;}// and so on ... /* * Simple examples * */ { return types: 'UNICORN' list: request } { return types: 'UNICORN' list: request food: request } { return types: 'UNICORN' list: async { let rainbow = await ; return rainbowdatacolors } } { return types: 'UNICORN' data: ...data list: async { let rainbow = await ; return rainbowdatacolors } food: async { let rainbow = await ; return rainbowdatacolors } } { return types: 'UNICORN' list: Promiseall request request }
API
APISync exposes single constructor function for creating middleware.
APISync( options: Object )
// Full example
postfix
Add your custom action type postfix for API call.
Default: PENDING, SUCCESS, FAILURE
onPending
Callback when actions in progress
/* * @param {function} dispatch - action dispatch function * @param {object} data - an array of props in the data key in the action */ {}
onSuccess
Callback on success
/* * @param {function} dispatch - action dispatch function * @param {object} result - total result object * @param {object} data - an array of props in the data key in the action */ {}
onError
Callback on error
/* * @param {function} dispatch - action dispatch function * @param {object|string} error - total error * @param {object} data - an array of props in the data key in the action */ {}
off
Global disable dispatching SUCCESS, PENDING or FAULURE event
off: pending: false success: false failure: false
Reducer create helper
Standart wrapper for create reducers. I use it on some projects , for this reason, he added to the package
; initialState { return ...state }
License
Copyright © 2016 Alexander Dukhovnyak
Released under the MIT license. See license for details.