Home

Awesome

redux-convention

Travis build status NPM version

Facilitates conversion of Flux Standard Action (FSA) to Canonical Composition Action (CCA) and vice-versa.

Use Case

redux-convention is used to convert between two competing standards: Flux Standard Action and Canonical Composition Action. The two standards can be used together in the same project, e.g. the main project might use CCA, but it can rely on middleware that is designed for FSA. redux-convention is used to convert actions between the two standards.

FSA is the more popular standard. There is a number of libraries that implement FSA and a lot more that unofficially implement FSA in full or in part.

CCA is part of a broader standard called Canonical Reducer Composition (CRC). The aim of CRC is to standardize Redux application composition. CCA is a variation of FSA using different semantics. redux-immutable is an example of a library that implements CCA standard.

Usage

Function

redux-convention exposes mapper object that in turn defines two functions: fromCCAtoFSA and fromFSAtoCCA. Input is a plan object. Output is a shallow copy of the input with mapped properties.

import {
    mapper
} from 'redux-convention';

let CCAction,
    FSAction;

CCAction = {
    name: 'foo',
    data: {
        foo: 'bar'
    },
    metadata: {
        baz: 'qux'
    }
};

FSAction = mapper.fromCCAtoFSA(CCAction);
CCAction = mapper.fromFSAtoCCA(FSAction);

Middleware

redux-convention exposes middleware object that in turn defines two functions: fromCCAtoFSA and fromFSAtoCCA. These functions are designed to work with Redux middleware, specifically, the applyMiddleware function.

Middleware can be used multiple times to convert FSA to CCA.

import {
    createStore,
    applyMiddleware
} from 'redux';

import {
    middleware as convention
} from 'redux-convention';

import {
     combineReducers
} from 'redux-immutable';

import * as reducers from './reducers';

import Immutable from 'immutable';

let reducer,
    state,
    store;

reducer = combineReducers(reducers);

state = Immutable.Map({});

state = reducer(state, {
    name: `CONSTRUCT`
});

store = applyMiddleware(
    // Middleware that uses CCA.
    convention.fromCCAtoFSA,
    // Middleware that uses FSA.
    convention.fromFSAtoCCA,
    // Middleware that uses CCA.
)(createStore)(reducer, state);

export default store;