npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redux-reducer-async

v1.1.2

Published

Create redux reducers for async behaviors of multiple actions.

Downloads

15

Readme

redux-reducer-async

Create redux reducers for async behaviors of multiple actions.

npm Travis CI Codecov

Be DRY & reduce boilerplate. Standardize state schema with managed properties for loading, success, and error cases.

Think of it as redux-actions for asynchronous reducers.

Works with Flux Standard Actions (FSA). By default, supports the action type conventions of redux-promise-middleware, but see Custom Action Types below for configuration to support redux-promise.

Install

npm install --save redux-reducer-async (copy)

Use

import createReducer from 'redux-reducer-async'

const myActionReducer = createReducer('MY_ACTION')

results in a reducer like this:

(state = {}, action = {}) => {
  switch (action.type) {
    case 'MY_ACTION_PENDING':
      return { ...state, loading: true, error: null }
    case 'MY_ACTION_FULFILLED':
      return { ...state, loading: false, error: null, data: action.payload }
    case 'MY_ACTION_REJECTED':
      return { ...state, loading: false, error: action.payload }
    default:
      return state
  }
}

You can then mount it with combineReducers:

import { combineReducers } from 'redux'
import createReducer from 'redux-reducer-async'

const rootReducer = combineReducers({
  myAction: createReducer('MY_ACTION')
})

Or even call it manually within another reducer (useful with custom properties or reducers):

import createReducer from 'redux-reducer-async'

const myActionReducer = createReducer('MY_ACTION')

const reducer = (state = {}, action = {}) => {
  state = myActionReducer(state, action)
  // ...
  return state
}

Custom Properties

You can provide custom property names (all optional) for each case to be used on the state:

createReducer('MY_ACTION', {
  loading: 'isMyActionLoading',
  success: 'myActionData',
  error: 'myActionError'
})

Custom Reducers

You can also provide custom reducer functions (again all optional, but be careful to define all cases if you use non-standard property names in one):

createReducer('MY_ACTION', {
  loading: state => ({
    ...state,
    myActionError: null,
    myActionIsLoading: true,
    extra: 'whatever'
  })
  // success, error...
})

And you can even mix these with custom properties:

createReducer('MY_ACTION', {
  loading: 'isLoading',
  error: (state, action) => ({
    ...state,
    isLoading: false,
    error: action.payload,
    also: 'etc'
  })
})

Custom Action Types

You can provide custom action types.

For example, to support redux-promise, which uses same the action type for success and error cases (though it does not provide a loading action), you can use finalActionType:

import createReducer, { finalActionType } from 'redux-reducer-async'

createReducer(finalActionType('MY_ACTION'))

which is effectively like providing custom action types:

createReducer({
  loading: 'MY_ACTION_PENDING',
  success: 'MY_ACTION',
  error: 'MY_ACTION'
})

Or similarly by passing suffixes to the actionTypes helper, which is normally used to explicitly define all types:

import createReducer, { actionTypes } from 'redux-reducer-async'

createReducer(actionTypes('MY_ACTION', '_LOADING', '_SUCCESS', '_ERROR'))

But can also be used to suppress suffixes (here undefined means use default):

createReducer(actionTypes('MY_ACTION', undefined, '', ''))

Transforms

As a shortcut to defining custom reducers, you can provide transform functions to manipulate only the payload, optionally in success and/or error cases:

createReducer('MY_ACTION', {
  transform: payload => ({
    ...payload,
    title: payload.title.trim()
  }),
  transformError: payload => ({
    ...payload,
    message: `There was an error: ${payload.message}`
  })
})