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

action-creators

v0.1.0

Published

Redux action creators with logging and validation

Downloads

43

Readme

action-creators

Build Status codecov

Action creators utilities for Redux.
Features:

  • Parameter validation using joi.
  • Debug created actions using debug.
  • No need to define constants.
  • No duplicated namespaces.
  • No duplicated action names.

Notes:

Installation

npm i --save action-creators joi joi-browser

Add to webpack.config.js

resolve: {
  alias: {
    joi: 'joi-browser'
  }
}

Enable debugging

Add to your your app.

if (process.env.NODE_ENV !== 'production') {
  process.env.DEBUG = 'ac:*';
}

Or in webpack.config.js

plugins: [
  new webpack.DefinePlugin({
    'process.env': {
      DEBUG: JSON.stringify(process.env.NODE_ENV !== 'production' ? 'ac:*' : ''),
    },
  }),
],

Quick demo

// user-actions.js

import createNamespace from 'action-creators';

const {createAction} = createNamespace('USERS');

const usersLoaded = createAction('USERS_LOADED',
  ['keyword', 'items', 'pageNumber', 'pageSize'],
  {
    keyword: Joi.string().required(),
    items: Joi.array().required(),
    pageNumber: Joi.number().required(),
    pageSize: Joi.number().required(),
  }
);
expect(usersLoaded('john', [{id: 1, name: 'john'}], 1, 10)).toEqual({
  type: 'USERS/USERS_LOADED',
  payload: {
    keyword: 'john',
    items: [{id: 1, name: 'john'}],
    pageNumber: 1,
    pageSize: 10,
  },
});

Console output:

ac:USERS USERS_LOADED { keyword: 'john', items: [ { id: 1, name: 'john' } ], pageNumber: 1, pageSize: 10 } +0ms

Error reporting

usersLoaded('john', -2, 1, 10)

throws an error

ValidationError: Validation failed for: "USERS/USERS_LOADED" {
  "keyword": "john",
  "pageNumber": 1,
  "pageSize": 10,
  "items" [1]: -2
}

[1] "items" must be an array

Usage with reducers

// user-reducer.js
import {usersLoaded} from './user-actions';

function reducer(state = {}, action) {
  switch (action.type) {
    case usersLoaded.toString(): 
      return {
        ...state,
        ...action.payload,
      }
    default:
      return state;
  }
}

You can also use handleActions from redux-actions.

Motivation

  • During development, action creators can be called with invalid or missing arguments, and it usually causes errors in the reducer function.
  • When using createAction from redux-actions, it's not obvious if the action creator requires any arguments.
    For example:
    increment = createAction('INCREMENT');
    You don't know if you should call increment() or increment(something). In such situation, you always must check the expected payload in the reducer.
  • I needed a fast way to debug created actions. There are existing libraries for logging like redux-logger, but it can sometimes be not convenience. You must expand 3 levels of an object to see the action payload. It's much more readable if the action payload is logged in a single line.

API

  • createNamespace(namespace)
    • Parameters:
      • namespace: String The namespace prefix for all action types. All namespaces must be unique otherwise an error will be thrown.
    • Returns:
      {createAction: Function} Return an object with a createAction property.
  • createAction(type, argNames, schema, transform)
    • Parameters:
      • type: String The action type. All namespaces must be unique otherwise an error will be thrown.
      • argNames: Array An array with arguments.
      • schema: Object A Joi schema. Must be an object containing all props from the argNames array.
      • transform: Function(Object) An optional function to transform the created action. You can use it to change payload or metadata.
    • Returns:
      • Function The action creator

MIT License

Copyright (c) 2017 Łukasz Sentkiewicz