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-actions-defined

v0.1.0

Published

A library for adding definition for actions in redux

Downloads

2

Readme

react-actions-defined

A library for adding definition for actions in redux

** PLEASE NOTE: This is an early, prerelease pacakge it is subject to (and will likely) change. **

This library leverages type-definitions for defining actions

Defining actions

To define a simple action that takes a payload of a string:

import { defineAction } from 'redux-actions-defined';

const { action, schema, creator } = defineAction('test-action', String);

More complex payloads

Similarly, you can define complex objects and arrays

import { defineAction } from 'redux-actions-defined';
import { types, optional } from 'type-definitions';

const { action, schema, creator } = defineAction('test-action', {
  id: optional(Number),
  value: {
    a: Number
    b: String,
    c: [String]
  },
  foo: types.Any.optional
});

this would accept payloads like:

{
  id: 1,
  value: {
    a: 1,
    b: 'test',
    c: ['test2', 'test3']
  }
}

but reject

{
  value: {
    a: 'not-a-nubmer',
    b: 'test',
    c: ['test2', 'test3']
  },
  foo: '!!!'
}

since value.a does not meet the Number condition

defineAction returns an object with three properties

  • type - The string action name
  • schema - The schema used to validate the payload
  • creator - a creator function, it takes a payload argument and returns an object in the form of { type: type, payload: payload }, emitting an error to the console if the provided payload does not match the specified schema

Defining your own creator

Sometimes you want define a creator that, rather than just taking a constructed payload, takes arguments and constructs a payload for you. You can do this by calling the .newCreator function of the defineAction() creator.

const { type, creator } = defineAction('test-action', {
  id: Number,
  value: String
});

const randomValueCreator = creator.newCreator((id) => {
  id,
  value: Math.random()
})

the callback passed to newCreator should return a properly constructed payload, which will be validated in the same way calling the initially generated creator would have been. Consequently randomValueCreator defined above will error since value is not a string.

Redefinition protection

This module keeps track of defined actions so if you try and define an action with the same type key else where it will let you know!

Middleware

You can include the provided middleware which will error in the console when either an action that hasn't been defined, or one that is sent with an invalid payload is dispatched

Future optimization: Currently there is no way to define action types to ignore from the middleware. So actions used by plugins (which you wouldn't expect to define) will error

import reducer from './somewhere'
import { applyMiddleware, createStore } from 'redux';
import { reduxActionsDefinedMiddleware } from 'redux-actions-defined';

const store = createStore(
  reducer,
  applyMiddleware(reduxActionsDefinedMiddleware())
);

Reducer helper

Also included is a helper for constructing multi action reducers. It takes an has map in the form

import { createReducer } from 'redux-actions-defined';

createReducer({
  action_type: function (state, action) {
    // do something
    return state;
  }
})

it will route the action to the appropriate function based on type, returning the state, unmodified, if the action type does not match any of the functions in the provided hash

Prior art

This was very much based on https://github.com/modernserf/redux-action-schema since I didn't love the array-style schema definitions