UNPKG

rtk-slice-manager

Version:

Wrap over slice from @reduxjs/toolkit for automatic generation of actions and isolation of effect logic after changing the state of the fields

111 lines (110 loc) 6.92 kB
import { CaseReducer, CaseReducerWithPrepare, PayloadAction, ThunkAction, Action, CaseReducerActions, SliceCaseReducers, CreateSliceOptions, ValidateSliceCaseReducers, Slice } from "@reduxjs/toolkit"; import { ThunkMiddleware } from 'redux-thunk'; import { AnyAction } from 'redux'; export declare type State = Record<string, unknown>; export declare type Cast<T, U> = T extends U ? T : any; export declare type GetObjValues<T> = T extends Record<any, infer V> ? V : never; export declare type SwitchKeyValue<T, T1 extends Record<string, any> = { [K in keyof T]: { key: K; value: T[K]; }; }, T2 = { [K in GetObjValues<T1>['value']]: Extract<GetObjValues<T1>, { value: K; }>['key']; }> = T2; export declare type TransformKeysToCamelCase<T extends Record<string, any>, T0 = { [K in keyof T]: `change${Capitalize<Cast<K, string>>}`; }, T1 = SwitchKeyValue<T0>, T2 = { [K in keyof T1]: T[Cast<T1[K], string>]; }> = T2; export declare type Handlers<T extends State> = { [K in keyof T]: CaseReducer<T, PayloadAction<T[K]>> | CaseReducerWithPrepare<T, PayloadAction<any, string, any, any>>; }; export declare type HookHandlers<T extends State> = { [K in keyof T]: (value: T[K]) => void; }; export declare type Reducers<T extends State> = { [K in keyof T]?: CaseReducer<T, PayloadAction<any>> | CaseReducerWithPrepare<T, PayloadAction<any, string, any, any>>; }; export declare type CapitalizeHandlers<S extends State> = TransformKeysToCamelCase<Handlers<GetTypesByDeppKeys<S>>>; export declare type CapitalizeHookHandlers<S extends State> = TransformKeysToCamelCase<HookHandlers<GetTypesByDeppKeys<S>>>; export interface Watcher<T extends State> { handler: (params: T) => WatcherHandlerAction<T>; fields: Deps<T>; isSubscriber?: boolean; } export declare type WatcherHandlerAction<T> = ThunkAction<any, T & any, undefined, Action<string>>; export declare type ManagerMiddleware<T> = ThunkMiddleware<T & any, AnyAction, undefined>; export declare type ManagerActions<T extends State> = CaseReducerActions<CapitalizeHandlers<T>> | CaseReducerActions<SliceCaseReducers<T>>; export declare type ManagerReducers<T, T0 extends SliceCaseReducers<T> = SliceCaseReducers<T>> = ValidateSliceCaseReducers<T, T0>; export declare type ManagerExtraReducers<T> = CreateSliceOptions<T, SliceCaseReducers<T>, string>['extraReducers']; export interface ManagerOptions<T extends State> { readonly name: string; initialState: T; readonly watchers?: Watcher<T>[]; reducers?: ManagerReducers<T, Partial<CapitalizeHandlers<T>> & SliceCaseReducers<T>>; extraReducers?: ManagerExtraReducers<T>; } export declare type SliceManager<T extends State> = { actions: ManagerActions<T>; name: string; middleware: ManagerMiddleware<T>; updateSubscriptions: () => WatcherHandlerAction<T>; slice: Slice<T, SliceCaseReducers<T>>; }; export declare type DotPrefix<T extends string> = T extends "" ? "" : `.${T}`; export declare type DotNestedKeys<T> = T extends (Number | Date | Function | Array<any> | Boolean | String) ? "" : (T extends Object ? { [K in Exclude<keyof T, symbol>]: `${K}` | `${K}${DotPrefix<DotNestedKeys<T[K]>>}`; }[Exclude<keyof T, symbol>] : "") extends infer D ? Extract<D, string> : never; export declare type GetTypesByDeppKeys<T, D extends string = '.', T0 = { [key in DotNestedKeys<T>]: JoinByDot<Split<key, D>>; }, T1 = SwitchKeyValue<T0>, T2 = { [K in keyof T1]: GetTypeByKeys<Split<Cast<T1[K], string>, D>, T>; }> = T2; export declare type Deps<T> = Array<keyof { [K in DotNestedKeys<T>]: GetTypeByKeys<Split<Cast<K, string>, '.'>, T>; }>; export declare type KeyOfDeps<T> = Cast<keyof Deps<T>, string>; export declare type Split<S extends string, D extends string> = string extends S ? string[] : S extends '' ? [] : S extends `${infer T}${D}${infer U}` ? [T, ...Split<U, D>] : [S]; declare type GetTypeByKeys<T extends (string | number)[], Obj> = T extends { length: 1; } ? Obj[Cast<T[0], string>] : T extends { length: 2; } ? Obj[Cast<T[0], string>][Cast<T[1], string>] : T extends { length: 3; } ? Obj[Cast<T[0], string>][Cast<T[1], string>][Cast<T[2], string>] : T extends { length: 4; } ? Obj[Cast<T[0], string>][Cast<T[1], string>][Cast<T[2], string>][Cast<T[3], string>] : T extends { length: 5; } ? Obj[Cast<T[0], string>][Cast<T[1], string>][Cast<T[2], string>][Cast<T[3], string>][Cast<T[4], string>] : T extends { length: 6; } ? Obj[Cast<T[0], string>][Cast<T[1], string>][Cast<T[2], string>][Cast<T[3], string>][Cast<T[4], string>][Cast<T[5], string>] : T extends { length: 7; } ? Obj[Cast<T[0], string>][Cast<T[1], string>][Cast<T[2], string>][Cast<T[3], string>][Cast<T[4], string>][Cast<T[5], string>][Cast<T[6], string>] : T extends { length: 8; } ? Obj[Cast<T[0], string>][Cast<T[1], string>][Cast<T[2], string>][Cast<T[3], string>][Cast<T[4], string>][Cast<T[5], string>][Cast<T[6], string>][Cast<T[7], string>] : T extends { length: 9; } ? Obj[Cast<T[0], string>][Cast<T[1], string>][Cast<T[2], string>][Cast<T[3], string>][Cast<T[4], string>][Cast<T[5], string>][Cast<T[6], string>][Cast<T[7], string>][Cast<T[8], string>] : Obj[Cast<T[0], string>][Cast<T[1], string>][Cast<T[2], string>][Cast<T[3], string>][Cast<T[4], string>][Cast<T[5], string>][Cast<T[6], string>][Cast<T[7], string>][Cast<T[8], string>][Cast<T[9], string>]; declare type CapitalizeStr<T> = Capitalize<Cast<T, string>>; declare type JoinByDot<T extends (string | number)[]> = T extends { length: 1; } ? `${T[0]}` : T extends { length: 2; } ? `${T[0]}${CapitalizeStr<T[1]>}` : T extends { length: 3; } ? `${T[0]}${CapitalizeStr<T[1]>}${CapitalizeStr<T[2]>}` : T extends { length: 4; } ? `${T[0]}${CapitalizeStr<T[1]>}${CapitalizeStr<T[2]>}${CapitalizeStr<T[3]>}` : T extends { length: 5; } ? `${T[0]}${CapitalizeStr<T[1]>}${CapitalizeStr<T[2]>}${CapitalizeStr<T[3]>}${CapitalizeStr<T[4]>}` : T extends { length: 6; } ? `${T[0]}${CapitalizeStr<T[1]>}${CapitalizeStr<T[2]>}${CapitalizeStr<T[3]>}${CapitalizeStr<T[4]>}${CapitalizeStr<T[5]>}` : T extends { length: 7; } ? `${T[0]}${CapitalizeStr<T[1]>}${CapitalizeStr<T[2]>}${CapitalizeStr<T[3]>}${CapitalizeStr<T[4]>}${CapitalizeStr<T[5]>}${CapitalizeStr<T[6]>}` : T extends { length: 8; } ? `${T[0]}${CapitalizeStr<T[1]>}${CapitalizeStr<T[2]>}${CapitalizeStr<T[3]>}${CapitalizeStr<T[4]>}${CapitalizeStr<T[5]>}${CapitalizeStr<T[6]>}${CapitalizeStr<T[7]>}` : T extends { length: 9; } ? `${T[0]}${CapitalizeStr<T[1]>}${CapitalizeStr<T[2]>}${CapitalizeStr<T[3]>}${CapitalizeStr<T[4]>}${CapitalizeStr<T[5]>}${CapitalizeStr<T[6]>}${CapitalizeStr<T[7]>}${CapitalizeStr<T[8]>}` : `${T[0]}${CapitalizeStr<T[1]>}${CapitalizeStr<T[2]>}${CapitalizeStr<T[3]>}${CapitalizeStr<T[4]>}${CapitalizeStr<T[5]>}${CapitalizeStr<T[6]>}${CapitalizeStr<T[7]>}${CapitalizeStr<T[8]>}${CapitalizeStr<T[9]>}`; export {};