redux-either
FSA-compliant either monad middleware for Redux.
This is based on redux-future.
npm install --save redux-either
Usage
import eitherMiddleware from 'redux-either';
import { Either } from 'ramda-fantasy';
const createStoreWithMiddleware = applyMiddleware(
eitherMiddleware( Either
, Either.either
)(createStore)
import eitherMiddleware from 'redux-either';
import { Either } from 'ramda-fantasy';
const createStoreWithMiddleware = applyMiddleware(
eitherMiddleware( Either
, (l, r, e) => e.fold(l, r)
)
)(createStore)
Example
import Either from 'data.either';
store.dispatch({
type: 'ADD'
, either: Either.Right(2)
});
Using in combination with redux-actions
Because it supports FSA actions, you can use redux-either in combination with redux-actions.
Example: Action creators
const action = createAction('FSA_ACTION');
store.dispatch(action(R.map(R.toUpper, Either.Right('test'))));
store.dispatch(action(R.map(R.toUpper, Either.Left('error'))));
Example: Future(Either)
You can use redux-either
together with redux-future
.
const futureEither = new Future((reject, resolve) => {
fetch('users', (err, res) =>
err
? reject(err)
: res.length <= 0
? resolve(Either.Left('no users found'))
: resolve(Either.Right(res)));
});
const action = createAction('FSA_ACTION');
store.dispatch(action(futureEither));
Related
What's an Either?
Libraries