Installations
npm install redux-redents
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
6.2.0
NPM Version
3.8.9
Score
71.4
Supply Chain
97.3
Quality
74.9
Maintenance
50
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (95.97%)
CSS (2.92%)
HTML (1.11%)
Total Downloads
Cumulative downloads
Total Downloads
2,231
Last day
0%
1
Compared to previous day
Last week
80%
9
Compared to previous week
Last month
233.3%
20
Compared to previous month
Last year
-31.7%
97
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Middlewares and Reducers for Redux
Motivation
Once you're required to fetch data from the server in your Redux applications, you need to either write a custom Middleware or use redux-* modules to fetch data. Anyway you need to implement actions creators, reducers or both. If your server provides rest api for data manipulation the implementation of anything becomes boring very quickly. And Redux-redents to the rescue!!!
The module contains the implementation of the:
- Promise middleware, supports fetching from and pulling to server data
- Chain middleware, supports call chains for chaining you actions.
- Dictionary reducers, provide convention based reducers to reduce server responses
You don't need to implement actions, just add reducers to the rootreducer and add middlewares to the store
Installation
1npm install redux-redents
Quick sample
1//file index.js 2//imports 3import {createStore, applyMiddlware} from 'redux'; 4import {Provider} from 'react-redux'; 5import {render} from 'react-dom'; 6import {dictionary_reducers,promiseMiddleware,chainMiddleware,createEntityOperation} from 'redux-redents'; 7import {Plants} from './plants'; 8 9//specify entities 10const plants = { 11 fruit: {}, 12 vegetable: {} 13} 14const EntConfig = { 15 defaults : { 16 baseUrl : 'http://server/uri' 17 } 18 entities : plants 19} 20 21//create reducers 22const dicts = dictionary_reducers(plants); 23//create store 24const store = applyMiddlware(promiseMiddleware,chainMiddleware)(createStore)(dicts); 25 26render(<Provider store={store})><Plants/></Provider>,document.getElemenentById('app')); 27 28//file plants.js 29 30 31export class Plants extends Component { 32 static propTypes = { 33 plants: propTypes.array.isRequired, 34 plant: propTypes.object.isRequired 35 } 36 componentDidMount() { 37 this.props.actions.entOper('fruit','index'); //load fruits list 38 this.props.actions.entOper('fruit','get','apple'); //load an apple 39 } 40 return ( 41 <div> 42 <ul> 43 {this.props.plants.map((cur) => <li>{cur.name}</li>)} //display list of the plants 44 </ul> 45 <h3>{this.props.plant.name}</h3>//diplay selected plant 46 <div> 47 ); 48} 49 50//connect props and actions to the Component 51function mapStateToProps(state) { 52 return { 53 plants: state.fruits, //connect fruits reducer to the plants property 54 plant: state.fruit 55 }; 56} 57 58function mapDispatchToProps(dispatch) { 59 return { 60 actions: bindActionCreators({entOper:createEntityOperation(EntConfig)}, dispatch) //binds actions.entOper to the call of the entityOperation function 61 }; 62} 63 64export default connect( 65 mapStateToProps, 66 mapDispatchToProps 67)(GenTransactionsPage);
Full sample is available in the client-demo folder
Conventions and defaults
Redux-redents uses Entities object to get information about entities names, operations supported and their data.
Entities configuration by conventions
Entities type structure:
1entities = { 2 defaults: { 3 baseUrl: //default baseUrl of the server endpoint 4 }, 5 entities : { 6 entityName : { 7 operation1: { 8 url: //operation url, 9 request: function(data) {return request object} //- function to produce request to perform operation1 10 } 11 } 12 } 13}
Default operations supported are:
- index - fetches the arrays of Entities.
- By convention uses the url
entities.defaults.baseUrl+"/"+entityName+"s"
- By convention uses the get method
- By convention uses the url
- get - fetches one entity
- By convention uses the url
entities.defaults.baseUrl+"/"+entityName+"s"+"/"+data
data is passed from the client code - By convention uses the get method
- By convention uses the url
- post - saves the entity data to the server
- By convention uses the url
entities.defaults.baseUrl+"/"+entityName+"s"
- By convention uses the post method
- By convention passes the data as the json body
- By convention uses the url
- delete - removes the entity from the server
- By convention uses the url
entities.defaults.baseUrl+"/"+entityName+"s"
- By convention uses the delete method
- By convention uses the url
You can override an operation and inside the operation you could override either the url, or the request. The request field of the entity object should be the function:
1function(data) { return promise}
It accepts data from the client code and returns promise that returns the result
Reducers by default
Redux-redents has the function that accepts Entities configuration and generates reducers for index and get operations for each entity mentioned in the configuration.
1dictionary_reducers({ent1:{},ent2{}}) 2produces 3{ 4 ent1 : function(state,action), //get ent1 5 ent1s : function(state,action), //index ent1 6 ent2 : function(state,action), //get ent2 7 ent2s : function(state,action), //index ent2 8}
Actions creator for promises
To emit actions that will fetch data from the server Redux-redents contains the entityOperation function
javascript function entityOperation(entity,operation,data,chainlink)
- entity - entity name to operate on
- operation - operation name to perform
- data - optional data required for operations. By convention data is used for
- 'get' - will be added to the url as
url/id
- 'post' - will be sent as request body
- 'delete' - will be added to the url as
url/id
- 'get' - will be added to the url as
- chainlink - optional action to perform after the current action will be finished. Action is the function that accepts the current action.res or whole action if its .res field is not defined
Promise Middleware
Promise Middleware accepts actions with promise dispatches original action with modified type (type+'_REQUEST') and adds promise callbacks that emit original actions with response from the server in the res field.
Chain Middleware
If you need emit an action after another will be returned data from the server, you could use the chainlink parameter of the entityOperation. The chainlink is the function that accepts one parameter.
- chainMiddleware only calls the function if original action has status field equal to 'done' (action.status=='done'). * chainMiddleware passes server response (action.res) to that function or original action if it doesn't have res field
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: Apache License 2.0: LICENSE:0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/23 approved changesets -- score normalized to 0
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Score
3
/10
Last Scanned on 2025-01-27
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