@abinnovision/nestjs-hatchet 0.8.0-beta.3 → 0.8.0-beta.5

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.
Files changed (115) hide show
  1. package/README.md +6 -6
  2. package/dist/abstracts/hosts.d.cts +70 -0
  3. package/dist/abstracts/hosts.d.mts +70 -0
  4. package/dist/abstracts/index.d.cts +1 -0
  5. package/dist/abstracts/index.d.mts +1 -0
  6. package/dist/client/client.cjs +34 -21
  7. package/dist/client/client.d.cts +70 -0
  8. package/dist/client/client.d.mts +70 -0
  9. package/dist/client/client.mjs +34 -21
  10. package/dist/client/index.d.cts +1 -0
  11. package/dist/client/index.d.mts +1 -0
  12. package/dist/events/event-definition.d.cts +92 -0
  13. package/dist/events/event-definition.d.mts +92 -0
  14. package/dist/events/index.d.cts +1 -0
  15. package/dist/events/index.d.mts +1 -0
  16. package/dist/exceptions/event-payload-malformed.exception.d.cts +17 -0
  17. package/dist/exceptions/event-payload-malformed.exception.d.mts +17 -0
  18. package/dist/exceptions/hatchet.exception.d.cts +14 -0
  19. package/dist/exceptions/hatchet.exception.d.mts +14 -0
  20. package/dist/exceptions/index.d.cts +4 -0
  21. package/dist/exceptions/index.d.mts +4 -0
  22. package/dist/exceptions/input-validation-failed.exception.d.cts +20 -0
  23. package/dist/exceptions/input-validation-failed.exception.d.mts +20 -0
  24. package/dist/exceptions/validation-issue.d.cts +34 -0
  25. package/dist/exceptions/validation-issue.d.mts +34 -0
  26. package/dist/execution/context/factory.d.cts +1 -0
  27. package/dist/execution/context/factory.d.mts +1 -0
  28. package/dist/execution/context/index.d.cts +1 -0
  29. package/dist/execution/context/index.d.mts +1 -0
  30. package/dist/execution/context/types.d.cts +112 -0
  31. package/dist/execution/context/types.d.mts +112 -0
  32. package/dist/execution/host-run/adapter-factory.d.cts +1 -0
  33. package/dist/execution/host-run/adapter-factory.d.mts +1 -0
  34. package/dist/execution/host-run/function-type.d.cts +28 -0
  35. package/dist/execution/host-run/function-type.d.mts +28 -0
  36. package/dist/execution/host-run/index.d.cts +1 -0
  37. package/dist/execution/host-run/index.d.mts +1 -0
  38. package/dist/execution/index.d.cts +2 -0
  39. package/dist/execution/index.d.mts +2 -0
  40. package/dist/hatchet.module-config.d.cts +33 -0
  41. package/dist/hatchet.module-config.d.mts +33 -0
  42. package/dist/hatchet.module.d.cts +26 -0
  43. package/dist/hatchet.module.d.mts +26 -0
  44. package/dist/index.d.cts +19 -0
  45. package/dist/index.d.mts +19 -0
  46. package/dist/interceptor/index.d.cts +1 -0
  47. package/dist/interceptor/index.d.mts +1 -0
  48. package/dist/interceptor/interceptor.d.cts +11 -0
  49. package/dist/interceptor/interceptor.d.mts +11 -0
  50. package/dist/metadata/decorators/host.decorator.d.cts +22 -0
  51. package/dist/metadata/decorators/host.decorator.d.mts +22 -0
  52. package/dist/metadata/decorators/index.d.cts +3 -0
  53. package/dist/metadata/decorators/index.d.mts +3 -0
  54. package/dist/metadata/decorators/task.decorator.d.cts +10 -0
  55. package/dist/metadata/decorators/task.decorator.d.mts +10 -0
  56. package/dist/metadata/decorators/workflow-task.decorator.d.cts +14 -0
  57. package/dist/metadata/decorators/workflow-task.decorator.d.mts +14 -0
  58. package/dist/metadata/index.d.cts +3 -0
  59. package/dist/metadata/index.d.mts +3 -0
  60. package/dist/metadata/translator.d.cts +1 -0
  61. package/dist/metadata/translator.d.mts +1 -0
  62. package/dist/references/helpers.d.cts +16 -0
  63. package/dist/references/helpers.d.mts +16 -0
  64. package/dist/references/index.d.cts +3 -0
  65. package/dist/references/index.d.mts +3 -0
  66. package/dist/references/refs.d.cts +31 -0
  67. package/dist/references/refs.d.mts +31 -0
  68. package/dist/references/{shared.d.ts → shared.d.cts} +26 -29
  69. package/dist/references/shared.d.mts +169 -0
  70. package/dist/sdk/{types.d.ts → types.d.cts} +5 -2
  71. package/dist/sdk/types.d.mts +10 -0
  72. package/dist/sdk-export/index.d.cts +1 -0
  73. package/dist/sdk-export/index.d.mts +1 -0
  74. package/package.json +13 -12
  75. package/dist/abstracts/hosts.d.ts +0 -66
  76. package/dist/abstracts/index.d.ts +0 -1
  77. package/dist/client/client.d.ts +0 -48
  78. package/dist/client/index.d.ts +0 -1
  79. package/dist/events/event-definition.d.ts +0 -88
  80. package/dist/events/index.d.ts +0 -2
  81. package/dist/exceptions/event-payload-malformed.exception.d.ts +0 -13
  82. package/dist/exceptions/hatchet.exception.d.ts +0 -11
  83. package/dist/exceptions/index.d.ts +0 -4
  84. package/dist/exceptions/input-validation-failed.exception.d.ts +0 -16
  85. package/dist/exceptions/validation-issue.d.ts +0 -30
  86. package/dist/execution/context/factory.d.ts +0 -22
  87. package/dist/execution/context/index.d.ts +0 -4
  88. package/dist/execution/context/types.d.ts +0 -108
  89. package/dist/execution/host-run/adapter-factory.d.ts +0 -12
  90. package/dist/execution/host-run/function-type.d.ts +0 -24
  91. package/dist/execution/host-run/index.d.ts +0 -2
  92. package/dist/execution/index.d.ts +0 -2
  93. package/dist/explorer/declaration-builder.service.d.ts +0 -70
  94. package/dist/explorer/index.d.ts +0 -1
  95. package/dist/explorer/worker-management.service.d.ts +0 -39
  96. package/dist/hatchet.module-config.d.ts +0 -31
  97. package/dist/hatchet.module.d.ts +0 -23
  98. package/dist/index.d.ts +0 -10
  99. package/dist/interceptor/index.d.ts +0 -1
  100. package/dist/interceptor/interceptor.d.ts +0 -13
  101. package/dist/internal/registrations.d.ts +0 -19
  102. package/dist/metadata/accessor.d.ts +0 -33
  103. package/dist/metadata/decorators/host.decorator.d.ts +0 -19
  104. package/dist/metadata/decorators/index.d.ts +0 -6
  105. package/dist/metadata/decorators/task.decorator.d.ts +0 -6
  106. package/dist/metadata/decorators/workflow-task.decorator.d.ts +0 -10
  107. package/dist/metadata/index.d.ts +0 -6
  108. package/dist/metadata/keys.d.ts +0 -3
  109. package/dist/metadata/translator.d.ts +0 -10
  110. package/dist/references/helpers.d.ts +0 -27
  111. package/dist/references/index.d.ts +0 -3
  112. package/dist/references/refs.d.ts +0 -27
  113. package/dist/sdk/hatchet-client.factory.d.ts +0 -5
  114. package/dist/sdk/index.d.ts +0 -1
  115. package/dist/sdk-export/index.d.ts +0 -7
package/README.md CHANGED
@@ -176,11 +176,11 @@ await client.emit(UserCreatedEvent, {
176
176
 
177
177
  ### Client
178
178
 
179
- | Method | Description |
180
- | ---------------------------------- | --------------------- |
181
- | `client.run(ref, input, opts?)` | Execute task/workflow |
182
- | `client.emit(event, payload)` | Emit single event |
183
- | `client.emitBulk(event, payloads)` | Emit multiple events |
179
+ | Method | Description |
180
+ | ----------------------------------------- | ---------------------------------------------- |
181
+ | `client.run(ref, input, opts?)` | Execute task/workflow |
182
+ | `client.emit(event, payload \| payloads)` | Emit one or many events |
183
+ | `client.emitBulk(event, payloads)` | Emit multiple events (deprecated — use `emit`) |
184
184
 
185
185
  ### Context Types
186
186
 
@@ -212,7 +212,7 @@ const result = await runRef.output; // Await when needed
212
212
  ### Bulk Event Emission
213
213
 
214
214
  ```typescript
215
- await client.emitBulk(OrderEvent, [
215
+ await client.emit(OrderEvent, [
216
216
  { orderId: "1", total: 100 },
217
217
  { orderId: "2", total: 200 },
218
218
  ]);
@@ -0,0 +1,70 @@
1
+ import { InputType } from "@hatchet-dev/typescript-sdk";
2
+ import { StandardSchemaV1 } from "@standard-schema/spec";
3
+
4
+ //#region src/abstracts/hosts.d.ts
5
+ /**
6
+ * Interface for classes that provide a schema for their input.
7
+ */
8
+ interface InputSchemaProvider<T> {
9
+ /**
10
+ * Returns the schema for the input of the host.
11
+ * If undefined, the input is not validated.
12
+ */
13
+ inputSchema: () => StandardSchemaV1<InputType, T> | undefined;
14
+ }
15
+ /**
16
+ * Marker class for Hatchet workflow host classes.
17
+ */
18
+ declare abstract class WorkflowHost<I> implements InputSchemaProvider<I> {
19
+ readonly inputSchema: InputSchemaProvider<I>["inputSchema"];
20
+ readonly __workflow_host_brand: void;
21
+ readonly __workflow_host_input: I;
22
+ /**
23
+ * Constructor for the WorkflowHost class.
24
+ *
25
+ * @param inputSchema
26
+ * @protected
27
+ */
28
+ protected constructor(inputSchema: InputSchemaProvider<I>["inputSchema"]);
29
+ }
30
+ /**
31
+ * Marker class for Hatchet task host classes.
32
+ */
33
+ declare abstract class TaskHost<I> implements InputSchemaProvider<I> {
34
+ readonly inputSchema: InputSchemaProvider<I>["inputSchema"];
35
+ readonly __task_host_brand: void;
36
+ readonly __task_host_input: I;
37
+ /**
38
+ * Constructor for the TaskHost class.
39
+ *
40
+ * @param inputSchema
41
+ * @protected
42
+ */
43
+ protected constructor(inputSchema: InputSchemaProvider<I>["inputSchema"]);
44
+ }
45
+ /**
46
+ * Creates a new WorkflowHost class with the provided input schema.
47
+ *
48
+ * @param input The input schema.
49
+ */
50
+ declare function workflowHost<I>(input?: ReturnType<InputSchemaProvider<I>["inputSchema"]>): {
51
+ new (): {
52
+ readonly __workflow_host_brand: void;
53
+ readonly __workflow_host_input: I;
54
+ readonly inputSchema: () => StandardSchemaV1<import("@hatchet-dev/typescript-sdk").JsonObject, I> | undefined;
55
+ };
56
+ };
57
+ /**
58
+ * Creates a new TaskHost class with the provided input schema.
59
+ *
60
+ * @param input The input schema.
61
+ */
62
+ declare function taskHost<I>(input?: ReturnType<InputSchemaProvider<I>["inputSchema"]>): {
63
+ new (): {
64
+ readonly __task_host_brand: void;
65
+ readonly __task_host_input: I;
66
+ readonly inputSchema: () => StandardSchemaV1<import("@hatchet-dev/typescript-sdk").JsonObject, I> | undefined;
67
+ };
68
+ };
69
+ //#endregion
70
+ export { InputSchemaProvider, TaskHost, WorkflowHost, taskHost, workflowHost };
@@ -0,0 +1,70 @@
1
+ import { InputType } from "@hatchet-dev/typescript-sdk";
2
+ import { StandardSchemaV1 } from "@standard-schema/spec";
3
+
4
+ //#region src/abstracts/hosts.d.ts
5
+ /**
6
+ * Interface for classes that provide a schema for their input.
7
+ */
8
+ interface InputSchemaProvider<T> {
9
+ /**
10
+ * Returns the schema for the input of the host.
11
+ * If undefined, the input is not validated.
12
+ */
13
+ inputSchema: () => StandardSchemaV1<InputType, T> | undefined;
14
+ }
15
+ /**
16
+ * Marker class for Hatchet workflow host classes.
17
+ */
18
+ declare abstract class WorkflowHost<I> implements InputSchemaProvider<I> {
19
+ readonly inputSchema: InputSchemaProvider<I>["inputSchema"];
20
+ readonly __workflow_host_brand: void;
21
+ readonly __workflow_host_input: I;
22
+ /**
23
+ * Constructor for the WorkflowHost class.
24
+ *
25
+ * @param inputSchema
26
+ * @protected
27
+ */
28
+ protected constructor(inputSchema: InputSchemaProvider<I>["inputSchema"]);
29
+ }
30
+ /**
31
+ * Marker class for Hatchet task host classes.
32
+ */
33
+ declare abstract class TaskHost<I> implements InputSchemaProvider<I> {
34
+ readonly inputSchema: InputSchemaProvider<I>["inputSchema"];
35
+ readonly __task_host_brand: void;
36
+ readonly __task_host_input: I;
37
+ /**
38
+ * Constructor for the TaskHost class.
39
+ *
40
+ * @param inputSchema
41
+ * @protected
42
+ */
43
+ protected constructor(inputSchema: InputSchemaProvider<I>["inputSchema"]);
44
+ }
45
+ /**
46
+ * Creates a new WorkflowHost class with the provided input schema.
47
+ *
48
+ * @param input The input schema.
49
+ */
50
+ declare function workflowHost<I>(input?: ReturnType<InputSchemaProvider<I>["inputSchema"]>): {
51
+ new (): {
52
+ readonly __workflow_host_brand: void;
53
+ readonly __workflow_host_input: I;
54
+ readonly inputSchema: () => StandardSchemaV1<import("@hatchet-dev/typescript-sdk").JsonObject, I> | undefined;
55
+ };
56
+ };
57
+ /**
58
+ * Creates a new TaskHost class with the provided input schema.
59
+ *
60
+ * @param input The input schema.
61
+ */
62
+ declare function taskHost<I>(input?: ReturnType<InputSchemaProvider<I>["inputSchema"]>): {
63
+ new (): {
64
+ readonly __task_host_brand: void;
65
+ readonly __task_host_input: I;
66
+ readonly inputSchema: () => StandardSchemaV1<import("@hatchet-dev/typescript-sdk").JsonObject, I> | undefined;
67
+ };
68
+ };
69
+ //#endregion
70
+ export { InputSchemaProvider, TaskHost, WorkflowHost, taskHost, workflowHost };
@@ -0,0 +1 @@
1
+ import { InputSchemaProvider, TaskHost, WorkflowHost, taskHost, workflowHost } from "./hosts.cjs";
@@ -0,0 +1 @@
1
+ import { InputSchemaProvider, TaskHost, WorkflowHost, taskHost, workflowHost } from "./hosts.mjs";
@@ -26,18 +26,45 @@ var Client = class {
26
26
  return this.client;
27
27
  }
28
28
  /**
29
- * Emit a single event.
29
+ * Emit one or many events of the same type.
30
+ *
31
+ * Pass a single payload to emit one event, or an array of payloads to emit
32
+ * many at once. The return type adjusts accordingly: a single {@link Event}
33
+ * for a single input, or an array of {@link Event} for an array of inputs.
30
34
  *
31
35
  * @param event The event definition to emit.
32
- * @param input The event payload (must match the event's schema).
36
+ * @param input A single event payload, or an array of payloads (each must
37
+ * match the event's schema).
33
38
  * @param options Optional emit configuration.
34
- * @returns The emitted event.
39
+ * @returns The emitted event, or an array of emitted events.
35
40
  *
36
41
  * @example
37
42
  * ```typescript
43
+ * // Single event
38
44
  * await client.emit(UserCreatedEvent, { userId: "123", email: "user@example.com" });
39
45
  * ```
46
+ *
47
+ * @example
48
+ * ```typescript
49
+ * // Multiple events
50
+ * await client.emit(OrderPlacedEvent, [
51
+ * { orderId: "1", userId: "123", total: 100 },
52
+ * { orderId: "2", userId: "456", total: 200 },
53
+ * ]);
54
+ * ```
40
55
  */ async emit(event, input, options) {
56
+ if (Array.isArray(input)) {
57
+ if (input.length === 0) return [];
58
+ return (await this.client.events.bulkPush(event.name, input.map((single) => ({
59
+ payload: {
60
+ ...single,
61
+ [require_event_definition.EVENT_MARKER]: event.name
62
+ },
63
+ ...options?.additionalMetadata !== void 0 && { additionalMetadata: options.additionalMetadata },
64
+ ...options?.priority !== void 0 && { priority: options.priority },
65
+ ...options?.scope !== void 0 && { scope: options.scope }
66
+ })), options)).events;
67
+ }
41
68
  return await this.client.events.push(event.name, {
42
69
  ...input,
43
70
  [require_event_definition.EVENT_MARKER]: event.name
@@ -46,29 +73,15 @@ var Client = class {
46
73
  /**
47
74
  * Emit multiple events of the same type.
48
75
  *
76
+ * @deprecated Use {@link Client.emit} with an array of inputs instead. This
77
+ * alias forwards to `emit` and will be removed in a future major version.
78
+ *
49
79
  * @param event The event definition to emit.
50
80
  * @param inputs Array of event payloads.
51
81
  * @param options Optional emit configuration.
52
82
  * @returns Array of emitted events.
53
- *
54
- * @example
55
- * ```typescript
56
- * await client.emitBulk(OrderPlacedEvent, [
57
- * { orderId: "1", userId: "123", total: 100 },
58
- * { orderId: "2", userId: "456", total: 200 },
59
- * ]);
60
- * ```
61
83
  */ async emitBulk(event, inputs, options) {
62
- if (inputs.length === 0) return [];
63
- return (await this.client.events.bulkPush(event.name, inputs.map((input) => ({
64
- payload: {
65
- ...input,
66
- [require_event_definition.EVENT_MARKER]: event.name
67
- },
68
- ...options?.additionalMetadata !== void 0 && { additionalMetadata: options.additionalMetadata },
69
- ...options?.priority !== void 0 && { priority: options.priority },
70
- ...options?.scope !== void 0 && { scope: options.scope }
71
- })), options)).events;
84
+ return await this.emit(event, inputs, options);
72
85
  }
73
86
  };
74
87
  Client = _ts_decorate([
@@ -0,0 +1,70 @@
1
+ import { AnyEventDefinition, EventInput } from "../events/event-definition.cjs";
2
+ import { HostRunFn } from "../execution/host-run/function-type.cjs";
3
+ import { HatchetClient } from "@hatchet-dev/typescript-sdk";
4
+
5
+ //#region src/client/client.d.ts
6
+ type EventClient = HatchetClient["events"];
7
+ type EventEmitOptions = Parameters<EventClient["push"]>[2];
8
+ type Event = Awaited<ReturnType<EventClient["push"]>>;
9
+ /**
10
+ * Input accepted by {@link Client.emit} — a single event payload or an array of them.
11
+ */
12
+ type EventEmitInput<E extends AnyEventDefinition> = EventInput<E> | EventInput<E>[];
13
+ /**
14
+ * Return type of {@link Client.emit}: an array of events when an array of inputs is
15
+ * provided, otherwise a single event.
16
+ */
17
+ type EventEmitReturn<E extends AnyEventDefinition, I extends EventEmitInput<E>> = I extends EventInput<E>[] ? Event[] : Event;
18
+ declare class Client {
19
+ private readonly client;
20
+ readonly run: HostRunFn;
21
+ constructor(client: HatchetClient);
22
+ /**
23
+ * Access to the underlying HatchetClient SDK.
24
+ * Use for advanced features not wrapped by this library.
25
+ */
26
+ get sdk(): HatchetClient;
27
+ /**
28
+ * Emit one or many events of the same type.
29
+ *
30
+ * Pass a single payload to emit one event, or an array of payloads to emit
31
+ * many at once. The return type adjusts accordingly: a single {@link Event}
32
+ * for a single input, or an array of {@link Event} for an array of inputs.
33
+ *
34
+ * @param event The event definition to emit.
35
+ * @param input A single event payload, or an array of payloads (each must
36
+ * match the event's schema).
37
+ * @param options Optional emit configuration.
38
+ * @returns The emitted event, or an array of emitted events.
39
+ *
40
+ * @example
41
+ * ```typescript
42
+ * // Single event
43
+ * await client.emit(UserCreatedEvent, { userId: "123", email: "user@example.com" });
44
+ * ```
45
+ *
46
+ * @example
47
+ * ```typescript
48
+ * // Multiple events
49
+ * await client.emit(OrderPlacedEvent, [
50
+ * { orderId: "1", userId: "123", total: 100 },
51
+ * { orderId: "2", userId: "456", total: 200 },
52
+ * ]);
53
+ * ```
54
+ */
55
+ emit<E extends AnyEventDefinition, I extends EventEmitInput<E>>(event: E, input: I, options?: EventEmitOptions): Promise<EventEmitReturn<E, I>>;
56
+ /**
57
+ * Emit multiple events of the same type.
58
+ *
59
+ * @deprecated Use {@link Client.emit} with an array of inputs instead. This
60
+ * alias forwards to `emit` and will be removed in a future major version.
61
+ *
62
+ * @param event The event definition to emit.
63
+ * @param inputs Array of event payloads.
64
+ * @param options Optional emit configuration.
65
+ * @returns Array of emitted events.
66
+ */
67
+ emitBulk<E extends AnyEventDefinition>(event: E, inputs: EventInput<E>[], options?: EventEmitOptions): Promise<Event[]>;
68
+ }
69
+ //#endregion
70
+ export { Client };
@@ -0,0 +1,70 @@
1
+ import { AnyEventDefinition, EventInput } from "../events/event-definition.mjs";
2
+ import { HostRunFn } from "../execution/host-run/function-type.mjs";
3
+ import { HatchetClient } from "@hatchet-dev/typescript-sdk";
4
+
5
+ //#region src/client/client.d.ts
6
+ type EventClient = HatchetClient["events"];
7
+ type EventEmitOptions = Parameters<EventClient["push"]>[2];
8
+ type Event = Awaited<ReturnType<EventClient["push"]>>;
9
+ /**
10
+ * Input accepted by {@link Client.emit} — a single event payload or an array of them.
11
+ */
12
+ type EventEmitInput<E extends AnyEventDefinition> = EventInput<E> | EventInput<E>[];
13
+ /**
14
+ * Return type of {@link Client.emit}: an array of events when an array of inputs is
15
+ * provided, otherwise a single event.
16
+ */
17
+ type EventEmitReturn<E extends AnyEventDefinition, I extends EventEmitInput<E>> = I extends EventInput<E>[] ? Event[] : Event;
18
+ declare class Client {
19
+ private readonly client;
20
+ readonly run: HostRunFn;
21
+ constructor(client: HatchetClient);
22
+ /**
23
+ * Access to the underlying HatchetClient SDK.
24
+ * Use for advanced features not wrapped by this library.
25
+ */
26
+ get sdk(): HatchetClient;
27
+ /**
28
+ * Emit one or many events of the same type.
29
+ *
30
+ * Pass a single payload to emit one event, or an array of payloads to emit
31
+ * many at once. The return type adjusts accordingly: a single {@link Event}
32
+ * for a single input, or an array of {@link Event} for an array of inputs.
33
+ *
34
+ * @param event The event definition to emit.
35
+ * @param input A single event payload, or an array of payloads (each must
36
+ * match the event's schema).
37
+ * @param options Optional emit configuration.
38
+ * @returns The emitted event, or an array of emitted events.
39
+ *
40
+ * @example
41
+ * ```typescript
42
+ * // Single event
43
+ * await client.emit(UserCreatedEvent, { userId: "123", email: "user@example.com" });
44
+ * ```
45
+ *
46
+ * @example
47
+ * ```typescript
48
+ * // Multiple events
49
+ * await client.emit(OrderPlacedEvent, [
50
+ * { orderId: "1", userId: "123", total: 100 },
51
+ * { orderId: "2", userId: "456", total: 200 },
52
+ * ]);
53
+ * ```
54
+ */
55
+ emit<E extends AnyEventDefinition, I extends EventEmitInput<E>>(event: E, input: I, options?: EventEmitOptions): Promise<EventEmitReturn<E, I>>;
56
+ /**
57
+ * Emit multiple events of the same type.
58
+ *
59
+ * @deprecated Use {@link Client.emit} with an array of inputs instead. This
60
+ * alias forwards to `emit` and will be removed in a future major version.
61
+ *
62
+ * @param event The event definition to emit.
63
+ * @param inputs Array of event payloads.
64
+ * @param options Optional emit configuration.
65
+ * @returns Array of emitted events.
66
+ */
67
+ emitBulk<E extends AnyEventDefinition>(event: E, inputs: EventInput<E>[], options?: EventEmitOptions): Promise<Event[]>;
68
+ }
69
+ //#endregion
70
+ export { Client };
@@ -26,18 +26,45 @@ var Client = class {
26
26
  return this.client;
27
27
  }
28
28
  /**
29
- * Emit a single event.
29
+ * Emit one or many events of the same type.
30
+ *
31
+ * Pass a single payload to emit one event, or an array of payloads to emit
32
+ * many at once. The return type adjusts accordingly: a single {@link Event}
33
+ * for a single input, or an array of {@link Event} for an array of inputs.
30
34
  *
31
35
  * @param event The event definition to emit.
32
- * @param input The event payload (must match the event's schema).
36
+ * @param input A single event payload, or an array of payloads (each must
37
+ * match the event's schema).
33
38
  * @param options Optional emit configuration.
34
- * @returns The emitted event.
39
+ * @returns The emitted event, or an array of emitted events.
35
40
  *
36
41
  * @example
37
42
  * ```typescript
43
+ * // Single event
38
44
  * await client.emit(UserCreatedEvent, { userId: "123", email: "user@example.com" });
39
45
  * ```
46
+ *
47
+ * @example
48
+ * ```typescript
49
+ * // Multiple events
50
+ * await client.emit(OrderPlacedEvent, [
51
+ * { orderId: "1", userId: "123", total: 100 },
52
+ * { orderId: "2", userId: "456", total: 200 },
53
+ * ]);
54
+ * ```
40
55
  */ async emit(event, input, options) {
56
+ if (Array.isArray(input)) {
57
+ if (input.length === 0) return [];
58
+ return (await this.client.events.bulkPush(event.name, input.map((single) => ({
59
+ payload: {
60
+ ...single,
61
+ [EVENT_MARKER]: event.name
62
+ },
63
+ ...options?.additionalMetadata !== void 0 && { additionalMetadata: options.additionalMetadata },
64
+ ...options?.priority !== void 0 && { priority: options.priority },
65
+ ...options?.scope !== void 0 && { scope: options.scope }
66
+ })), options)).events;
67
+ }
41
68
  return await this.client.events.push(event.name, {
42
69
  ...input,
43
70
  [EVENT_MARKER]: event.name
@@ -46,29 +73,15 @@ var Client = class {
46
73
  /**
47
74
  * Emit multiple events of the same type.
48
75
  *
76
+ * @deprecated Use {@link Client.emit} with an array of inputs instead. This
77
+ * alias forwards to `emit` and will be removed in a future major version.
78
+ *
49
79
  * @param event The event definition to emit.
50
80
  * @param inputs Array of event payloads.
51
81
  * @param options Optional emit configuration.
52
82
  * @returns Array of emitted events.
53
- *
54
- * @example
55
- * ```typescript
56
- * await client.emitBulk(OrderPlacedEvent, [
57
- * { orderId: "1", userId: "123", total: 100 },
58
- * { orderId: "2", userId: "456", total: 200 },
59
- * ]);
60
- * ```
61
83
  */ async emitBulk(event, inputs, options) {
62
- if (inputs.length === 0) return [];
63
- return (await this.client.events.bulkPush(event.name, inputs.map((input) => ({
64
- payload: {
65
- ...input,
66
- [EVENT_MARKER]: event.name
67
- },
68
- ...options?.additionalMetadata !== void 0 && { additionalMetadata: options.additionalMetadata },
69
- ...options?.priority !== void 0 && { priority: options.priority },
70
- ...options?.scope !== void 0 && { scope: options.scope }
71
- })), options)).events;
84
+ return await this.emit(event, inputs, options);
72
85
  }
73
86
  };
74
87
  Client = _ts_decorate([
@@ -0,0 +1 @@
1
+ import { Client } from "./client.cjs";
@@ -0,0 +1 @@
1
+ import { Client } from "./client.mjs";
@@ -0,0 +1,92 @@
1
+ import { StandardSchemaV1 } from "@standard-schema/spec";
2
+
3
+ //#region src/events/event-definition.d.ts
4
+ /**
5
+ * Marker embedded in event payload to identify an event type.
6
+ * Used by isCtx() type guard to determine which event triggered the workflow.
7
+ */
8
+ declare const EVENT_MARKER = "__event_name";
9
+ /**
10
+ * Type-safe event definition.
11
+ * Use with Client.emit() to publish events.
12
+ *
13
+ * @template TName The literal string type of the event name.
14
+ * @template TSchema The StandardSchema type for input validation.
15
+ */
16
+ declare class EventDefinition<TName extends string, TSchema extends StandardSchemaV1> {
17
+ readonly name: TName;
18
+ readonly schema: TSchema;
19
+ /**
20
+ * Phantom types for input/output inference.
21
+ * Do not access at runtime - will throw.
22
+ */
23
+ readonly __types: {
24
+ input: StandardSchemaV1.InferInput<TSchema>;
25
+ output: StandardSchemaV1.InferOutput<TSchema>;
26
+ };
27
+ constructor(name: TName, schema: TSchema);
28
+ /**
29
+ * Type guard: casts the context input to this event's schema output type
30
+ * without requiring the event marker to be present.
31
+ *
32
+ * Use this for externally sourced events that arrive without the marker
33
+ * injected by Client.emit(). Schema validation still runs and throws if
34
+ * the payload is malformed.
35
+ *
36
+ * @param ctx The context to check (TaskCtx, WorkflowCtx, HelperCtx, or SDK Context).
37
+ * @returns True if the context input is a non-null object that passes schema validation.
38
+ * @throws {EventPayloadMalformedException} If the payload fails schema validation.
39
+ */
40
+ cast<C extends {
41
+ input: unknown;
42
+ }>(ctx: C): ctx is C & {
43
+ input: StandardSchemaV1.InferOutput<TSchema>;
44
+ };
45
+ /**
46
+ * Type guard: checks if the context was triggered by this event.
47
+ * Validates the input against the event schema and throws if malformed.
48
+ *
49
+ * @param ctx The context to check (TaskCtx, WorkflowCtx, HelperCtx, or SDK Context).
50
+ * @returns True if the context input contains the event marker matching this event.
51
+ * @throws {EventPayloadMalformedException} If the event marker matches but the payload fails schema validation.
52
+ */
53
+ isCtx<C extends {
54
+ input: unknown;
55
+ }>(ctx: C): ctx is C & {
56
+ input: StandardSchemaV1.InferOutput<TSchema> & {
57
+ [EVENT_MARKER]: TName;
58
+ };
59
+ };
60
+ }
61
+ /**
62
+ * Any event definition, used for generic constraints.
63
+ */
64
+ type AnyEventDefinition = EventDefinition<string, StandardSchemaV1>;
65
+ /**
66
+ * Extracts the input type from an event definition.
67
+ */
68
+ type EventInput<E extends AnyEventDefinition> = E["__types"]["input"];
69
+ /**
70
+ * Extracts the output type from an event definition.
71
+ */
72
+ type EventOutput<E extends AnyEventDefinition> = E["__types"]["output"];
73
+ /**
74
+ * Factory function to create a type-safe event definition.
75
+ *
76
+ * @param name The event name (e.g., "user:created").
77
+ * @param schema A StandardSchema-compatible schema (e.g., Zod schema).
78
+ * @returns A new EventDefinition instance.
79
+ *
80
+ * @example
81
+ * ```typescript
82
+ * import { z } from "zod";
83
+ *
84
+ * const UserCreatedEvent = defineEvent("user:created", z.object({
85
+ * userId: z.string(),
86
+ * email: z.string().email(),
87
+ * }));
88
+ * ```
89
+ */
90
+ declare const defineEvent: <TName extends string, TSchema extends StandardSchemaV1>(name: TName, schema: TSchema) => EventDefinition<TName, TSchema>;
91
+ //#endregion
92
+ export { AnyEventDefinition, EventDefinition, EventInput, EventOutput, defineEvent };
@@ -0,0 +1,92 @@
1
+ import { StandardSchemaV1 } from "@standard-schema/spec";
2
+
3
+ //#region src/events/event-definition.d.ts
4
+ /**
5
+ * Marker embedded in event payload to identify an event type.
6
+ * Used by isCtx() type guard to determine which event triggered the workflow.
7
+ */
8
+ declare const EVENT_MARKER = "__event_name";
9
+ /**
10
+ * Type-safe event definition.
11
+ * Use with Client.emit() to publish events.
12
+ *
13
+ * @template TName The literal string type of the event name.
14
+ * @template TSchema The StandardSchema type for input validation.
15
+ */
16
+ declare class EventDefinition<TName extends string, TSchema extends StandardSchemaV1> {
17
+ readonly name: TName;
18
+ readonly schema: TSchema;
19
+ /**
20
+ * Phantom types for input/output inference.
21
+ * Do not access at runtime - will throw.
22
+ */
23
+ readonly __types: {
24
+ input: StandardSchemaV1.InferInput<TSchema>;
25
+ output: StandardSchemaV1.InferOutput<TSchema>;
26
+ };
27
+ constructor(name: TName, schema: TSchema);
28
+ /**
29
+ * Type guard: casts the context input to this event's schema output type
30
+ * without requiring the event marker to be present.
31
+ *
32
+ * Use this for externally sourced events that arrive without the marker
33
+ * injected by Client.emit(). Schema validation still runs and throws if
34
+ * the payload is malformed.
35
+ *
36
+ * @param ctx The context to check (TaskCtx, WorkflowCtx, HelperCtx, or SDK Context).
37
+ * @returns True if the context input is a non-null object that passes schema validation.
38
+ * @throws {EventPayloadMalformedException} If the payload fails schema validation.
39
+ */
40
+ cast<C extends {
41
+ input: unknown;
42
+ }>(ctx: C): ctx is C & {
43
+ input: StandardSchemaV1.InferOutput<TSchema>;
44
+ };
45
+ /**
46
+ * Type guard: checks if the context was triggered by this event.
47
+ * Validates the input against the event schema and throws if malformed.
48
+ *
49
+ * @param ctx The context to check (TaskCtx, WorkflowCtx, HelperCtx, or SDK Context).
50
+ * @returns True if the context input contains the event marker matching this event.
51
+ * @throws {EventPayloadMalformedException} If the event marker matches but the payload fails schema validation.
52
+ */
53
+ isCtx<C extends {
54
+ input: unknown;
55
+ }>(ctx: C): ctx is C & {
56
+ input: StandardSchemaV1.InferOutput<TSchema> & {
57
+ [EVENT_MARKER]: TName;
58
+ };
59
+ };
60
+ }
61
+ /**
62
+ * Any event definition, used for generic constraints.
63
+ */
64
+ type AnyEventDefinition = EventDefinition<string, StandardSchemaV1>;
65
+ /**
66
+ * Extracts the input type from an event definition.
67
+ */
68
+ type EventInput<E extends AnyEventDefinition> = E["__types"]["input"];
69
+ /**
70
+ * Extracts the output type from an event definition.
71
+ */
72
+ type EventOutput<E extends AnyEventDefinition> = E["__types"]["output"];
73
+ /**
74
+ * Factory function to create a type-safe event definition.
75
+ *
76
+ * @param name The event name (e.g., "user:created").
77
+ * @param schema A StandardSchema-compatible schema (e.g., Zod schema).
78
+ * @returns A new EventDefinition instance.
79
+ *
80
+ * @example
81
+ * ```typescript
82
+ * import { z } from "zod";
83
+ *
84
+ * const UserCreatedEvent = defineEvent("user:created", z.object({
85
+ * userId: z.string(),
86
+ * email: z.string().email(),
87
+ * }));
88
+ * ```
89
+ */
90
+ declare const defineEvent: <TName extends string, TSchema extends StandardSchemaV1>(name: TName, schema: TSchema) => EventDefinition<TName, TSchema>;
91
+ //#endregion
92
+ export { AnyEventDefinition, EventDefinition, EventInput, EventOutput, defineEvent };
@@ -0,0 +1 @@
1
+ import { AnyEventDefinition, EventDefinition, EventInput, EventOutput, defineEvent } from "./event-definition.cjs";
@@ -0,0 +1 @@
1
+ import { AnyEventDefinition, EventDefinition, EventInput, EventOutput, defineEvent } from "./event-definition.mjs";