Skip to content

Instantly share code, notes, and snippets.

@threepointone
Created February 15, 2016 20:03
Show Gist options
  • Save threepointone/bef4a28d577707af38b2 to your computer and use it in GitHub Desktop.
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
// 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