A small library to handle messages, message types and message creators (also known as "actions", "action types" and "action creators" in redux context). js-messages is written in TypeScript, everything is completely type safe.
npm install --save js-messages
js-messages consists of three functions:
defineMessage
: To define one type of message / to create one message creatordefineMessages
: To define multiple related messages represented by message creatorsprops
: A helper function to define a message creator where all message properties must be declared explicitly in an object.
Example 1 (using defineMessage
)
import { defineMessage } from 'js-messages'
const increment = defineMessage('increment')
// for messages of shape { type: 'increment' }
const decrement = defineMessage('decrement')
// for messages of shape { type: 'decrement' }
const resetTo = defineMessage('resetTo', (value: number) => ({ value }))
// for messages of shape { type: 'resetTo', value: number }
Example 2 (using defineMessage
and props
)
const saveToStorage = defineMessage(
'saveToStorage',
props<{ tasks: Task[]; storageKey: string }>()
)
// for messages of shape
// {
// type: 'saveToStorage',
// tasks: Task[],
// storageKey: string
// }
Example 3 (using defineMessages
, without namespace):
import { defineMessages } from 'js-messages'
const Actions = defineMessages({
increment: null,
// for messages of shape { type: 'increment' }
decrement: null,
// for messages of shape { type: 'decrement' }
resetTo: (value: number) => ({ value })
// for messages of shape { type: 'resetTo', value: number }
log: (value: number, message: string = null) => ({ value, message })
// for messages of shape { type: 'log', value: number, message: string }
})
Example 4 (using defineMessages
, with namespace):
import { defineMessages } from 'js-messages'
const Actions = defineMessages('counter', {
increment: null,
// for messages of shape { type: 'counter.increment' }
decrement: null,
// for messages of shape { type: 'counter.decrement' }
resetTo: (value: number) => ({ value })
// for messages of shape { type: 'counter.resetTo', value: number }
log: (value: number, message: string = null) => ({ value, message })
// for messages of shape { type: 'counter.log', value: number, message: string }
})
"js-messages" is licensed under LGPLv3.
"js-messages" is currently in beta status.