redux-aar
Version:
Actions creator and reducers utilities for redux
137 lines (136 loc) • 4.41 kB
TypeScript
import { Action as ReduxAction, Reducer } from "redux";
export declare type Action<T = any> = ReduxAction<string> & {
payload: T;
};
export declare type ActionCreatorReturn<T> = (T extends undefined ? EmptyActionFn : T extends boolean ? T extends null ? ActionFn<null> : ActionFn<boolean> : ActionFn<T>) & ReduxAction<string>;
export declare type ActionFn<T> = (payload: T) => Action<T>;
export declare type EmptyActionFn = () => Action;
export declare type EmptyAction = ReduxAction<string>;
export declare type ReducerMapperFn<S, P> = (state: S, payload: P) => S;
export interface IReducerLike<InitialState> {
/**
* Binds a reducing function to an action. The function will be invoked only if the bound action is dispatched.
*
* ```ts
* import * as reduxaar from 'redux-aar';
*
* const update = reduxaar.createAction<number>('update');
* const update = reduxaar.createAction('reset');
* const initialState = () => ({ counter: 0 });
* const reducer = createReducer(initialState());
*
* reducer
* .on(reset, initialState)
* .on(update, (state, counter) => ({ counter }))
* ;
* ```
*
* @template T
* @param {ActionCreatorReturn<T>} action
* @param {ReducerMapperFn<InitialState, T>} mapper
* @returns {IReducerLike<T>}
* @memberof IReducerLike
*/
on<T>(action: ActionCreatorReturn<T>, mapper: ReducerMapperFn<InitialState, T>): IReducerLike<InitialState>;
/**
* Returns a valid `Reducer<InitialState>` for redux `combineReducers` function.
*
* ```ts
* // counter.ts
* import * as reduxaar from 'redux-aar';
*
* const decrease = reduxaar.createAction('decrease');
* const increase = reduxaar.createAction('increase');
* const initialState = () => ({ counter: 0 });
* const reducer = createReducer(initialState());
*
* reducer
* .on(decrease, state => ({ counter: state.counter-- }))
* .on(increase, state => ({ counter: state.counter++ }))
* ;
*
* export default reducer.reduce();
*
* // store.ts
* import * as redux from 'redux';
* import counter from './counter';
*
* const store = redux.createStore(
* redux.combineReducers(
* {
* counter,
* }
* )
* );
*
* export default store;
* ```
*
* @returns {Reducer<InitialState>}
* @memberof IReducerLike
*/
reduce(): Reducer<InitialState>;
}
/**
* Creates an action. If the type `T` is specified, it will return `ActionFn<T>` otherwise `EmptyActionFn`.
*
* ```ts
* const reset = createAction('reset');
* const setName = createAction<string>('setName');
*
* dispatch(reset());
* dispatch(setName('foobarbaz'));
* ```
*
* @export
* @template T
* @param {string} type
* @returns {ActionCreatorReturn<T>}
*/
export declare function createAction<T = undefined>(type: string): ActionCreatorReturn<T>;
/**
* Creates a reducer. A Reducer handles all mutations to a state when some actions are dispatched.
* It takes only one argument which is the initial state to mutate.
* To use a created reducer in redux, you have to use the method `reduce` with redux's `combineReducer` function.
*
* ```ts
* import { createReducer } from 'redux-aar';
*
* const initialState = () => ({ counter: 0 });
* const reducer = createReducer(initialState());
*
* export default reducer.reduce();
* ```
*
* @export
* @template S
* @param {S} initialState
* @returns {IReducerLike<InitialState>}
*/
export declare function createReducer<S>(initialState: S): IReducerLike<S>;
/**
* Creates a prefix and returns a `createAction` function.
* This is useful if you want to scope all your actions under a given prefix.
* Use this if you are writing a domain driven store.
*
* ```ts
* const createAction = prefix('user');
*
* const reset = createAction('reset');
* const setName = createAction<string>('setName');
*
* reset().type // 'user::reset'
* setName('lorem ipsum').type // 'user::setName'
* ```
*
* @export
* @param {string} prefix
* @returns {typeof createAction}
*/
export declare function prefix(prefix: string): typeof createAction;
declare const _default: {
createAction: typeof createAction;
createReducer: typeof createReducer;
prefix: typeof prefix;
};
export default _default;