Created
February 15, 2016 20:03
-
-
Save threepointone/bef4a28d577707af38b2 to your computer and use it in GitHub Desktop.
middleware that ensures actions have flux-standard-action shape, but also allows an `optimist` key
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// like https://github.com/meadow/redux-ensure-fsa, but allows `optimist` as a key | |
import isPlainObject from 'lodash.isplainobject'; | |
const validKeys = [ | |
'type', | |
'payload', | |
'error', | |
'meta', | |
'optimist' | |
]; | |
function isValidKey(key) { | |
return validKeys.indexOf(key) > -1; | |
} | |
export function isFSA(action) { | |
return isPlainObject(action) && | |
typeof action.type !== 'undefined' && | |
Object.keys(action).every(isValidKey); | |
} | |
export default function ensureFSAMiddleware () { | |
return next => action => { | |
if (!isFSA(action)) { | |
console.error(action); // eslint-disable-line | |
throw new Error('Flux Standard Action Violation: Actions must only have type, payload, error, optimist, or meta properties.'); | |
} | |
return next(action); | |
}; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment