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 🙏

© 2025 – Pkg Stats / Ryan Hefner

redux-action-factory

v0.3.1

Published

Simple library for creating schema-validated Redux Actions (or thunks, promises, etc.). Powered by the awesomely awesome Schema-Inspector.

Readme

Redux Action Factory

npm version Build Status Coverage Status

Simple library for creating schema-validated Redux Actions (or thunks, promises, etc.). Powered by the awesomely awesome Schema-Inspector.

Installation

npm install redux-action-factory --save

Motivation

In big projects, many action types have to be managed, each one with its Action Creator (sometimes a thunk that will dispatch other actions and perform side-effects), and that generates a large number of files, complexity, and makes the project harder to maintain in general.

With Redux Action Factory, good maintenance is achieved by:

  • Centralizing the Actions' specification in a file.
  • Ensuring robustness in the action creation process by checking arguments' types.
  • Eliminating the need for synchronous actions' Action Creators.
  • Providing a common interface and place for all complex Action Creators (asynchronous action creators, thunks, etc.).

Furthermore, it has been designed with Unit Testing in mind.

Usage

Initialize the factory

In your index.js you should put something similar to:

index.js:

var initialize = require('redux-action-factory').initialize;
var actionsSpecification = require('./actions.json');

initialize({
    actions: actionsSpecification,
    actionCreators: [...],  // optional
    inject: [...]           // optional
});

Note: It has been made to support ES6 imports:

import {initialize} from 'redux-action-factory';

Create a specification file for your actions

Example of actions.json:

{
    "ACTION_NAME": {
        "type": "myProject/ACTION",
        "args": {
            "type": "object",
            "strict": true,
            "properties": {
                "actionParam1": {"type": "number"},
                "actionParam2": {"type": "string", "optional": true}
            }
        }
    }
}

Use it to create actions

Somewhere in your app:

var createAction = require('redux-action-factory').createAction;

dispatch(createAction('ACTION_NAME', {actionParam1: 42, actionParam2: 'Other data'}));

And finally, a little change in the reducers

In your reducers, instead of:

case 'ACTION_NAME':

Just do:

var type = require('redux-action-factory').type;

[...]
case type('ACTION_NAME'):

Testing

When testing, if you need to check if the proper action was created but you don't want to trigger any side effects or execute any Action Creator code, you can use the createRawAction method as follows:

var createRawAction = require('redux-action-factory').createRawAction;

// This won't execute the Action Creator function
createRawAction('ACTION_NAME', {actionParam1: 42, actionParam2: 'Other data'});

Also, if you need to test the correctness of your actions.json file(s), you can use the exported objects actionsSanitizationSchema and actionsValidationSchema like this:

var sanitization = require('redux-action-factory').actionsSanitizationSchema;
var validation = require('redux-action-factory').actionsValidationSchema;
var inspector = require('schema-inspector'); // Schema-Inspector should be installed in your project
var actions = require('./actions.json');
var assert = require('chai').assert;

describe('Actions Schema', function () {
  it('should be a valid schema', function () {
    inspector.sanitize(sanitization, actions);
    var result = inspector.validate(validation, actions);
    assert.ok(result.valid, result.format());
  });
});

API

initialize

initialize(configuration)

Creates and initializes the Action Factory every time it is called. Only the last call will be taken into account.

  • configuration: The configuration object for the factory. It allows the following properties:
    • actions: An actions specification object. Check Actions API for more details.
    • actionCreators(optional): A map of Action Creators names and functions. These functions should return dispatchable objects in your Redux system. A good practice is to define each of them in a separate file inside a specific folder, and then require them all with a glob require library (babel-plugin-import-glob, node-glob, etc.).
    • inject(optional): An additional parameter that will be injected as a second parameter into every Action Creator call. Any type allowed, also objects that can contain whatever fits you. Useful for passing interfaces for actions side-effects (like APIs interfaces) into every action creator.

Actions

{
    "ACTION": {
        "type": {string}
        "args": {object}
        "sanitize": {object} //optional
        "creator": {string} //optional
    }
}
  • type: A unique string that shall be used to identify the Action in the reducers and across systems (recommended to be namespaced).
  • args: A valid Schema-Inspector's validation schema to run against the parameters passed in createAction.
  • sanitize(optional): A valid Schema-Inspector's sanitization schema to run against the parameters passed in createAction.
  • creator(optional): The name of an Action Creator provided in the initial configuration.

getConfig

getConfig()

Returns the config passed in the last call to initialize.

createAction

createAction(actionName, params)

If actionName and params are compliant with the configuration's actions specification, creates and returns a dispatchable action. Otherwise, throws an Error with human readable details on the failure.

  • actionName: The Action's name in the specification.
  • params(optional): The Action's data.

If no Action Creator was defined for that action, returns the raw action, an object such as:

{
    type: configuration.actions[actionName].type,
    ...params
}

Otherwise, executes the provided action creator with the raw action as a first parameter and the injected parameter as second.

actionCreator(rawAction, configuration.inject)

createRawAction

createRawAction(actionName, params)

Same as createAction, but it always returns the raw action object without calling the actionCreator (if there was one).

actionsValidationSchema

actionsValidationSchema

The schema against which the configuration.actions object will be validated after being sanitized. Useful for validating actions.json correctness at testing time.

actionsSanitizationSchema

actionsSanitizationSchema

The schema by which the configuration.actions object will be sanitized before validation. Useful for validating actions.json correctness at testing time.

Thanks and contributing

Thanks to the boilerplate's authors Travelport-Ukraine for npm-module-boilerplate-es6.

If you have an idea on how to improve this library, I listen to suggestions here in the issues channel.