cogfy-data-exchange 1.0.13 → 1.0.15

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.
@@ -8,6 +8,10 @@ type Handlers<F extends FlowJson> = {
8
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
9
  * @returns An object with a `dispatch` method to trigger handlers and the original flow definition.
10
10
  */
11
+ type DispatchPayloads<F extends FlowJson> = {
12
+ [K in AllTriggers<F>]: PayloadByTrigger<F, K, ExtractScreen<K, F>>;
13
+ };
14
+ export type AllPayloads<F extends FlowJson> = DispatchPayloads<F>[AllTriggers<F>];
11
15
  export declare function createFlow<F extends FlowJson>(flow: F, handlers: Handlers<F>): {
12
16
  dispatch<T extends AllTriggers<F>>(trigger: T, payload: PayloadByTrigger<F, T, ExtractScreen<T, F>>): Promise<NextResult<F, ExtractScreen<T, F>>>;
13
17
  flow: F;
@@ -1,12 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createFlow = createFlow;
4
- /**
5
- * Creates a flow engine based on the provided flow definition and handlers.
6
- * @param flow The flow definition, which includes the routing model and screen definitions.
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
- */
10
4
  function createFlow(flow, handlers) {
11
5
  return {
12
6
  dispatch(trigger, payload) {
@@ -1,3 +1,4 @@
1
+ import type { FromSchema } from './schema';
1
2
  type ExtractScreenById<T, Id extends string> = T extends {
2
3
  screens: infer Screens;
3
4
  } ? Screens extends readonly unknown[] ? Extract<Screens[number], {
@@ -20,6 +21,33 @@ type ExtractChildByTrigger<Children, Trigger extends string> = Children extends
20
21
  };
21
22
  };
22
23
  } ? Children : never;
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>>;
24
+ type ExtractScreenData<T> = T extends {
25
+ data: infer D;
26
+ } ? D : never;
27
+ type ExtractDataSourceIds<DS> = DS extends readonly {
28
+ id: infer Id;
29
+ }[] ? Id : string;
30
+ type ComponentFormValue<C> = C extends {
31
+ type: 'RadioButtonsGroup' | 'Dropdown';
32
+ 'data-source': infer DS;
33
+ } ? ExtractDataSourceIds<DS> : C extends {
34
+ type: 'CheckboxGroup';
35
+ 'data-source': infer DS;
36
+ } ? ExtractDataSourceIds<DS>[] : C extends {
37
+ type: 'TextInput' | 'TextArea' | 'DatePicker';
38
+ } ? string : C extends {
39
+ type: 'OptIn';
40
+ } ? boolean : string;
41
+ type ResolvePayloadValue<V, Children, ScreenData> = V extends `$${'{'}form.${infer FieldName}}` ? [Extract<Children, {
42
+ name: FieldName;
43
+ }>] extends [never] ? string : ComponentFormValue<Extract<Children, {
44
+ name: FieldName;
45
+ }>> : V extends `$${'{'}data}` ? {
46
+ [K in keyof ScreenData]: FromSchema<ScreenData[K]>;
47
+ } : V extends `$${'{'}data.${infer DataField}}` ? DataField extends keyof ScreenData ? FromSchema<ScreenData[DataField]> : unknown : V;
48
+ type ResolvePayload<P, Children, ScreenData> = P extends unknown ? {
49
+ [K in keyof P]: ResolvePayloadValue<P[K], Children, ScreenData>;
50
+ } : never;
51
+ export type PayloadByScreenId<T, Id extends string> = ResolvePayload<ExtractPayload<ExtractChildren<ExtractScreenById<T, Id>>>, ExtractChildren<ExtractScreenById<T, Id>>, ExtractScreenData<ExtractScreenById<T, Id>>>;
52
+ export type PayloadByTrigger<T, Trigger extends string, ScreenId extends string> = ResolvePayload<ExtractPayload<ExtractChildByTrigger<ExtractChildren<ExtractScreenById<T, ScreenId>>, Trigger>>, ExtractChildren<ExtractScreenById<T, ScreenId>>, ExtractScreenData<ExtractScreenById<T, ScreenId>>>;
25
53
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cogfy-data-exchange",
3
- "version": "1.0.13",
3
+ "version": "1.0.15",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -12,6 +12,12 @@ type Handlers<F extends FlowJson> = {
12
12
  * @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.
13
13
  * @returns An object with a `dispatch` method to trigger handlers and the original flow definition.
14
14
  */
15
+ type DispatchPayloads<F extends FlowJson> = {
16
+ [K in AllTriggers<F>]: PayloadByTrigger<F, K, ExtractScreen<K, F>>
17
+ }
18
+
19
+ export type AllPayloads<F extends FlowJson> = DispatchPayloads<F>[AllTriggers<F>]
20
+
15
21
  export function createFlow<F extends FlowJson>(flow: F, handlers: Handlers<F>) {
16
22
  return {
17
23
  dispatch<T extends AllTriggers<F>>(
@@ -1,3 +1,5 @@
1
+ import type { FromSchema } from './schema'
2
+
1
3
  type ExtractScreenById<T, Id extends string> = T extends {
2
4
  screens: infer Screens
3
5
  }
@@ -20,8 +22,50 @@ type ExtractChildByTrigger<Children, Trigger extends string> = Children extends
20
22
  ? Children
21
23
  : never
22
24
 
23
- export type PayloadByScreenId<T, Id extends string> = ExtractPayload<ExtractChildren<ExtractScreenById<T, Id>>>
25
+ type ExtractScreenData<T> = T extends { data: infer D } ? D : never
26
+
27
+ type ExtractDataSourceIds<DS> = DS extends readonly { id: infer Id }[] ? Id : string
28
+
29
+ type ComponentFormValue<C> = C extends {
30
+ type: 'RadioButtonsGroup' | 'Dropdown'
31
+ 'data-source': infer DS
32
+ }
33
+ ? ExtractDataSourceIds<DS>
34
+ : C extends {
35
+ type: 'CheckboxGroup'
36
+ 'data-source': infer DS
37
+ }
38
+ ? ExtractDataSourceIds<DS>[]
39
+ : C extends { type: 'TextInput' | 'TextArea' | 'DatePicker' }
40
+ ? string
41
+ : C extends { type: 'OptIn' }
42
+ ? boolean
43
+ : string
44
+
45
+ type ResolvePayloadValue<V, Children, ScreenData> = V extends `$${'{'}form.${infer FieldName}}`
46
+ ? [Extract<Children, { name: FieldName }>] extends [never]
47
+ ? string
48
+ : ComponentFormValue<Extract<Children, { name: FieldName }>>
49
+ : V extends `$${'{'}data}`
50
+ ? { [K in keyof ScreenData]: FromSchema<ScreenData[K]> }
51
+ : V extends `$${'{'}data.${infer DataField}}`
52
+ ? DataField extends keyof ScreenData
53
+ ? FromSchema<ScreenData[DataField]>
54
+ : unknown
55
+ : V
56
+
57
+ type ResolvePayload<P, Children, ScreenData> = P extends unknown
58
+ ? { [K in keyof P]: ResolvePayloadValue<P[K], Children, ScreenData> }
59
+ : never
60
+
61
+ export type PayloadByScreenId<T, Id extends string> = ResolvePayload<
62
+ ExtractPayload<ExtractChildren<ExtractScreenById<T, Id>>>,
63
+ ExtractChildren<ExtractScreenById<T, Id>>,
64
+ ExtractScreenData<ExtractScreenById<T, Id>>
65
+ >
24
66
 
25
- export type PayloadByTrigger<T, Trigger extends string, ScreenId extends string> = ExtractPayload<
26
- ExtractChildByTrigger<ExtractChildren<ExtractScreenById<T, ScreenId>>, Trigger>
67
+ export type PayloadByTrigger<T, Trigger extends string, ScreenId extends string> = ResolvePayload<
68
+ ExtractPayload<ExtractChildByTrigger<ExtractChildren<ExtractScreenById<T, ScreenId>>, Trigger>>,
69
+ ExtractChildren<ExtractScreenById<T, ScreenId>>,
70
+ ExtractScreenData<ExtractScreenById<T, ScreenId>>
27
71
  >