项目作者: rnsloan

项目描述 :
Redux middleware. Callbacks using allowlisting or denylisting of actions
高级语言: JavaScript
项目地址: git://github.com/rnsloan/redux-allow-deny.git
创建时间: 2015-11-22T23:50:41Z
项目社区:https://github.com/rnsloan/redux-allow-deny

开源协议:MIT License

下载


redux-allow-deny

Redux middleware to execute a callback on action types using a allowlist or denylist approach

Installation

npm install --save redux-allow-deny

Usage

Both the allowlist and denylist methods expect two parameters:

  1. Actions (array) array of action types (string) to check against
  2. callback (function)

The callback is passed two parameters:

  1. Action (object) the current action
  2. state (object) the state object with the methods getState and dispatch

Example

Note: recommended to move the middleware creation into a separate file

  1. import { createStore, combineReducers, applyMiddleware, compose } from 'redux'
  2. import * as reducers from '../reducers'
  3. import * as wb from 'redux-allow-deny'
  4. import {
  5. CREATE_SHORTLIST,
  6. EDIT_SHORTLIST,
  7. } from '../constants'
  8. const reducer = combineReducers(reducers)
  9. //the callback function passed action and state
  10. function logShortlistActions(action, state) {
  11. console.log(`ACTION: ${action.type}`)
  12. }
  13. //create middleware to execute the callback if an Action has type 'CREATE_SHORTLIST' or 'EDIT_SHORTLIST'
  14. const allowlist = wb.allowlist([CREATE_SHORTLIST, EDIT_SHORTLIST], logShortlistActions)
  15. // standard redux boilerplate:
  16. const createStoreWithMiddleware = compose(
  17. applyMiddleware(allowlist)
  18. )(createStore)
  19. export default function configureStore(initialState) {
  20. return createStoreWithMiddleware(reducer, initialState)
  21. }

denylist method works exactly the same

  1. const denylist = wb.denylist([CREATE_SHORTLIST, EDIT_SHORTLIST], logActionIfNotShortlist)

The methods can be exported individually:

  1. import {allowlist} from "redux-allow-deny"
  2. const allowlistMiddleware = allowlist([ACTION_1, ACTION_2], callback)

Credit

Inspired by: github.com/michaelcontento/redux-storage