2.0.2 • Published 7 years ago
redux-observable-utils v2.0.2
redux-observable-utils
Installation
npm install redux-observable-utils
Usage
createRequestEpicDucks
used for simple api request
- params
moduleName: stringmodule name which use to nest reducer in redux storereducerName: stringreducer name appeared in redux storeapi: Functionfunction that return promise to get your payloadmapActionToPayload: Function(optional) default is(action) => action.payloadparentModuleName: string(optional) parent module name which use to nest reducer in redux storeoptions: Object(optional) include fields:cache: booleanset to false to disable cache. default is truecacheDuration: numbercache duration in millisecond. default is 300
return
ducksrequestTypes: Objectan object that includes redux types:FETCHcall api to fetch data, will check lastUpdated, didInvalidate, isFetchingINVALIDATEinvalidate reducer so dispatchingFETCHwill call api againCLEARset reducer to initial stateINVALIDATE_ALLinvalidate all keyed reducer so dispatchingFETCHwill call api againCLEAR_ALLset all keyed reducer to initial stateREQUESTcall api to request data, will not check lastUpdated, didInvalidate, isFetchingSUCCESSapi success and set payload into reducerFAILUREapi failure and set error into reducer
requestActions: Objectan object that includes redux actions:fetchdispatch FETCH actionparamspass params as object, eg:fetch({ id: 1 })
invalidatedispatch INVALIDATE actioncleardispatch CLEAR actioninvalidateAlldispatch INVALIDATE_ALL actionclearAlldispatch CLEAR_ALL actionrequestdispatch REQUEST actionparamspass params as object, eg:fetch({ id: 1 })
successdispatch SUCCESS action, is not called manually, it will be called after api call is successpayloaddata return by api callparamsable to access params passed into fetch/request
failuredispatch FAILURE action, is not called manually, it will be called after api call is successparamsable to access params passed into fetch/request
reducer: Functionreducer function to be added into redux storeselector: Functionselector function to get data from storereducerName: stringreducerName passed in
epicepic function to be added into redux observable epic middleware
example:
{
ducks: {
requestTypes,
requestActions,
reducer,
selector,
reducerName,
},
epic,
}usage example:
const todos = createRequestEpicDucks({
moduleName: 'TODO',
reducerName: 'TODOS',
api: api.getTodos,
mapActionToPayload: (action) => action.payload,
parentModuleName: 'HOME',
options: {
cache: true,
cacheDuration: 300,
},
});store example:
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'
createRequestByKeyEpicDucks
used for api request group by key
- params
moduleName: stringmodule name which use to nest reducer in redux storereducerName: stringreducer name appeared in redux storeapi: Functionfunction that return promise to get your payloadmapActionToKey: Functionfunction that map action to request keymapActionToPayload: Function(optional) default is(action) => action.payloadparentModuleName: string(optional) parent module name which use to nest reducer in redux storerestoreFetchableKeyToAction: Functionfunction that map fetchable key back to actionoptions: Object(optional) include fields:cache: booleanset to false to disable cache. default is truecacheDuration: numbercache duration in millisecond. default is 300
return
ducksrequestTypes: Objectsame as in createRequestEpicDucksrequestActions: Objectsame as in createRequestEpicDucksreducer: Functionreducer function to be added into redux storeselector: Functionselector function to get data from storereducerName: stringreducerName passed in
epicepic function to be added into redux observable epic middleware
usage example:
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,
},
});store example:
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 mapActionToKey2.0.2
7 years ago
2.0.1
7 years ago
2.0.0
7 years ago
1.2.12
8 years ago
1.2.11
8 years ago
1.2.10
8 years ago
1.2.9
8 years ago
1.2.8
8 years ago
1.2.7
8 years ago
1.2.6
8 years ago
1.2.5
8 years ago
1.2.4
8 years ago
1.2.3
8 years ago
1.2.2
8 years ago
1.2.1
8 years ago
1.1.0
9 years ago
1.0.6
9 years ago
1.0.5
9 years ago
1.0.4
9 years ago
1.0.3
9 years ago
1.0.2
9 years ago
1.0.0
9 years ago