- Notifications
You must be signed in to change notification settings - Fork0
goncy/async-action-creator
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Async actions with steroids for Redux or whatever you want.
Action creator is a library that helps with handling async actions on redux or similar implementations, giving a couple of methods for dispatching async actions and handling actions status, errors, and responses in a separated reducers so you don't need to hesitate creating a reducer for that.
// Creating an actionimport{createAction}from'async-action-creator'constmyAction=createAction('MY_ACTION')// Mounting the reducerimport{REDUCER_NAME,reducer}from'async-action-creator'constrootReducer=combineReducers({// All of your reducers[REDUCER_NAME]:reducer// Naming is important})// Creating the servicesexportdefault{[myAction.TYPE]:{action:myAction,uri:"https://api.chucknorris.io/jokes/random",method:"GET",onResolve:response=>response.value,onReject:error=>error.message,options:options=>({ ...options,'content-type':'text'})},};// Adding the middlewareimport{middleware}from'async-action-creator'...createStore(rootReducer,applyMiddleware(middleware(services)))...// Middleware in actiondispatch(myAction.run())// Actions automatically dispatched:{type:'MY_ACTION'}{type:'MY_ACTION_STARTED'}{type:'MY_ACTION_RESOLVED',payload:"Jean-Claude Van Damme once attempted to throw a Chuck Norris Roundhouse Kick. He was immediately arrested for fraud."}// Dispatching async actions manuallyconstmapDispatchToProps={run:myAction.run,// run({foo: 'bar'}) -> {type: MY_ACTION, payload: {foo: 'bar'}}start:myAction.start,// start({foo: 'bar'}) -> {type: MY_ACTION_STARTED, payload: {foo: 'bar'}}fetch:myAction.fetch,// fetch({foo: 'bar'}) -> {type: MY_ACTION_FETCH, payload: {foo: 'bar'}}update:myAction.update,// update({foo: 'bar'}) -> {type: MY_ACTION_UPDATE, payload: {foo: 'bar'}}create:myAction.create,// create({foo: 'bar'}) -> {type: MY_ACTION_CREATE, payload: {foo: 'bar'}}remove:myAction.remove,// remove({foo: 'bar'}) -> {type: MY_ACTION_REMOVE, payload: {foo: 'bar'}}resolve:myAction.resolve,// resolve({foo: 'bar'}) -> {type: MY_ACTION_RESOLVED, payload: {foo: 'bar'}}reject:myAction.reject// reject({foo: 'bar'}) -> {type: MY_ACTION_REJECTED, payload: {foo: 'bar'}}}// Getting action status, error and response// Using this with this `myAction.resolve({foo: 'bar'})` as the last action dispatchedconstmapStateToProps=state=>({status:myAction.getStatus(state),// => 'resolved'error:myAction.getError(state),// => undefinedresponse:myAction.getResponse(state)// => {foo: 'bar'})}// Using actions in reducersswitch(type){casemyAction.TYPE:// => 'MY_ACTION'return{ ...state,hello: 'me'}casemyAction.STARTED:// => 'MY_ACTION_STARTED'return{ ...state,hello:'cat'}casemyAction.FETCH:// => 'MY_ACTION_FETCH'return{ ...state,hello:'rat'}casemyAction.UPDATE:// => 'MY_ACTION_UPDATE'return{ ...state,hello:'rabbit'}casemyAction.CREATE:// => 'MY_ACTION_CREATE'return{ ...state,hello:'owl'}casemyAction.REMOVE:// => 'MY_ACTION_REMOVE'return{ ...state,hello:'elephant'}casemyAction.RESOLVED:// => 'MY_ACTION_RESOLVED'return{ ...state,hello:'dog'}casemyAction.REJECTED:// => 'MY_ACTION_REJECTED'return{ ...state,hello:'dodo'}default:returnstate}
Sometimes you need an action status and you had to create a new reducer just for that, also, this reduces the boilerplate of creating a lot of actions for all your async actions.
yarn add async-action-creator// ornpm install --save async-action-creator
// jest testsyarntest// jest coverageyarn coverSimply create a pull request :)
- Code style:Standard
- FlowType used
This project is based on the idea ofmake-action-creator by@ajchambeaud, and thegetStatus andgetError implementations to the same library from@pablen.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS INTHE SOFTWARE.
About
Action creator with steroids for Redux or whatever you want
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.