@abinnovision/nestjs-hatchet 0.5.0 → 0.6.0-beta.1

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.
@@ -24,6 +24,24 @@
24
24
  } });
25
25
  }
26
26
  /**
27
+ * Type guard: casts the context input to this event's schema output type
28
+ * without requiring the event marker to be present.
29
+ *
30
+ * Use this for externally sourced events that arrive without the marker
31
+ * injected by Client.emit(). Schema validation still runs and throws if
32
+ * the payload is malformed.
33
+ *
34
+ * @param ctx The context to check (TaskCtx, WorkflowCtx, HelperCtx, or SDK Context).
35
+ * @returns True if the context input is a non-null object that passes schema validation.
36
+ * @throws Error if the payload fails schema validation.
37
+ */ cast(ctx) {
38
+ if (typeof ctx.input !== "object" || ctx.input === null) return false;
39
+ const result = this.schema["~standard"].validate(ctx.input);
40
+ if (result instanceof Promise) throw new Error(`Event '${this.name}' schema validation must be synchronous. Use a sync schema.`);
41
+ if ("issues" in result && result.issues) throw new Error(`Event '${this.name}' payload is malformed: ${JSON.stringify(result.issues)}`);
42
+ return true;
43
+ }
44
+ /**
27
45
  * Type guard: checks if the context was triggered by this event.
28
46
  * Validates the input against the event schema and throws if malformed.
29
47
  *
@@ -32,10 +50,7 @@
32
50
  * @throws Error if the event marker matches but the payload fails schema validation.
33
51
  */ isCtx(ctx) {
34
52
  if (typeof ctx.input !== "object" || ctx.input === null || !("__event_name" in ctx.input) || ctx.input["__event_name"] !== this.name) return false;
35
- const result = this.schema["~standard"].validate(ctx.input);
36
- if (result instanceof Promise) throw new Error(`Event '${this.name}' schema validation must be synchronous. Use a sync schema.`);
37
- if ("issues" in result && result.issues) throw new Error(`Event '${this.name}' payload is malformed: ${JSON.stringify(result.issues)}`);
38
- return true;
53
+ return this.cast(ctx);
39
54
  }
40
55
  };
41
56
  /**
@@ -23,6 +23,23 @@ export declare class EventDefinition<TName extends string, TSchema extends Stand
23
23
  output: StandardSchemaV1.InferOutput<TSchema>;
24
24
  };
25
25
  constructor(name: TName, schema: TSchema);
26
+ /**
27
+ * Type guard: casts the context input to this event's schema output type
28
+ * without requiring the event marker to be present.
29
+ *
30
+ * Use this for externally sourced events that arrive without the marker
31
+ * injected by Client.emit(). Schema validation still runs and throws if
32
+ * the payload is malformed.
33
+ *
34
+ * @param ctx The context to check (TaskCtx, WorkflowCtx, HelperCtx, or SDK Context).
35
+ * @returns True if the context input is a non-null object that passes schema validation.
36
+ * @throws Error if the payload fails schema validation.
37
+ */
38
+ cast<C extends {
39
+ input: unknown;
40
+ }>(ctx: C): ctx is C & {
41
+ input: StandardSchemaV1.InferOutput<TSchema>;
42
+ };
26
43
  /**
27
44
  * Type guard: checks if the context was triggered by this event.
28
45
  * Validates the input against the event schema and throws if malformed.
@@ -24,6 +24,24 @@
24
24
  } });
25
25
  }
26
26
  /**
27
+ * Type guard: casts the context input to this event's schema output type
28
+ * without requiring the event marker to be present.
29
+ *
30
+ * Use this for externally sourced events that arrive without the marker
31
+ * injected by Client.emit(). Schema validation still runs and throws if
32
+ * the payload is malformed.
33
+ *
34
+ * @param ctx The context to check (TaskCtx, WorkflowCtx, HelperCtx, or SDK Context).
35
+ * @returns True if the context input is a non-null object that passes schema validation.
36
+ * @throws Error if the payload fails schema validation.
37
+ */ cast(ctx) {
38
+ if (typeof ctx.input !== "object" || ctx.input === null) return false;
39
+ const result = this.schema["~standard"].validate(ctx.input);
40
+ if (result instanceof Promise) throw new Error(`Event '${this.name}' schema validation must be synchronous. Use a sync schema.`);
41
+ if ("issues" in result && result.issues) throw new Error(`Event '${this.name}' payload is malformed: ${JSON.stringify(result.issues)}`);
42
+ return true;
43
+ }
44
+ /**
27
45
  * Type guard: checks if the context was triggered by this event.
28
46
  * Validates the input against the event schema and throws if malformed.
29
47
  *
@@ -32,10 +50,7 @@
32
50
  * @throws Error if the event marker matches but the payload fails schema validation.
33
51
  */ isCtx(ctx) {
34
52
  if (typeof ctx.input !== "object" || ctx.input === null || !("__event_name" in ctx.input) || ctx.input["__event_name"] !== this.name) return false;
35
- const result = this.schema["~standard"].validate(ctx.input);
36
- if (result instanceof Promise) throw new Error(`Event '${this.name}' schema validation must be synchronous. Use a sync schema.`);
37
- if ("issues" in result && result.issues) throw new Error(`Event '${this.name}' payload is malformed: ${JSON.stringify(result.issues)}`);
38
- return true;
53
+ return this.cast(ctx);
39
54
  }
40
55
  };
41
56
  /**
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@abinnovision/nestjs-hatchet",
4
- "version": "0.5.0",
4
+ "version": "0.6.0-beta.1",
5
5
  "keywords": [
6
6
  "nestjs",
7
7
  "hatchet"