https://github.com/budarin/redux-business-logic-middleare
Middleware for processing business login in redux application
https://github.com/budarin/redux-business-logic-middleare
Last synced: about 2 months ago
JSON representation
Middleware for processing business login in redux application
- Host: GitHub
- URL: https://github.com/budarin/redux-business-logic-middleare
- Owner: budarin
- License: mit
- Created: 2021-06-13T12:13:51.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2024-11-19T01:10:36.000Z (7 months ago)
- Last Synced: 2025-03-24T09:04:05.533Z (2 months ago)
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/@budarin/redux-business-logic-middleare
- Size: 371 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# redux-business-logic-middleare
Middleware for processing business login in redux application.
## Instllation
Using npm
```shell
npm install --save-dev @budarin/redux-business-logic-middleare
```Using yarn
```shell
yarn add -D @budarin/redux-business-logic-middleare
```## Using:
Let's implement a simple business rule for Todo: “when creating a todo, send the todo to the server and to the redux store”.
Let's describe the essence of Todo — its constants, actions, business rules and a redeser in accordance with the concept of [ducks](https://github.com/erikras/ducks-modular-redux).
`./ducks/todo.js`
```js
import { sendTodo } from 'src/services/api'
import { onAction } from'@budarin/redux-business-logic-middleare';const SET_ERROR = 'TODO/SET_ERROR';
export const ADD_TODO = 'TODO/ADD_TODO';export const setError = (error) => ({
type: SET_ERROR,
payload: error
})export const addTodo = ( todo ) => ({
type: ADD_TODO,
payload: todo
});// our business rule
export const addTodoMiddleware = async (store, next, action) => {
// call the API method to send todo to the server
try {
await sendTodo(action.payload);
} catch(error) {
return store.dispatch(setError(error));
}// otherwise, we pass the action to the next middleware
return next(action);
}// let's add the business rule
onAction(ADD_TODO, addTodoMiddleware)export default const reducer = (state = initialState, action) => {
switch (action.type) {
case ADD_TODO: {
...
}
case SET_ERROR {
...
}
default:
return state;
}
};
```Add midleware to stores middlewares
```js
import todoReducer from '../ducks/todo.js'
import { createStore, applyMiddleware, combineReducers } from 'redux'
import { bussinesLogicMiddleware } from '@budarin/redux-business-logic-middleare';const reducers = combineReducers(
...
todoReducer,
...
);const store = createStore(
reducers,
initialState,
applyMiddleware(bussinesLogicMiddleware)
);
```To remove bussines-rule from processing
```js
import { ADD_TODO } from '../ducks/todo.js'
import { offAction } from '@budarin/redux-business-logic-middleare';offAction(ADD_TODO);
```
To remove all bussines-rules from processing```js
import { removeAllbusinessRules } from '@budarin/redux-business-logic-middleare';removeAllbusinessRules();
```