Gathering detailed insights and metrics for react-redux-gen
Gathering detailed insights and metrics for react-redux-gen
Gathering detailed insights and metrics for react-redux-gen
Gathering detailed insights and metrics for react-redux-gen
redux-file-gen
Generates necessary files for react-redux application
auto-gen-react
create multiple app based es6+webpack, eg: react, react+redux
generator-redux
CLI for Redux: next-gen functional Flux/React with devtools
reactor-gen
React template generator cli for plugins like redux, router etc.
Generate actions and reducers based on naming convention to communicate with the api's using react with redux in one line
npm install react-redux-gen
Typescript
Module System
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
2 Stars
39 Commits
2 Watchers
17 Branches
1 Contributors
Updated on Mar 05, 2023
Latest Version
0.1.6
Package Id
react-redux-gen@0.1.6
Unpacked Size
31.46 kB
Size
9.95 kB
File Count
4
NPM Version
6.4.1
Node Version
10.12.0
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
You can find this package on NPM
Yarn:
yarn react-redux-gen
NPM:
npm install react-redux-gen
Redux-gen will generate actions, thunk and reducers in one line using naming conventions and API based.
You can use fully if you have an standard object CRUD, like an User, or use the plain actions and extend with your async actions.
This is based on Redux. If this is too advanced for you, and you need to know more in how to use redux, please check the guides: https://redux.js.org/recipes/reducing-boilerplate
Redux Gen generate actions and reducers based on convention, and there's many use cases to use the way you want
As the application increase, the process of create actions and reducers is pretty repetitive and we can get a lot of benefits if we use the same language that API REST naming conventions if we are connecting on an API.
You can use these three utility functions from react-redux-gen:
import { genActionNames, genPlainActions, genAsyncActions } from 'react-redux-gen'
Returns the action names in a object so you can use to be referecend later, like on reducers
genActionNames(entity, types, states)
Parameter | Type | Description | Default |
---|---|---|---|
entity | String | The entity name | There's no default for this one |
types | Array | Types of action | Array('create', 'update', 'delete', 'list', 'fetch') |
states | Array | States of request | Array('REQUESTED', 'SUCCESS', 'ERROR') |
Returns the action functions in a friendly way to be used to dispatch actions
genPlainActions(entity, types, states)
Parameter | Type | Description | Default |
---|---|---|---|
entity | String | The entity name | There's no default for this one |
types | Array | Types of action | Array('create', 'update', 'delete', 'list', 'fetch') |
states | Array | States of request | Array('REQUESTED', 'SUCCESS', 'ERROR') |
For REQUESTED
state, it will return the following action object:
{ type: 'ENTITY_TYPE_REQUESTED', completed: false, error: false }
For SUCCESS
state, it will return the following action object:
{ type: 'ENTITY_TYPE_SUCCESS', completed: true, data: data, error: false }
For ERROR
state, it will return the following action object:
{ type: 'ENTITY_TYPE_ERROR', completed: true, error: error }
Returns the async action standard function to communicate with rest API. These functions make calls to api's based on the base URL, dispatching request action, success when succeed, and error when the the call to the api fails. We use axios to make our calls.
genAsyncActions(entity, url, headers, types, states)
Parameter | Type | Description | Default |
---|---|---|---|
entity | String | The entity name | There's no default for this one |
url | String | The base url to call | There's no default for this one |
headers | Object | Extra headers | There's no default for this one |
types | Array | Types of action | Array('create', 'update', 'delete', 'list', 'fetch') |
states | Array | States of request | Array('REQUESTED', 'SUCCESS', 'ERROR') |
This is a basic example to use to define the CRUD actions for your user
object
import { genPlainActions, genAsyncActions } from 'react-redux-gen'
const userActions = genPlainActions('user')
const userAsyncActions = genAsyncActions('user', '/users)
export { userActions, userAsyncActions }
This code above will generate the actions needed with states REQUESTED
, SUCCESS
and ERROR
for actions and the async actions with the basic calls from these states.
{
'create': ['CREATE_USER_REQUESTED', 'CREATE_USER_SUCCESS', 'CREATE_USER_ERROR'],
'update': ['UPDATE_USER_REQUESTED', 'UPDATE_USER_SUCCESS', 'UPDATE_USER_ERROR'],
'delete': ['DELETE_USER_REQUESTED', 'DELETE_USER_SUCCESS', 'DELETE_USER_ERROR'],
'list': ['LIST_USER_REQUESTED', 'LIST_USER_SUCCESS', 'LIST_USER_ERROR'],
'fetch': ['FETCH_USER_REQUESTED', 'FETCH_USER_SUCCESS', 'FETCH_USER_ERROR'],
}
Our generated async actions is like this:
import axios from 'axios'
import { genPlainActions, genActionNames } from 'react-redux-gen'
const actions = genPlainActions('user', '/authenticated', ['action'])
// [0] => REQUESTED, [1] => SUCCESS, [2] => ERROR
const example = () => {
return dispatch => {
dispatch(actions.action[0]())
return axios
.get('/authenticated')
.then( response => {
return dispatch(actions.action[1](response.data))
}).catch( e => {
return dispatch(actions.action[2](e))
})
}
}
The async actions is still experimental and we don't support much customization for these case, and we hope to improve this for new releases.
With the async object is possible to dispatch actions from this call, for example:
genAsyncActions('user', 'http://example.com/user/')['create']({name: 'jonh doe'})
Will dispatch
{ type: 'CREATE_USER_REQUESTED', completed: false }
, { type: 'CREATE_USER_SUCCESS', error: false, completed: true, data: {name: 'John doe'} }
,
and for an error on create:
will generate { type: 'CREATE_USER_REQUESTED', completed: false }
, { type: 'CREATE_USER_ERROR', error: {}, completed: true }
If you want to pass headers to axios, you should run like this example:
import { genPlainActions, genAsyncActions } from 'react-redux-gen'
const headers = {
'Authorization': `Basic ${process.env.REACT_APP_SECRET}`,
'Content-Type': 'application/json'
}
const userActions = genPlainActions('user')
const userAsyncActions = genAsyncActions('user', '/users', headers)
export { userActions, userAsyncActions }
import { genReducer } from 'react-redux-gen'
const user = genReducer('user', { data: {}, error: false, completed: true }) // the object and initial state
export default user
If you want to use just the actions and use your own async actions, feel free to do like the example below for a login action:
1import { genPlainActions, genActionNames } from 'react-redux-gen' 2import { host } from '../url' 3 4import Auth from '../modules/Auth' 5 6const headers = { 7 'Authorization': `Bearer ${Auth.getToken()}`, 8 'Content-Type': 'application/json' 9} 10 11const loginActionNames = genActionNames('user', ['logged', 'login', 'logout']) 12const loginActions = genPlainActions('user',['logged', 'login', 'logout', 'register']) 13 14const logged = () => { 15 return dispatch => { 16 dispatch(loginActions.logged[0]()) 17 return axios 18 .get(host + '/authenticated', { headers: { 19 'Authorization': `Bearer ${Auth.getToken()}`, 20 'Content-Type': 'application/json' 21 } 22 }) 23 .then( response => { 24 return dispatch(loginActions.logged[1](response.data)) 25 }).catch( e => { 26 return dispatch(loginActions.logged[2](e)) 27 }) 28 } 29} 30 31const login = (user) => { 32 return dispatch => { 33 dispatch(loginActions.login[0]()) 34 return axios 35 .post(host + '/authorize/local', user) 36 .then( response => { 37 return dispatch(loginActions.login[1](response.data)) 38 }).catch( e => { 39 return dispatch(loginActions.login[2](e)) 40 }) 41 } 42} 43 44const logout = () => { 45 return (dispatch) => { 46 dispatch(loginActions.logout[0]()) 47 if(Auth.deauthenticateUser()) { 48 return dispatch(loginActions.logout[1]({})) 49 } 50 return dispatch(loginActions.logout[2](new Error('we have an error to logout, try again later'))) 51 } 52} 53 54const register = (user) => { 55 return dispatch => { 56 dispatch(loginActions.register[0]()) 57 return axios 58 .post(host + '/auth/register', user) 59 .then( response => { 60 return dispatch(loginActions.register[1](response.data)) 61 }).catch( e => { 62 return dispatch(loginActions.register[2](e)) 63 }) 64 } 65} 66 67 68 69export { loginActionNames, loginActions, logged, login, logout, register }
For a login reducer, we could use
import {
loginActionNames
} from '../actions/login'
const login = (state = { completed: true, data: {}, error: false }, action) => {
switch (action.type) {
case loginActionNames.logged[0]:
return { ...state, completed: action.completed }
case loginActionNames.logged[1]:
return { ...state, completed: action.completed, data: action.data, error: false }
case loginActionNames.logged[2]:
return { ...state, completed: action.completed, error: action.error }
case loginActionNames.logout[0]:
return { ...state, completed: action.completed, error: action.error }
case loginActionNames.logout[1]:
return { ...state, completed: action.completed, error: action.error, data: action.data }
case loginActionNames.logout[2]:
return { ...state, completed: action.completed, error: action.error }
default:
return state
}
}
export default login
You can check our actions and reducers from a project using react-redux-gen
See full example on a real project
yarn install
yarn start
yarn test
This package is maintained by Alexandre Magno
This open source library is licensed under MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
no SAST tool detected
Details
Reason
Found 0/30 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
81 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-14
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