Skip to content

Instantly share code, notes, and snippets.

@jwarby
Last active October 6, 2016 00:27
Show Gist options
  • Save jwarby/7ab709c80701ce349e1e191bc795deda to your computer and use it in GitHub Desktop.
Save jwarby/7ab709c80701ce349e1e191bc795deda to your computer and use it in GitHub Desktop.
A middleware for Redux which verifies that all actions dispatched to the store follow the Flux Standard Action (https://github.com/acdlite/flux-standard-action) pattern. A warning is shown (only once) for each action which violates the pattern.
import { isFSA } from 'flux-standard-action'
function createFSAComplianceMiddleware() {
const alreadyWarnedAbout = []
return ({ dispatch, getState }) => next => action => {
const { type } = action
if (!isFSA(action) && alreadyWarnedAbout.indexOf(type) === -1) {
// eslint-disable-next-line no-console
console.warn(`${type} does not conform to the Flux Standard Action pattern`)
alreadyWarnedAbout.push(type)
}
return next(action)
}
}
const fsaCompliance = createFSAComplianceMiddleware()
export default fsaCompliance
@jwarby
Copy link
Author

jwarby commented Oct 6, 2016

Usage

import { applyMiddleware, createStore } from 'redux'
import fsaCompliance from 'path/to/fsaCompliance.js'

/* ...snip... */
let store = createStore(
  rootReducer,
  applyMiddleware(
    /* ...snip other middleware... */
   fsaCompliance
  )
)

Additional dependencies

Notes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment