UNPKG

raiden-ts

Version:

Raiden Light Client Typescript/Javascript SDK

226 lines (225 loc) 11.3 kB
import * as t from 'io-ts'; import type { Observable } from 'rxjs'; import type { Hash } from './types'; /** * The type of a generic action */ export declare type Action<TType extends string = string> = { readonly type: TType; }; export declare type Reducer<S = any, A extends Action = Action> = (state: S | undefined, action: A) => S; interface ActionConstraint { readonly type: string; readonly payload?: unknown; readonly meta?: unknown; readonly error?: true; } declare type ActionFactory<A extends ActionConstraint> = A extends { readonly meta?: unknown; } ? A extends { readonly payload?: unknown; } ? (payload: A['payload'], meta: A['meta']) => A : (_: undefined, meta: A['meta']) => A : A extends { readonly payload?: unknown; } ? (payload: A['payload']) => A : () => A; declare type ActionCreatorMembers<A extends ActionConstraint> = { type: A['type']; codec: t.Mixed; is: (a: unknown) => a is A; error: A extends { readonly error: true; } ? true : undefined; }; export declare type ActionCreator<A extends ActionConstraint> = ActionFactory<A> & ActionCreatorMembers<A>; declare type ActionCreatorFromTuple<P extends readonly [type: string, payload?: t.Mixed, meta?: t.Mixed, error?: true]> = ActionCreator<P extends { readonly 3: true; } ? P extends { readonly 2: t.Mixed; } ? P extends { readonly 1: t.Mixed; } ? { readonly type: P[0]; readonly payload: t.TypeOf<P[1]>; readonly meta: t.TypeOf<P[2]>; readonly error: true; } : { readonly type: P[0]; readonly meta: t.TypeOf<P[2]>; readonly error: true; } : P extends { readonly 1: t.Mixed; } ? { readonly type: P[0]; readonly payload: t.TypeOf<P[1]>; readonly error: true; } : { readonly type: P[0]; readonly error: true; } : P extends { readonly 2: t.Mixed; } ? P extends { readonly 1: t.Mixed; } ? { readonly type: P[0]; readonly payload: t.TypeOf<P[1]>; readonly meta: t.TypeOf<P[2]>; } : { readonly type: P[0]; readonly meta: t.TypeOf<P[2]>; } : P extends { readonly 1: t.Mixed; } ? { readonly type: P[0]; readonly payload: t.TypeOf<P[1]>; } : { readonly type: P[0]; }>; /** * @param args - Args params * @returns action creator */ export declare function createAction<P extends readonly [type: string, payload?: t.Mixed, meta?: t.Mixed, error?: true]>(...args: P): ActionCreatorFromTuple<P>; export declare type AnyAC = ActionCreator<any>; /** * A version of ReturnType which returns `never` insted of `any` if it can't match function type */ export declare type OnlyReturnType<F> = F extends (...args: any) => infer R ? R : never; /** union of all value types in type T */ export declare type ValueOf<T> = T[keyof T]; /** Types extending this constraint can be unfolded/flattened into a union of ActionCreators */ export declare type ActionsUnionConstraint = readonly AnyAC[] | { readonly [K: string]: AnyAC | { readonly [K2: string]: AnyAC; }; } | AnyAC; /** Convert tuples, arrays, modules and mappings containing ACs & AACs to the plain union of ACs */ export declare type ActionsUnion<AC extends ActionsUnionConstraint> = AC extends readonly AnyAC[] ? AC[number] : AC extends AnyAC ? AC : ValueOf<{ [K in keyof AC]: AC[K] extends AnyAC ? AC[K] : AC[K] extends { readonly [K2: string]: AnyAC; } ? ValueOf<AC[K]> : never; }>; /** * Type helper to extract the type of an action or a mapping of actions * Usage: const action: ActionType<typeof actionCreator>; */ export declare type ActionType<AC extends ActionsUnionConstraint> = OnlyReturnType<ActionsUnion<AC>>; export declare type ActionTypeOf<AC extends ActionsUnionConstraint> = ActionType<AC>['type']; export declare function isActionOf<AC extends ActionsUnionConstraint>(ac: AC, action: unknown): action is ActionType<AC>; export declare function isActionOf<AC extends ActionsUnionConstraint>(ac: AC): (action: unknown) => action is ActionType<AC>; /*** Async Actions ***/ /** * Maps parameters for createAsyncAction to respective async ActionCreators (request, success and * failure) */ declare type AsyncActionCreator<P extends readonly [ meta: t.Mixed, type: string, request?: t.Mixed, success?: t.Mixed, failure?: t.Mixed ]> = { readonly request: ActionCreatorFromTuple<[`${P[1]}/request`, P[2], P[0]]>; readonly success: ActionCreatorFromTuple<[`${P[1]}/success`, P[3], P[0]]>; readonly failure: ActionCreatorFromTuple<[ `${P[1]}/failure`, P[4] extends t.Mixed ? P[4] : t.UnknownC, P[0], true ]>; }; /** * A type which constrains any async ActionCreator tuple */ export declare type AnyAAC = { readonly request: ActionCreator<{ readonly type: `${string}/request`; readonly payload?: any; readonly meta: any; }>; readonly success: ActionCreator<{ readonly type: `${string}/success`; readonly payload?: any; readonly meta: any; }>; readonly failure: ActionCreator<{ readonly type: `${string}/failure`; readonly payload: unknown; readonly meta: any; readonly error: true; }>; }; /** * Create a set of async actions * * Here, meta is first class citizen, as it's required and what links a request with its responses * (success or failure). * * @param args - Arguments tuple; [meta, type] are required, while [request, success an failure] * are codecs to be used as payloads for the respective ActionCreators * @returns Async actions */ export declare function createAsyncAction<P extends readonly [ meta: t.Mixed, type: string, request?: t.Mixed, success?: t.Mixed, failure?: t.Mixed ]>(...args: P): AsyncActionCreator<P>; export declare function isResponseOf<AAC extends AnyAAC>(asyncAction: AAC, meta: ActionType<AAC['request']>['meta']): (action: unknown) => action is ActionType<AAC['success'] | AAC['failure']>; export declare function isResponseOf<AAC extends AnyAAC>(asyncAction: AAC, meta: ActionType<AAC['request']>['meta'], action: unknown): action is ActionType<AAC['success'] | AAC['failure']>; export declare function isConfirmationResponseOf<AAC extends AnyAAC>(asyncAction: AAC, meta: ActionType<AAC['request']>['meta'], action: unknown): action is (ActionType<AAC['success']> & { payload: { confirmed: boolean; }; }) | ActionType<AAC['failure']>; export declare function isConfirmationResponseOf<AAC extends AnyAAC>(asyncAction: AAC, meta: ActionType<AAC['request']>['meta']): (action: unknown) => action is (ActionType<AAC['success']> & { payload: { confirmed: boolean; }; }) | ActionType<AAC['failure']>; export declare function asyncActionToPromise<AAC extends AnyAAC>(asyncAction: AAC, meta: ActionType<AAC['request']>['meta'], action$: Observable<Action>): Promise<ActionType<AAC['success']>['payload']>; export declare function asyncActionToPromise<AAC extends AnyAAC>(asyncAction: AAC, meta: ActionType<AAC['request']>['meta'], action$: Observable<Action>, confirmed: false): Promise<ActionType<AAC['success']>['payload'] & { txBlock: number; txHash: Hash; confirmed: undefined | boolean; }>; export declare function asyncActionToPromise<AAC extends AnyAAC>(asyncAction: AAC, meta: ActionType<AAC['request']>['meta'], action$: Observable<Action>, confirmed: true): Promise<ActionType<AAC['success']>['payload'] & { txBlock: number; txHash: Hash; confirmed: true; }>; /** * Create a reducer which can be extended with additional actions handlers * * Usage: * const reducer = createReducer(State) * .handle(action, (s, a): State => ...) * .handle(...) * .handle(...); * * @param initialState - state for initialization (if no state is passed on reducer call) * @returns A reducer function, extended with a handle method to extend it */ export declare function createReducer<S, A extends Action = Action>(initialState: S): Reducer<S, A> & { handle: <AC extends AnyAC & (AD extends never ? never : AD), H extends (state: S, action: OnlyReturnType<ActionsUnion<AC>>) => S, AD extends AnyAC = AC>(ac: AC | readonly AC[], handler: H) => Reducer<S, A> & { handle: <AC_1 extends AnyAC & (AD_1 extends AC ? never : AD_1), H_1 extends (state: S, action: OnlyReturnType<ActionsUnion<AC_1>>) => S, AD_1 extends AnyAC = AC_1>(ac: AC_1 | readonly AC_1[], handler: H_1) => Reducer<S, A> & { handle: <AC_2 extends AnyAC & (AD_2 extends AC | AC_1 ? never : AD_2), H_2 extends (state: S, action: OnlyReturnType<ActionsUnion<AC_2>>) => S, AD_2 extends AnyAC = AC_2>(ac: AC_2 | readonly AC_2[], handler: H_2) => Reducer<S, A> & { handle: <AC_3 extends AnyAC & (AD_3 extends AC | AC_1 | AC_2 ? never : AD_3), H_3 extends (state: S, action: OnlyReturnType<ActionsUnion<AC_3>>) => S, AD_3 extends AnyAC = AC_3>(ac: AC_3 | readonly AC_3[], handler: H_3) => Reducer<S, A> & { handle: <AC_4 extends AnyAC & (AD_4 extends AC | AC_1 | AC_2 | AC_3 ? never : AD_4), H_4 extends (state: S, action: OnlyReturnType<ActionsUnion<AC_4>>) => S, AD_4 extends AnyAC = AC_4>(ac: AC_4 | readonly AC_4[], handler: H_4) => Reducer<S, A> & { handle: <AC_5 extends AnyAC & (AD_5 extends AC | AC_1 | AC_2 | AC_3 | AC_4 ? never : AD_5), H_5 extends (state: S, action: OnlyReturnType<ActionsUnion<AC_5>>) => S, AD_5 extends AnyAC = AC_5>(ac: AC_5 | readonly AC_5[], handler: H_5) => Reducer<S, A> & { handle: <AC_6 extends AnyAC & (AD_6 extends AC | AC_1 | AC_2 | AC_3 | AC_4 | AC_5 ? never : AD_6), H_6 extends (state: S, action: OnlyReturnType<ActionsUnion<AC_6>>) => S, AD_6 extends AnyAC = AC_6>(ac: AC_6 | readonly AC_6[], handler: H_6) => Reducer<S, A> & { handle: <AC_7 extends AnyAC & (AD_7 extends AC | AC_1 | AC_2 | AC_3 | AC_4 | AC_5 | AC_6 ? never : AD_7), H_7 extends (state: S, action: OnlyReturnType<ActionsUnion<AC_7>>) => S, AD_7 extends AnyAC = AC_7>(ac: AC_7 | readonly AC_7[], handler: H_7) => Reducer<S, A> & { handle: <AC_8 extends AnyAC & (AD_8 extends AC | AC_1 | AC_2 | AC_3 | AC_4 | AC_5 | AC_6 | AC_7 ? never : AD_8), H_8 extends (state: S, action: OnlyReturnType<ActionsUnion<AC_8>>) => S, AD_8 extends AnyAC = AC_8>(ac: AC_8 | readonly AC_8[], handler: H_8) => Reducer<S, A> & { handle: <AC_9 extends AnyAC & (AD_9 extends AC | AC_1 | AC_2 | AC_3 | AC_4 | AC_5 | AC_6 | AC_7 | AC_8 ? never : AD_9), H_9 extends (state: S, action: OnlyReturnType<ActionsUnion<AC_9>>) => S, AD_9 extends AnyAC = AC_9>(ac: AC_9 | readonly AC_9[], handler: H_9) => Reducer<S, A> & { handle: <AC_10 extends AnyAC & (AD_10 extends AC | AC_1 | AC_2 | AC_3 | AC_4 | AC_5 | AC_6 | AC_7 | AC_8 | AC_9 ? never : AD_10), H_10 extends (state: S, action: OnlyReturnType<ActionsUnion<AC_10>>) => S, AD_10 extends AnyAC = AC_10>(ac: AC_10 | readonly AC_10[], handler: H_10) => Reducer<S, A> & any; }; }; }; }; }; }; }; }; }; }; }; export {};