Every action will dispatch type name from types plus postfix.
// This example will dispatch PEOPLE_PENDING, PEOPLE_SUCCESS or PEOPLE_FAILURE/* * Full example * */exportfunctiongetPeopleList(country, age) {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 * */asyncfunctiongetPeopleName(state, dispatch, props) {let {people: {entriesOnPage}} = state;let requestString =`people/?rows=${entriesOnPage}`;const {data: {people,total}} =awaitfetch(requestString);return {people, total};}// and so on .../* * Simple examples * */exportfunctiongetUnicornList(data) {return { types:'UNICORN', list:request.post('food/', data) } }exportfunctiongetUnicornList(data) {return { types:'UNICORN', list:request.post('list/', data), food:request.post('food/', data), } }exportfunctiongetUnicornList() {return { types:'UNICORN',list:async (state, dispatch, props) => {let rainbow =awaitgetRainbow();returnrainbow.data.colors } } }exportfunctiongetUnicornList(data) {return { types:'UNICORN', data: {...data},list:async (state, dispatch, props) => {let rainbow =awaitgetRainbow();returnrainbow.data.colors },food:async (state, dispatch, props) => {let rainbow =awaitgetFood();returnrainbow.data.colors } } }exportfunctiongetUnicornList(data1, data2) {return { types:'UNICORN', list:Promise.all([request.post('food/', data1),request.post('rainbow/',data2) ]) } }
API
APISync exposes single constructor function for creating middleware.
APISync( options: Object )
// Full exampleAPISync({ postfix: { pending:'IS_PENDING', success:'IS_SUCCESS' failure: 'IS_FAILURE' },// global enable/disable dispatching event off: { pending:false, success:false, failure:false, } onPending: (dispatch, data) => {console.log('some action api pending'); },onSuccess: (dispatch, result, data) => {console.log('some action api success'); },onError: (dispatch, error, data) => {// example use on errorsif(err.status ===401)dispatch({type:'LOGOUT_SUCCESS'});console.log('some action api error'); }})
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 */onPending= (dispatch, data) => {}
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 */onSuccess= (dispatch, result, data) => {}
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 */onError= (dispatch, error, data) => {}
off
Global disable dispatching SUCCESS, PENDING or FAULURE event