npm install redux-observable-utils
used for simple api request
-
params
moduleName: string
module name which use to nest reducer in redux storereducerName: string
reducer name appeared in redux storeapi: Function
function that return promise to get your payloadmapActionToPayload: Function
(optional) default is(action) => action.payload
parentModuleName: string
(optional) parent module name which use to nest reducer in redux storeoptions: Object
(optional) include fields:cache: boolean
set to false to disable cache. default is truecacheDuration: number
cache duration in millisecond. default is 300
-
return
ducks
requestTypes: Object
an object that includes redux types:FETCH
call api to fetch data, will check lastUpdated, didInvalidate, isFetchingINVALIDATE
invalidate reducer so dispatchingFETCH
will call api againCLEAR
set reducer to initial stateINVALIDATE_ALL
invalidate all keyed reducer so dispatchingFETCH
will call api againCLEAR_ALL
set all keyed reducer to initial stateREQUEST
call api to request data, will not check lastUpdated, didInvalidate, isFetchingSUCCESS
api success and set payload into reducerFAILURE
api failure and set error into reducer
requestActions: Object
an object that includes redux actions:fetch
dispatch FETCH actionparams
pass params as object, eg:fetch({ id: 1 })
invalidate
dispatch INVALIDATE actionclear
dispatch CLEAR actioninvalidateAll
dispatch INVALIDATE_ALL actionclearAll
dispatch CLEAR_ALL actionrequest
dispatch REQUEST actionparams
pass params as object, eg:fetch({ id: 1 })
success
dispatch SUCCESS action, is not called manually, it will be called after api call is successpayload
data return by api callparams
able to access params passed into fetch/request
failure
dispatch FAILURE action, is not called manually, it will be called after api call is successparams
able to access params passed into fetch/request
reducer: Function
reducer function to be added into redux storeselector: Function
selector function to get data from storereducerName: string
reducerName passed in
epic
epic function to be added into redux observable epic middleware
{
ducks: {
requestTypes,
requestActions,
reducer,
selector,
reducerName,
},
epic,
}
const todos = createRequestEpicDucks({
moduleName: 'TODO',
reducerName: 'TODOS',
api: api.getTodos,
mapActionToPayload: (action) => action.payload,
parentModuleName: 'HOME',
options: {
cache: true,
cacheDuration: 300,
},
});
action TODO/TODOS_FETCH dispatched:
does not change the store, it is used in epic to dispatch TODO/TODOS_REQUEST if the criteria below is met.
- if isFetching is true, REQUEST will not be dispatched
- if lastUpdated is within cacheDuration, REQUEST will not be dispatched
- if didInvalidate is false, REQUEST will not be dispatched
- if lastUpdated is within cacheDuration, REQUEST will not be dispatched
action TODO/TODOS_REQUEST dispatched:
TODO: Object
TODOS: Object
didInvalidate: false
error: undefined
isFetching: true
payload: undefined
action TODO/TODOS_SUCCESS dispatched:
TODO: Object
TODOS: Object
didInvalidate: false
error: undefined
isFetching: false
lastUpdated: Moment
payload: 'data'
used for api request group by key
-
params
moduleName: string
module name which use to nest reducer in redux storereducerName: string
reducer name appeared in redux storeapi: Function
function that return promise to get your payloadmapActionToKey: Function
function that map action to request keymapActionToPayload: Function
(optional) default is(action) => action.payload
parentModuleName: string
(optional) parent module name which use to nest reducer in redux storerestoreFetchableKeyToAction: Function
function that map fetchable key back to actionoptions: Object
(optional) include fields:cache: boolean
set to false to disable cache. default is truecacheDuration: number
cache duration in millisecond. default is 300
-
return
ducks
requestTypes: Object
same as in createRequestEpicDucksrequestActions: Object
same as in createRequestEpicDucksreducer: Function
reducer function to be added into redux storeselector: Function
selector function to get data from storereducerName: string
reducerName passed in
epic
epic function to be added into redux observable epic middleware
const todos = createRequestByKeyEpicDucks({
moduleName: 'TODO',
reducerName: 'TODO',
api: api.getTodos,
mapActionToKey: (action) => action.params.todoId,
mapActionToPayload: (action) => action.payload,
parentModuleName: 'HOME',
options: {
cache: true,
cacheDuration: 300,
},
});
TODO: Object
TODO: Object
1: Object
didInvalidate: false
error: undefined
isFetching: true
payload: undefined
2: Object
didInvalidate: false
error: undefined
isFetching: true
payload: undefined
// TODO object keys (1, 2) are todoId returned in mapActionToKey