// Type definitions for redux-pack 0.1 // Project: https://github.com/lelandrichardson/redux-pack // Definitions by: tansongyang // dschuman // pweinberg // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.4 import { Action as ReduxAction, Middleware, Reducer } from 'redux'; export const KEY: { readonly LIFECYCLE: 'redux-pack/LIFECYCLE'; readonly TRANSACTION: 'redux-pack/TRANSACTION'; }; export const LIFECYCLE: { readonly START: 'start'; readonly SUCCESS: 'success'; readonly FAILURE: 'failure'; }; export type LIFECYCLEValues = 'start' | 'succes' | 'failure'; export const middleware: Middleware; // MetaPayload differs from ActionMeta in that it is the object that the reducers // receive, instead of what is dispatched export type MetaPayload = M & { ['redux-pack/LIFECYCLE']?: LIFECYCLEValues; ['redux-pack/TRANSACTION']?: string; }; // Incomplete typing export type PackActionPayload = ReduxAction & { payload: Payload; meta: MetaPayload; }; export type handlerReducer = (state: S, action: A) => S; export interface Handlers { start?: handlerReducer>; finish?: handlerReducer; failure?: handlerReducer>; success?: handlerReducer>; always?: handlerReducer; } export type GetState = () => S; export interface ActionMeta { startPayload?: TStartPayload; onStart?(payload: TStartPayload, getState: GetState): void; onFinish?(resolved: boolean, getState: GetState): void; onSuccess?(response: TSuccessPayload, getState: GetState): void; onFailure?(error: TErrorPayload, getState: GetState): void; ['redux-pack/LIFECYCLE']?: LIFECYCLEValues; ['redux-pack/TRANSACTION']?: string; } export interface PackError { error: boolean; payload: any; } export interface Action extends ReduxAction { promise?: Promise; payload?: TSuccessPayload | TErrorPayload | TStartPayload; meta?: ActionMeta & TMetaPayload; // add optional error key to conform to FSA design: https://github.com/redux-utilities/flux-standard-action // note that users of this middleware (using our types) must conform to FSA shaped actions or code will not compile error?: boolean | null; } export interface TFullState { [key: string]: any; } export function handle( state: TState, action: Action, handlers: Handlers, ): TState;