redux-grim
Version:
Generator for asynchronous Redux endpoint actions and reducers
26 lines (24 loc) • 924 B
JavaScript
import normalize from './normalize';
import processRules from './processRules';
import { isSuccessType, isSetType } from '../util';
/**
* Create Redux middleware function which normalizes entities
*/
export default function getNormalizerMiddleware(arrayRules, callback) {
const rules = processRules(arrayRules);
return () => next => action => {
// Only normalize success actions
if (isSuccessType(action.type) || isSetType(action.type)) {
// entityType is added by autogenerated actions. Normalization only applies
// to autogenerated components.
const { payload, meta } = action;
const entityType = meta && meta.entityType;
if (entityType && payload && rules[entityType]) {
meta.entities = {};
action.payload = normalize(rules, entityType, payload, meta.entities);
callback && callback(entityType, payload);
}
}
return next(action);
};
}