UNPKG

4.08 kBTypeScriptView Raw
1import { ActionCreator, ActionCreatorProps, Creator, FunctionWithParametersType, NotAllowedCheck, TypedAction } from './models';
2type Join<Str extends string, Separator extends string = ' '> = Str extends `${infer First}${Separator}${infer Rest}` ? Join<`${First}${Rest}`, Separator> : Str;
3type CapitalizeWords<Str extends string> = Str extends `${infer First} ${infer Rest}` ? `${Capitalize<First>} ${CapitalizeWords<Rest>}` : Capitalize<Str>;
4type StringLiteralCheck<Str extends string, Name extends string> = string extends Str ? `${Name} must be a string literal type` : unknown;
5type UniqueEventNameCheck<EventNames extends string, EventName extends string> = ActionName<EventName> extends ActionName<Exclude<EventNames, EventName>> ? `${ActionName<EventName>} action is already defined` : unknown;
6type NotAllowedEventPropsCheck<PropsCreator extends ActionCreatorProps<unknown> | Creator> = PropsCreator extends ActionCreatorProps<infer Props> ? Props extends void ? unknown : NotAllowedCheck<Props & object> : PropsCreator extends Creator<any, infer Result> ? NotAllowedCheck<Result> : unknown;
7type EventCreator<PropsCreator extends ActionCreatorProps<unknown> | Creator, Type extends string> = PropsCreator extends ActionCreatorProps<infer Props> ? void extends Props ? ActionCreator<Type, () => TypedAction<Type>> : ActionCreator<Type, (props: Props & NotAllowedCheck<Props & object>) => Props & TypedAction<Type>> : PropsCreator extends Creator<infer Props, infer Result> ? FunctionWithParametersType<Props, Result & NotAllowedCheck<Result> & TypedAction<Type>> & TypedAction<Type> : never;
8type ActionName<EventName extends string> = Uncapitalize<Join<CapitalizeWords<EventName>>>;
9interface ActionGroupConfig<Source extends string, Events extends Record<string, ActionCreatorProps<unknown> | Creator>> {
10 source: Source & StringLiteralCheck<Source, 'source'>;
11 events: Events & {
12 [EventName in keyof Events]: StringLiteralCheck<EventName & string, 'event name'> & UniqueEventNameCheck<keyof Events & string, EventName & string> & NotAllowedEventPropsCheck<Events[EventName]>;
13 };
14}
15type ActionGroup<Source extends string, Events extends Record<string, ActionCreatorProps<unknown> | Creator>> = {
16 [EventName in keyof Events as ActionName<EventName & string>]: EventCreator<Events[EventName], `[${Source}] ${EventName & string}`>;
17};
18/**
19 * @description
20 * A function that creates a group of action creators with the same source.
21 *
22 * @param config An object that contains a source and dictionary of events.
23 * An event is a key-value pair of an event name and event props.
24 * @returns A dictionary of action creators.
25 * The name of each action creator is created by camel casing the event name.
26 * The type of each action is created using the "[Source] Event Name" pattern.
27 *
28 * @usageNotes
29 *
30 * ```ts
31 * const authApiActions = createActionGroup({
32 * source: 'Auth API',
33 * events: {
34 * // defining events with payload using the `props` function
35 * 'Login Success': props<{ userId: number; token: string }>(),
36 * 'Login Failure': props<{ error: string }>(),
37 *
38 * // defining an event without payload using the `emptyProps` function
39 * 'Logout Success': emptyProps(),
40 *
41 * // defining an event with payload using the props factory
42 * 'Logout Failure': (error: Error) => ({ error }),
43 * },
44 * });
45 *
46 * // action type: "[Auth API] Login Success"
47 * authApiActions.loginSuccess({ userId: 10, token: 'ngrx' });
48 *
49 * // action type: "[Auth API] Login Failure"
50 * authApiActions.loginFailure({ error: 'Login Failure!' });
51 *
52 * // action type: "[Auth API] Logout Success"
53 * authApiActions.logoutSuccess();
54 *
55 * // action type: "[Auth API] Logout Failure";
56 * authApiActions.logoutFailure(new Error('Logout Failure!'));
57 * ```
58 */
59export declare function createActionGroup<Source extends string, Events extends Record<string, ActionCreatorProps<unknown> | Creator>>(config: ActionGroupConfig<Source, Events>): ActionGroup<Source, Events>;
60export declare function emptyProps(): ActionCreatorProps<void>;
61export {};