
Security News
Socket Security Analysis Is Now One Click Away on npm
npm now links to Socket's security analysis on every package page. Here's what you'll find when you click through.
rainbow-actions
Advanced tools
This is not a project to reduce redux boilerplate. The project goal is to gather the same domain actions and action creators in namespaces by their domain purpose.
npm install rainbow-actions
import {createActions} from 'rainbow-actions'
type PostId = string
type PayloadDictionary = {
init: PostId;
// ^^^^^^ action creator payload
fulfill: {id: PostId; title: string};
error: {code: number};
fail: never;
// ^^^^^ action creator has no payload
}
const requestPost = createActions<PayloadDictionary>()('get_post')
/**
* runtime value `'get_post_init'`
* typescript type `'get_post_init'`
*/
const initType = requestPost.init.type
/**
* runtime value `{type: 'get_post_init', payload: '42'}`
* typescript type `{type: 'get_post_init', payload: PostId}`
*/
const initAction = requestPost.init('42')
Typescript raises an error when one attempts to access a property of requestPost any other than the keys of PayloadDictionary.
import {createActions, ExtractNamespaceActions, ExtractManyNamespaceActions} from 'rainbow-actions'
type RequestPayloads = {
init: number;
get: string;
end: never;
}
const request = createActions<RequestPayloads>()('request')
/**
* To extract action types from a single namespace you can use `ExtractNamespaceActions`
*/
type Actions = ExtractNamespaceActions<typeof request>
Actions // {type: 'request_init'; payload: number} | {type: 'request_get'; payload: string} | {type: 'request_end'}
/**
* To extract action types from multiple namespaces you can use `ExtractManyNamespaceActions`
*/
type AllActions = ExtractManyNamespaceActions<[typeof request, typeof anotherNamespace /*...etc*/]>
Note the object passed to the function after the action type base:
import {createActions} from 'rainbow-actions'
type PayloadDictionary = {
one: number;
two: boolean;
three: string;
}
/**
* You can see that using the optional action dictionary argument you can define:
* * payload - a function to generate/modify payload
* * meta - a function to generate/modify meta information
* * error - a flag for error actions
*
* You can define the ones you need or omit them alltogether
*/
const base = createActions<PayloadDictionary>()('base', {
one: {payload: (id) => id * 2},
// ^^ the type inferred as number since we defined it in PayloadDictionary
two: {meta: (flag) => `flag is ${flag}`},
three: {error: true},
})
/**
* runtime value `{type: 'base_one', payload: 10}`
* typescript type `{type: 'base_one', payload: number}`
*/
const one = base.one(5)
/**
* runtime value `{type: 'base_two', payload: true, meta: 'flag is true'}`
* typescript type `{type: 'base_two', payload: boolean, meta: string}`
*/
const two = base.two(true)
/**
* runtime value `{type: 'base_three', payload: 'hey', error: true}`
* typescript type `{type: 'base_three', payload: string, error: true}`
*/
const three = base.three('hey')
Both basic and advanced usage work using the Proxy object. This means if you are not 100% confident about your types, then developers may accidentally dispatch incorrect actions since any method on the created action namespace will be a valid action creator. If this sounds scary, worry no more, this package also provides a safe version.
If you prefer to avoid the Proxy usage or are not 100% confident in the typescript types in your project, you can use the safe version. It requires to pass all of the actions for the namespace to be accessible.
import {createActions} from 'rainbow-actions/safe'
// note the import path ^^^^
type PayloadDictionary = {
none: never;
one: number;
two: boolean;
three: string;
}
const requestPost = createActions<PayloadDictionary>()('base', {
none: 0,
// ^ if no creators are necessary, 0 can be a placeholer
one: {payload: (id) => id * 2},
two: {meta: (flag) => `flag is ${flag}`},
three: {error: true},
})
/**
* runtime value `{type: 'base_none'}`
* typescript type `{type: 'base_none'}`
*/
const none = base.none()
/**
* runtime value `{type: 'base_one', payload: 10}`
* typescript type `{type: 'base_one', payload: number}`
*/
const one = base.one(5)
/**
* runtime value `{type: 'base_two', payload: true, meta: 'flag is true'}`
* typescript type `{type: 'base_two', payload: boolean, meta: string}`
*/
const two = base.two(true)
/**
* runtime value `{type: 'base_three', payload: 'hey', error: true}`
* typescript type `{type: 'base_three', payload: string, error: true}`
*/
const three = base.three('hey')
FAQs
Type safe and namespaced redux actions
The npm package rainbow-actions receives a total of 37 weekly downloads. As such, rainbow-actions popularity was classified as not popular.
We found that rainbow-actions demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
npm now links to Socket's security analysis on every package page. Here's what you'll find when you click through.

Security News
A compromised npm publish token was used to push a malicious postinstall script in cline@2.3.0, affecting the popular AI coding agent CLI with 90k weekly downloads.

Product
Socket is now scanning AI agent skills across multiple languages and ecosystems, detecting malicious behavior before developers install, starting with skills.sh's 60,000+ skills.