Gathering detailed insights and metrics for @cromwellian/redux-promise-middleware-actions
Gathering detailed insights and metrics for @cromwellian/redux-promise-middleware-actions
Gathering detailed insights and metrics for @cromwellian/redux-promise-middleware-actions
Gathering detailed insights and metrics for @cromwellian/redux-promise-middleware-actions
Redux action creator for making async actions compatible with redux-promise-middleware
npm install @cromwellian/redux-promise-middleware-actions
Typescript
Module System
Node Version
NPM Version
71.8
Supply Chain
99.3
Quality
75.1
Maintenance
100
Vulnerability
100
License
TypeScript (100%)
Total Downloads
839
Last Day
1
Last Week
8
Last Month
13
Last Year
137
MIT License
12 Commits
2 Watchers
4 Branches
1 Contributors
Updated on Nov 12, 2018
Minified
Minified + Gzipped
Latest Version
2.1.1
Package Id
@cromwellian/redux-promise-middleware-actions@2.1.1
Unpacked Size
26.58 kB
Size
7.32 kB
File Count
23
NPM Version
6.4.1
Node Version
10.13.0
Cumulative downloads
Total Downloads
Last Day
0%
1
Compared to previous day
Last Week
100%
8
Compared to previous week
Last Month
-55.2%
13
Compared to previous month
Last Year
4.6%
137
Compared to previous year
7
Create Redux actions with a type
and payload
in a standardized way. Inspired by redux-actions but simpler and with special support for asynchronous actions (promises).
Has no dependencies and is tiny (~680 bytes gzipped). First class TypeScript support.
Works with redux-promise-middleware to handle asynchronous actions by dispatching pending
, fulfilled
and rejected
events based on the state of the input promise.
Goals of this library:
Note: If you are using TypeScript this library requires TypeScript 3. For TypeScript 2 use version 1 of this library.
You need to install this library as well as redux-promise-middleware.
npm install redux-promise-middleware-actions redux-promise-middleware
Include redux-promise-middleware when you create your store:
1import promiseMiddleware from 'redux-promise-middleware'; 2 3composeStoreWithMiddleware = applyMiddleware( 4 promiseMiddleware(), 5)(createStore);
Synchronous actions works exactly like redux-actions. You supply a function that returns whatever payload the action should have (if any).
1import { createAction } from 'redux-promise-middleware-actions'; 2 3export const foo = createAction('FOO', (num) => num); 4 5dispatch(foo(5)); // { type: 'FOO', payload: 5 }
When handling the action in a reducer, you simply cast the action function to a string to return the type. This ensures type safety (no spelling errors) and you can use code navigation to find all uses of an action.
1const fooType = String(foo); // 'FOO'
When you create an asynchronous action you need to return a promise payload. If your action is called FOO
the following events will be dispatched:
FOO_PENDING
is dispatched immediatelyFOO_FULFILLED
is dispatched when the promise is resolved
FOO_REJECTED
is dispatched instead if the promise is rejected1import { createAsyncAction } from 'redux-promise-middleware-actions'; 2 3export const fetchData = createAsyncAction('FETCH_DATA', async () => { 4 const res = await fetch(...); 5 return res.json(); 6}); 7 8dispatch(fetchData()); // { type: 'FETCH_DATA_PENDING' }
An async action function has three properties to access the possible outcome actions: pending
, fulfilled
and rejected
. You can dispatch them directly (in tests etc.):
1dispatch(fetchData.pending()); // { type: 'FETCH_DATA_PENDING' } 2dispacth(fetchData.fulfilled(payload)); // { type: 'FETCH_DATA_FULFILLED', payload: ... } 3dispacth(fetchData.rejected(err)); // { type: 'FETCH_DATA_REJECTED', payload: err, error: true }
But normally you only need them when you are writing reducers:
1case String(fetchData.pending): // 'FETCH_DATA_PENDING'
2case String(fetchData.fulfilled): // 'FETCH_DATA_FULFILLED'
3case String(fetchData.rejected): // 'FETCH_DATA_REJECTED'
Note that if you try and use the base function in a reducer, an error will be thrown to ensure you are not listening for an action that will never happen:
1case String(fetchData): // throws an error
You can now handle the different events in your reducer by referencing the possible outcome states:
1import { fetchData } from './actions'; 2 3export default (state, action) => { 4 switch (action.type) { 5 case String(fetchData.pending): 6 return { 7 ...state, 8 pending: true, 9 }; 10 case String(fetchData.fulfilled): 11 return { 12 ...state, 13 data: action.payload, 14 error: undefined, 15 pending: false, 16 }; 17 case String(fetchData.rejected): 18 return { 19 ...state, 20 error: action.payload, 21 pending: false, 22 }; 23 default: 24 return state; 25 } 26};
It can get tedious writing the same reducer for every single async action so we've included a simple reducer that does the same as the example above:
1import { asyncReducer } from 'redux-promise-middleware-actions'; 2import { fetchData } from './actions'; 3 4export default asyncReducer(fetchData);
You can also combine it with an existing reducer:
1import { asyncReducer } from 'redux-promise-middleware-actions'; 2import { fetchData } from './actions'; 3 4const fetchReducer = asyncReducer(fetchData); 5 6export default (state, action) => { 7 const newState = fetchReducer(state, action); 8 9 switch (action.type) { 10 case 'SOME_OTHER_ACTION': 11 return { ... }; 12 default: 13 return newState; 14 } 15};
You can add metadata to any action by supplying an additional metadata creator function. The metadata creator will receive the same arguments as the payload creator:
createAction(type, payloadCreator, metadataCreator)
1export const foo = createAction( 2 'FOO', 3 (num) => num, 4 (num) => num + num 5); 6 7dispatch(foo(5)); // { type: 'FOO', meta: 10, payload: 5 }
createAsyncAction(type, payloadCreator, metadataCreator)
1export const fetchData = createAsyncAction( 2 'FETCH_DATA', 3 (n: number) => fetch(...), 4 (n: number) => ({ n }) 5); 6 7dispatch(fetchData(42)); 8// { type: 'FETCH_DATA_PENDING', meta: { n: 42 } } 9// { type: 'FETCH_DATA_FULFILLED', meta: { n: 42 }, payload: Promise<...> } 10// { type: 'FETCH_DATA_REJECTED', meta: { n: 42 }, payload: Error(...) }
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
no SAST tool detected
Details
Reason
Found 0/12 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
38 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-06-16
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More