cogfy-data-exchange 1.0.11 → 1.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,15 +1,15 @@
1
- import type { ExtractScreen, FlowJson, NextResult, PayloadByScreenId, ScreenName, ScreenTrigger } from './types';
1
+ import type { AllTriggers, ExtractScreen, FlowJson, NextResult, PayloadByTrigger } from './types';
2
2
  type Handlers<F extends FlowJson> = {
3
- [K in ScreenName<F>]: (data: PayloadByScreenId<F, K>) => Promise<NextResult<F, K>>;
3
+ [K in AllTriggers<F>]: (data: PayloadByTrigger<F, K, ExtractScreen<K, F>>) => Promise<NextResult<F, ExtractScreen<K, F>>>;
4
4
  };
5
5
  /**
6
6
  * Creates a flow engine based on the provided flow definition and handlers.
7
7
  * @param flow The flow definition, which includes the routing model and screen definitions.
8
- * @param handlers An object mapping screen IDs to their corresponding handler functions. Each handler receives the data for its screen and returns a promise that resolves to the next result, which includes the next screen and its data.
9
- * @returns An object with a `dispatch` method to trigger screen handlers and the original flow definition.
8
+ * @param handlers An object mapping trigger strings to their corresponding handler functions. Each handler receives the payload for its trigger and returns a promise that resolves to the next result, which includes the next screen and its data.
9
+ * @returns An object with a `dispatch` method to trigger handlers and the original flow definition.
10
10
  */
11
11
  export declare function createFlow<F extends FlowJson>(flow: F, handlers: Handlers<F>): {
12
- dispatch<T extends ScreenTrigger<ScreenName<F>>>(trigger: T, payload: PayloadByScreenId<F, ExtractScreen<T, F>>): Promise<NextResult<F, ExtractScreen<T, F>>>;
12
+ dispatch<T extends AllTriggers<F>>(trigger: T, payload: PayloadByTrigger<F, T, ExtractScreen<T, F>>): Promise<NextResult<F, ExtractScreen<T, F>>>;
13
13
  flow: F;
14
14
  };
15
15
  export {};
@@ -4,14 +4,14 @@ exports.createFlow = createFlow;
4
4
  /**
5
5
  * Creates a flow engine based on the provided flow definition and handlers.
6
6
  * @param flow The flow definition, which includes the routing model and screen definitions.
7
- * @param handlers An object mapping screen IDs to their corresponding handler functions. Each handler receives the data for its screen and returns a promise that resolves to the next result, which includes the next screen and its data.
8
- * @returns An object with a `dispatch` method to trigger screen handlers and the original flow definition.
7
+ * @param handlers An object mapping trigger strings to their corresponding handler functions. Each handler receives the payload for its trigger and returns a promise that resolves to the next result, which includes the next screen and its data.
8
+ * @returns An object with a `dispatch` method to trigger handlers and the original flow definition.
9
9
  */
10
10
  function createFlow(flow, handlers) {
11
11
  return {
12
12
  dispatch(trigger, payload) {
13
- const screen = trigger.replace('screen.', '');
14
- return handlers[screen](payload);
13
+ const handler = handlers[trigger];
14
+ return handler(payload);
15
15
  },
16
16
  flow
17
17
  };
@@ -1,15 +1,15 @@
1
1
  export type FlowJsonDataValue = {
2
2
  type: 'string';
3
- __example__?: string;
3
+ __example__: string;
4
4
  } | {
5
5
  type: 'number';
6
- __example__?: number;
6
+ __example__: number;
7
7
  } | {
8
8
  type: 'boolean';
9
- __example__?: boolean;
9
+ __example__: boolean;
10
10
  } | {
11
11
  type: 'object';
12
- properties: Record<string, FlowJsonDataValue>;
12
+ properties?: Record<string, FlowJsonDataValue>;
13
13
  __example__?: Record<string, unknown>;
14
14
  } | {
15
15
  type: 'array';
@@ -9,9 +9,17 @@ type ExtractChildren<T> = T extends {
9
9
  };
10
10
  } ? C extends readonly unknown[] ? C[number] : never : never;
11
11
  type ExtractPayload<T> = T extends {
12
- "on-click-action": {
12
+ 'on-click-action': {
13
13
  payload: infer P;
14
14
  };
15
15
  } ? P : never;
16
+ type ExtractChildByTrigger<Children, Trigger extends string> = Children extends {
17
+ 'on-click-action': {
18
+ payload: {
19
+ trigger: Trigger;
20
+ };
21
+ };
22
+ } ? Children : never;
16
23
  export type PayloadByScreenId<T, Id extends string> = ExtractPayload<ExtractChildren<ExtractScreenById<T, Id>>>;
24
+ export type PayloadByTrigger<T, Trigger extends string, ScreenId extends string> = ExtractPayload<ExtractChildByTrigger<ExtractChildren<ExtractScreenById<T, ScreenId>>, Trigger>>;
17
25
  export {};
@@ -1,4 +1,17 @@
1
1
  import type { FlowJson } from './flow-json';
2
2
  import type { ScreenName } from './screen';
3
3
  export type ScreenTrigger<S extends string> = `screen.${S}`;
4
- export type ExtractScreen<T extends string, F extends FlowJson> = Extract<T extends `screen.${infer S}` ? S : never, ScreenName<F>>;
4
+ type AllChildTriggers<F extends FlowJson> = F['screens'][number] extends infer S ? S extends {
5
+ layout: {
6
+ children: readonly (infer C)[];
7
+ };
8
+ } ? C extends {
9
+ 'on-click-action': {
10
+ payload: {
11
+ trigger: infer T;
12
+ };
13
+ };
14
+ } ? T extends string ? T : never : never : never : never;
15
+ export type AllTriggers<F extends FlowJson> = ScreenTrigger<ScreenName<F>> | AllChildTriggers<F>;
16
+ export type ExtractScreen<T extends string, F extends FlowJson> = Extract<T extends `screen.${infer S}.event.${string}` ? S : T extends `screen.${infer S}` ? S : never, ScreenName<F>>;
17
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cogfy-data-exchange",
3
- "version": "1.0.11",
3
+ "version": "1.0.12",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",