
Security News
Feross on Risky Business Weekly Podcast: npm’s Ongoing Supply Chain Attacks
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
@artossystems/a
Advanced tools
A zero-dependency, boilerplate-reducing utility for easily creating typesafe Redux actions in TypeScript.
yarn add @artossystems/a
# or
npm install @artossystems/a
/**
* Actions
*/
import a from '@artossystems/a'
// Simple action without payload
const ASimpleAction = a('SIMPLE/ACTION')
type ASimpleAction = ReturnType<typeof ASimpleAction>
/**
* Produces:
* {
* type: "SIMPLE/ACTION",
* }
*/
// Action with payload
const APayloadAction = a('PAYLOAD/ACTION', {} as { str: string })
type APayloadAction = ReturnType<typeof APayloadAction>
/**
* Produces:
* {
* type: "PAYLOAD/ACTION",
* str: "a string",
* }
*/
export { ASimpleAction, APayloadAction }
/**
* Reducer
*/
export const reducer: Reducer<IState, ASimpleAction | APayloadAction> = (
state = { stringInState: 'idle' },
action,
) => {
switch (action.type) {
case ASimpleAction.TYPE:
return {
...state,
stringInState: 'updated string',
}
case APayloadAction.TYPE:
return {
...state,
stringInState: action.str,
}
default:
return state
}
}
/**
* Map Dispatch To Props
*/
const mapDispatchToProps = (dispatch: Dispatch): IDispatchProps => ({
onSimpleClick: () => dispatch(ASimpleAction()),
onPayloadClick: (str: string) => dispatch(APayloadAction({ str })),
})
The aim of this project is to reduce boilerplate but also aid readability of action files. Ideally, the action and types will fit on one line each. However, Prettier's 80 character line-length limit may make this difficult. One way to give yourself more characters, is to export all the actions at the end of the file:
const ASimpleAction = a('SIMPLE/ACTION')
type ASimpleAction = ReturnType<typeof ASimpleAction>
const APayloadAction = a('PAYLOAD/ACTION', {} as { str: string })
type APayloadAction = ReturnType<typeof APayloadAction>
export { ASimpleAction, APayloadAction }
If you have many actions in a file, separating the action construction and the TS type declarations aids readability. You will soon realise if you've forgotten the TS type when you come to use it in reducers, tests etc.
const ASimpleAction = a('SIMPLE/ACTION')
const APayloadAction = a('PAYLOAD/ACTION', {} as { str: string })
const AOtherPayloadAction = a('PAYLOAD/OTHER_ACTION', {} as { num: number })
type ASimpleAction = ReturnType<typeof ASimpleAction>
type APayloadAction = ReturnType<typeof APayloadAction>
type AOtherPayloadAction = ReturnType<typeof AOtherPayloadAction>
export { ASimpleAction, APayloadAction, AOtherPayloadAction }
Depending on your tslint config, the payload object can either be written as
{} as { str: string }
or
<{ str: string }>{}
Clone repo and install dependencies using NPM:
npm i
Source for a
can be found in src/a.ts
.
Run the demo redux app using:
npm start
MIT. See the LICENSE file for more information.
Remember to update the CHANGELOG and bump the version number
using npm version patch
, npm version minor
, or npm version major
. Publish
to registry using npm publish --access public
.
This project was inspired by Nicholas Jamieson's ts-action package. Many thanks to him and for his swift answers to questions.
Also thanks to the person who wrote this post, you set us on a path to understanding more about Typescript.
FAQs
Typesafe Redux action creation utility
We found that @artossystems/a demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 10 open source maintainers 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
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
Research
/Security News
Ongoing npm supply chain attack spreads to DuckDB: multiple packages compromised with the same wallet-drainer malware.