@temporal-contract/contract 0.1.0 → 0.2.0

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.
package/dist/index.cjs CHANGED
@@ -1,3 +1,4 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1
2
  let zod = require("zod");
2
3
 
3
4
  //#region src/builder.ts
@@ -227,21 +228,21 @@ function isStandardSchema(value) {
227
228
  */
228
229
  const identifierSchema = zod.z.string().min(1).regex(/^[a-zA-Z_$][a-zA-Z0-9_$]*$/, "must be a valid JavaScript identifier");
229
230
  /**
230
- * Extract clean error message from Zod validation error
231
+ * Extract a clean, single-line error message from a Zod validation error.
232
+ *
233
+ * Uses `error.issues` directly (compatible with Zod v4+) rather than parsing
234
+ * `error.message` as JSON, which was a Zod v3 implementation detail.
231
235
  */
232
236
  function getCleanErrorMessage(error) {
233
- try {
234
- const parsed = JSON.parse(error.message);
235
- if (Array.isArray(parsed) && parsed.length > 0) {
236
- const firstError = parsed[0];
237
- if (firstError?.code === "invalid_key" && firstError?.issues && firstError.issues.length > 0) {
238
- const nestedError = firstError.issues[0];
239
- if (nestedError?.message) return nestedError.message;
240
- }
241
- if (firstError?.message) return firstError.message;
242
- }
243
- } catch {}
244
- return error.message;
237
+ const issues = error.issues;
238
+ if (!issues || issues.length === 0) return error.message;
239
+ const firstIssue = issues[0];
240
+ if (!firstIssue) return error.message;
241
+ if (firstIssue.code === "invalid_key" && "issues" in firstIssue && Array.isArray(firstIssue.issues) && firstIssue.issues.length > 0) {
242
+ const nestedMessage = firstIssue.issues[0]?.message;
243
+ if (nestedMessage) return nestedMessage;
244
+ }
245
+ return firstIssue.message ?? error.message;
245
246
  }
246
247
  /**
247
248
  * Schema for validating activity definitions
package/dist/index.d.cts CHANGED
@@ -10,49 +10,49 @@ type AnySchema = StandardSchemaV1;
10
10
  /**
11
11
  * Definition of an activity
12
12
  */
13
- interface ActivityDefinition<TInput extends AnySchema = AnySchema, TOutput extends AnySchema = AnySchema> {
13
+ type ActivityDefinition<TInput extends AnySchema = AnySchema, TOutput extends AnySchema = AnySchema> = {
14
14
  readonly input: TInput;
15
15
  readonly output: TOutput;
16
- }
16
+ };
17
17
  /**
18
18
  * Definition of a signal
19
19
  */
20
- interface SignalDefinition<TInput extends AnySchema = AnySchema> {
20
+ type SignalDefinition<TInput extends AnySchema = AnySchema> = {
21
21
  readonly input: TInput;
22
- }
22
+ };
23
23
  /**
24
24
  * Definition of a query
25
25
  */
26
- interface QueryDefinition<TInput extends AnySchema = AnySchema, TOutput extends AnySchema = AnySchema> {
26
+ type QueryDefinition<TInput extends AnySchema = AnySchema, TOutput extends AnySchema = AnySchema> = {
27
27
  readonly input: TInput;
28
28
  readonly output: TOutput;
29
- }
29
+ };
30
30
  /**
31
31
  * Definition of an update
32
32
  */
33
- interface UpdateDefinition<TInput extends AnySchema = AnySchema, TOutput extends AnySchema = AnySchema> {
33
+ type UpdateDefinition<TInput extends AnySchema = AnySchema, TOutput extends AnySchema = AnySchema> = {
34
34
  readonly input: TInput;
35
35
  readonly output: TOutput;
36
- }
36
+ };
37
37
  /**
38
38
  * Definition of a workflow
39
39
  */
40
- interface WorkflowDefinition<TActivities extends Record<string, ActivityDefinition> = Record<string, ActivityDefinition>, TSignals extends Record<string, SignalDefinition> = Record<string, SignalDefinition>, TQueries extends Record<string, QueryDefinition> = Record<string, QueryDefinition>, TUpdates extends Record<string, UpdateDefinition> = Record<string, UpdateDefinition>> {
40
+ type WorkflowDefinition<TActivities extends Record<string, ActivityDefinition> = Record<string, ActivityDefinition>, TSignals extends Record<string, SignalDefinition> = Record<string, SignalDefinition>, TQueries extends Record<string, QueryDefinition> = Record<string, QueryDefinition>, TUpdates extends Record<string, UpdateDefinition> = Record<string, UpdateDefinition>> = {
41
41
  readonly input: AnySchema;
42
42
  readonly output: AnySchema;
43
43
  readonly activities?: TActivities;
44
44
  readonly signals?: TSignals;
45
45
  readonly queries?: TQueries;
46
46
  readonly updates?: TUpdates;
47
- }
47
+ };
48
48
  /**
49
49
  * Contract definition containing workflows and optional global activities
50
50
  */
51
- interface ContractDefinition<TWorkflows extends Record<string, WorkflowDefinition> = Record<string, WorkflowDefinition>, TActivities extends Record<string, ActivityDefinition> = Record<string, ActivityDefinition>> {
51
+ type ContractDefinition<TWorkflows extends Record<string, WorkflowDefinition> = Record<string, WorkflowDefinition>, TActivities extends Record<string, ActivityDefinition> = Record<string, ActivityDefinition>> = {
52
52
  readonly taskQueue: string;
53
53
  readonly workflows: TWorkflows;
54
54
  readonly activities?: TActivities;
55
- }
55
+ };
56
56
  /**
57
57
  * UTILITY TYPES
58
58
  */
@@ -282,4 +282,5 @@ declare function defineWorkflow<TWorkflow extends WorkflowDefinition>(definition
282
282
  */
283
283
  declare function defineContract<TContract extends ContractDefinition>(definition: TContract): TContract;
284
284
  //#endregion
285
- export { type ActivityDefinition, type AnySchema, type ContractDefinition, type InferActivityNames, type InferContractWorkflows, type InferWorkflowNames, type QueryDefinition, type SignalDefinition, type UpdateDefinition, type WorkflowDefinition, defineActivity, defineContract, defineQuery, defineSignal, defineUpdate, defineWorkflow };
285
+ export { type ActivityDefinition, type AnySchema, type ContractDefinition, type InferActivityNames, type InferContractWorkflows, type InferWorkflowNames, type QueryDefinition, type SignalDefinition, type UpdateDefinition, type WorkflowDefinition, defineActivity, defineContract, defineQuery, defineSignal, defineUpdate, defineWorkflow };
286
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.cts","names":[],"sources":["../src/types.ts","../src/builder.ts"],"mappings":";;;;;AAOA;;;KAAY,SAAA,GAAY,gBAAA;;AAKxB;;KAAY,kBAAA,gBACK,SAAA,GAAY,SAAA,kBACX,SAAA,GAAY,SAAA;EAAA,SAEnB,KAAA,EAAO,MAAA;EAAA,SACP,MAAA,EAAQ,OAAA;AAAA;;;;KAMP,gBAAA,gBAAgC,SAAA,GAAY,SAAA;EAAA,SAC7C,KAAA,EAAO,MAAA;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;;;;;;;;;;;;AAOA;;;;;;;;AAPA,iBCgDgB,YAAA,iBAA6B,gBAAA,CAAA,CAAkB,UAAA,EAAY,OAAA,GAAU,OAAA;;;;;;;;;;;;;;;AD9BrF;;;;;;;;;;iBC0DgB,WAAA,gBAA2B,eAAA,CAAA,CAAiB,UAAA,EAAY,MAAA,GAAS,MAAA;;;;;;;;;;;;;AD/CjF;;;;;;;;;;;;;;;;iBC+EgB,YAAA,iBAA6B,gBAAA,CAAA,CAAkB,UAAA,EAAY,OAAA,GAAU,OAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAqCrE,cAAA,mBAAiC,kBAAA,CAAA,CAC/C,UAAA,EAAY,SAAA,GACX,SAAA;;;;;;;;;;;ADrGH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsBA;;;;;;;;;iBCsIgB,cAAA,mBAAiC,kBAAA,CAAA,CAC/C,UAAA,EAAY,SAAA,GACX,SAAA"}
package/dist/index.d.mts CHANGED
@@ -10,49 +10,49 @@ type AnySchema = StandardSchemaV1;
10
10
  /**
11
11
  * Definition of an activity
12
12
  */
13
- interface ActivityDefinition<TInput extends AnySchema = AnySchema, TOutput extends AnySchema = AnySchema> {
13
+ type ActivityDefinition<TInput extends AnySchema = AnySchema, TOutput extends AnySchema = AnySchema> = {
14
14
  readonly input: TInput;
15
15
  readonly output: TOutput;
16
- }
16
+ };
17
17
  /**
18
18
  * Definition of a signal
19
19
  */
20
- interface SignalDefinition<TInput extends AnySchema = AnySchema> {
20
+ type SignalDefinition<TInput extends AnySchema = AnySchema> = {
21
21
  readonly input: TInput;
22
- }
22
+ };
23
23
  /**
24
24
  * Definition of a query
25
25
  */
26
- interface QueryDefinition<TInput extends AnySchema = AnySchema, TOutput extends AnySchema = AnySchema> {
26
+ type QueryDefinition<TInput extends AnySchema = AnySchema, TOutput extends AnySchema = AnySchema> = {
27
27
  readonly input: TInput;
28
28
  readonly output: TOutput;
29
- }
29
+ };
30
30
  /**
31
31
  * Definition of an update
32
32
  */
33
- interface UpdateDefinition<TInput extends AnySchema = AnySchema, TOutput extends AnySchema = AnySchema> {
33
+ type UpdateDefinition<TInput extends AnySchema = AnySchema, TOutput extends AnySchema = AnySchema> = {
34
34
  readonly input: TInput;
35
35
  readonly output: TOutput;
36
- }
36
+ };
37
37
  /**
38
38
  * Definition of a workflow
39
39
  */
40
- interface WorkflowDefinition<TActivities extends Record<string, ActivityDefinition> = Record<string, ActivityDefinition>, TSignals extends Record<string, SignalDefinition> = Record<string, SignalDefinition>, TQueries extends Record<string, QueryDefinition> = Record<string, QueryDefinition>, TUpdates extends Record<string, UpdateDefinition> = Record<string, UpdateDefinition>> {
40
+ type WorkflowDefinition<TActivities extends Record<string, ActivityDefinition> = Record<string, ActivityDefinition>, TSignals extends Record<string, SignalDefinition> = Record<string, SignalDefinition>, TQueries extends Record<string, QueryDefinition> = Record<string, QueryDefinition>, TUpdates extends Record<string, UpdateDefinition> = Record<string, UpdateDefinition>> = {
41
41
  readonly input: AnySchema;
42
42
  readonly output: AnySchema;
43
43
  readonly activities?: TActivities;
44
44
  readonly signals?: TSignals;
45
45
  readonly queries?: TQueries;
46
46
  readonly updates?: TUpdates;
47
- }
47
+ };
48
48
  /**
49
49
  * Contract definition containing workflows and optional global activities
50
50
  */
51
- interface ContractDefinition<TWorkflows extends Record<string, WorkflowDefinition> = Record<string, WorkflowDefinition>, TActivities extends Record<string, ActivityDefinition> = Record<string, ActivityDefinition>> {
51
+ type ContractDefinition<TWorkflows extends Record<string, WorkflowDefinition> = Record<string, WorkflowDefinition>, TActivities extends Record<string, ActivityDefinition> = Record<string, ActivityDefinition>> = {
52
52
  readonly taskQueue: string;
53
53
  readonly workflows: TWorkflows;
54
54
  readonly activities?: TActivities;
55
- }
55
+ };
56
56
  /**
57
57
  * UTILITY TYPES
58
58
  */
@@ -282,4 +282,5 @@ declare function defineWorkflow<TWorkflow extends WorkflowDefinition>(definition
282
282
  */
283
283
  declare function defineContract<TContract extends ContractDefinition>(definition: TContract): TContract;
284
284
  //#endregion
285
- export { type ActivityDefinition, type AnySchema, type ContractDefinition, type InferActivityNames, type InferContractWorkflows, type InferWorkflowNames, type QueryDefinition, type SignalDefinition, type UpdateDefinition, type WorkflowDefinition, defineActivity, defineContract, defineQuery, defineSignal, defineUpdate, defineWorkflow };
285
+ export { type ActivityDefinition, type AnySchema, type ContractDefinition, type InferActivityNames, type InferContractWorkflows, type InferWorkflowNames, type QueryDefinition, type SignalDefinition, type UpdateDefinition, type WorkflowDefinition, defineActivity, defineContract, defineQuery, defineSignal, defineUpdate, defineWorkflow };
286
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/types.ts","../src/builder.ts"],"mappings":";;;;;AAOA;;;KAAY,SAAA,GAAY,gBAAA;;AAKxB;;KAAY,kBAAA,gBACK,SAAA,GAAY,SAAA,kBACX,SAAA,GAAY,SAAA;EAAA,SAEnB,KAAA,EAAO,MAAA;EAAA,SACP,MAAA,EAAQ,OAAA;AAAA;;;;KAMP,gBAAA,gBAAgC,SAAA,GAAY,SAAA;EAAA,SAC7C,KAAA,EAAO,MAAA;AAAA;;;;KAMN,eAAA,gBACK,SAAA,GAAY,SAAA,kBACX,SAAA,GAAY,SAAA;EAAA,SAEnB,KAAA,EAAO,MAAA;EAAA,SACP,MAAA,EAAQ,OAAA;AAAA;;;;KAMP,gBAAA,gBACK,SAAA,GAAY,SAAA,kBACX,SAAA,GAAY,SAAA;EAAA,SAEnB,KAAA,EAAO,MAAA;EAAA,SACP,MAAA,EAAQ,OAAA;AAAA;;;;KAMP,kBAAA,qBACU,MAAA,SAAe,kBAAA,IAAsB,MAAA,SAAe,kBAAA,oBACvD,MAAA,SAAe,gBAAA,IAAoB,MAAA,SAAe,gBAAA,oBAClD,MAAA,SAAe,eAAA,IAAmB,MAAA,SAAe,eAAA,oBACjD,MAAA,SAAe,gBAAA,IAAoB,MAAA,SAAe,gBAAA;EAAA,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;;;AAsBA;;;;;;;;;iBCsIgB,cAAA,mBAAiC,kBAAA,CAAA,CAC/C,UAAA,EAAY,SAAA,GACX,SAAA"}
package/dist/index.mjs CHANGED
@@ -227,21 +227,21 @@ function isStandardSchema(value) {
227
227
  */
228
228
  const identifierSchema = z.string().min(1).regex(/^[a-zA-Z_$][a-zA-Z0-9_$]*$/, "must be a valid JavaScript identifier");
229
229
  /**
230
- * Extract clean error message from Zod validation error
230
+ * Extract a clean, single-line error message from a Zod validation error.
231
+ *
232
+ * Uses `error.issues` directly (compatible with Zod v4+) rather than parsing
233
+ * `error.message` as JSON, which was a Zod v3 implementation detail.
231
234
  */
232
235
  function getCleanErrorMessage(error) {
233
- try {
234
- const parsed = JSON.parse(error.message);
235
- if (Array.isArray(parsed) && parsed.length > 0) {
236
- const firstError = parsed[0];
237
- if (firstError?.code === "invalid_key" && firstError?.issues && firstError.issues.length > 0) {
238
- const nestedError = firstError.issues[0];
239
- if (nestedError?.message) return nestedError.message;
240
- }
241
- if (firstError?.message) return firstError.message;
242
- }
243
- } catch {}
244
- return error.message;
236
+ const issues = error.issues;
237
+ if (!issues || issues.length === 0) return error.message;
238
+ const firstIssue = issues[0];
239
+ if (!firstIssue) return error.message;
240
+ if (firstIssue.code === "invalid_key" && "issues" in firstIssue && Array.isArray(firstIssue.issues) && firstIssue.issues.length > 0) {
241
+ const nestedMessage = firstIssue.issues[0]?.message;
242
+ if (nestedMessage) return nestedMessage;
243
+ }
244
+ return firstIssue.message ?? error.message;
245
245
  }
246
246
  /**
247
247
  * Schema for validating activity definitions
@@ -301,4 +301,5 @@ const contractValidationSchema = z.object({
301
301
  });
302
302
 
303
303
  //#endregion
304
- export { defineActivity, defineContract, defineQuery, defineSignal, defineUpdate, defineWorkflow };
304
+ export { defineActivity, defineContract, defineQuery, defineSignal, defineUpdate, defineWorkflow };
305
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/builder.ts"],"sourcesContent":["import { z } from \"zod\";\nimport type { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport type {\n ActivityDefinition,\n ContractDefinition,\n QueryDefinition,\n SignalDefinition,\n UpdateDefinition,\n WorkflowDefinition,\n} from \"./types.js\";\n\n// Exported builders first (classic functions for hoisting)\n\n/**\n * Define a Temporal activity with type-safe input and output schemas.\n *\n * Activities are the building blocks of Temporal workflows that execute business logic\n * and interact with external services. This function preserves TypeScript types while\n * providing a consistent structure for activity definitions.\n *\n * @template TActivity - The activity definition type with input/output schemas\n * @param definition - The activity definition containing input and output schemas\n * @returns The same definition with preserved types for type inference\n *\n * @example\n * ```typescript\n * import { defineActivity } from '@temporal-contract/contract';\n * import { z } from 'zod';\n *\n * export const sendEmail = defineActivity({\n * input: z.object({\n * to: z.string().email(),\n * subject: z.string(),\n * body: z.string(),\n * }),\n * output: z.object({\n * messageId: z.string(),\n * sentAt: z.date(),\n * }),\n * });\n * ```\n */\nexport function defineActivity<TActivity extends ActivityDefinition>(\n definition: TActivity,\n): TActivity {\n return definition;\n}\n\n/**\n * Define a Temporal signal with type-safe input schema.\n *\n * Signals are asynchronous messages sent to running workflows to update their state\n * or trigger certain behaviors. This function ensures type safety for signal payloads.\n *\n * @template TSignal - The signal definition type with input schema\n * @param definition - The signal definition containing input schema\n * @returns The same definition with preserved types for type inference\n *\n * @example\n * ```typescript\n * import { defineSignal } from '@temporal-contract/contract';\n * import { z } from 'zod';\n *\n * export const approveOrder = defineSignal({\n * input: z.object({\n * orderId: z.string(),\n * approvedBy: z.string(),\n * }),\n * });\n * ```\n */\nexport function defineSignal<TSignal extends SignalDefinition>(definition: TSignal): TSignal {\n return definition;\n}\n\n/**\n * Define a Temporal query with type-safe input and output schemas.\n *\n * Queries allow you to read the current state of a running workflow without\n * modifying it. They are synchronous and should not perform any mutations.\n *\n * @template TQuery - The query definition type with input/output schemas\n * @param definition - The query definition containing input and output schemas\n * @returns The same definition with preserved types for type inference\n *\n * @example\n * ```typescript\n * import { defineQuery } from '@temporal-contract/contract';\n * import { z } from 'zod';\n *\n * export const getOrderStatus = defineQuery({\n * input: z.object({ orderId: z.string() }),\n * output: z.object({\n * status: z.enum(['pending', 'processing', 'completed', 'failed']),\n * updatedAt: z.date(),\n * }),\n * });\n * ```\n */\nexport function defineQuery<TQuery extends QueryDefinition>(definition: TQuery): TQuery {\n return definition;\n}\n\n/**\n * Define a Temporal update with type-safe input and output schemas.\n *\n * Updates are similar to signals but return a value and wait for the workflow\n * to process them before completing. They provide a synchronous way to modify\n * workflow state and get immediate feedback.\n *\n * @template TUpdate - The update definition type with input/output schemas\n * @param definition - The update definition containing input and output schemas\n * @returns The same definition with preserved types for type inference\n *\n * @example\n * ```typescript\n * import { defineUpdate } from '@temporal-contract/contract';\n * import { z } from 'zod';\n *\n * export const updateOrderQuantity = defineUpdate({\n * input: z.object({\n * orderId: z.string(),\n * newQuantity: z.number().positive(),\n * }),\n * output: z.object({\n * success: z.boolean(),\n * totalPrice: z.number(),\n * }),\n * });\n * ```\n */\nexport function defineUpdate<TUpdate extends UpdateDefinition>(definition: TUpdate): TUpdate {\n return definition;\n}\n\n/**\n * Define a Temporal workflow with type-safe input, output, and associated operations.\n *\n * Workflows are durable functions that orchestrate activities, handle timeouts,\n * and manage long-running processes. This function provides type safety for the\n * entire workflow definition including activities, signals, queries, and updates.\n *\n * @template TWorkflow - The workflow definition type with all associated schemas\n * @param definition - The workflow definition containing input, output, and operations\n * @returns The same definition with preserved types for type inference\n *\n * @example\n * ```typescript\n * import { defineWorkflow, defineActivity, defineSignal } from '@temporal-contract/contract';\n * import { z } from 'zod';\n *\n * export const processOrder = defineWorkflow({\n * input: z.object({ orderId: z.string() }),\n * output: z.object({ success: z.boolean() }),\n * activities: {\n * validatePayment: defineActivity({\n * input: z.object({ orderId: z.string() }),\n * output: z.object({ valid: z.boolean() }),\n * }),\n * },\n * signals: {\n * cancel: defineSignal({\n * input: z.object({ reason: z.string() }),\n * }),\n * },\n * });\n * ```\n */\nexport function defineWorkflow<TWorkflow extends WorkflowDefinition>(\n definition: TWorkflow,\n): TWorkflow {\n return definition;\n}\n\n/**\n * Define a complete Temporal contract with type-safe workflows and activities.\n *\n * A contract is the central definition that ties together your Temporal application's\n * workflows and activities. It provides:\n * - Type safety across client, worker, and workflow code\n * - Automatic validation at runtime\n * - Compile-time verification of implementations\n * - Clear API boundaries and documentation\n *\n * The contract validates the structure and ensures:\n * - Task queue is specified\n * - At least one workflow is defined\n * - Valid JavaScript identifiers are used\n * - No conflicts between global and workflow-specific activities\n * - All schemas implement the Standard Schema specification\n *\n * @template TContract - The contract definition type\n * @param definition - The complete contract definition\n * @returns The same definition with preserved types for type inference\n * @throws {Error} If the contract structure is invalid\n *\n * @example\n * ```typescript\n * import { defineContract } from '@temporal-contract/contract';\n * import { z } from 'zod';\n *\n * export const myContract = defineContract({\n * taskQueue: 'orders',\n * workflows: {\n * processOrder: {\n * input: z.object({ orderId: z.string() }),\n * output: z.object({ success: z.boolean() }),\n * activities: {\n * chargePayment: {\n * input: z.object({ amount: z.number() }),\n * output: z.object({ transactionId: z.string() }),\n * },\n * },\n * },\n * },\n * // Optional global activities shared across workflows\n * activities: {\n * logEvent: {\n * input: z.object({ message: z.string() }),\n * output: z.void(),\n * },\n * },\n * });\n * ```\n */\nexport function defineContract<TContract extends ContractDefinition>(\n definition: TContract,\n): TContract {\n // Validate entire contract structure with Zod (including activity conflicts)\n const validationResult = contractValidationSchema.safeParse(definition);\n\n if (!validationResult.success) {\n const cleanMessage = getCleanErrorMessage(validationResult.error);\n throw new Error(`Contract validation failed: ${cleanMessage}`);\n }\n\n return definition;\n}\n\n/**\n * Check if a value is a Standard Schema compatible schema\n */\nfunction isStandardSchema(value: unknown): value is StandardSchemaV1 {\n // Standard Schema can be either an object or a function (e.g., ArkType)\n if (\n (typeof value !== \"object\" && typeof value !== \"function\") ||\n value === null ||\n !(\"~standard\" in value)\n ) {\n return false;\n }\n\n const standard = (value as Record<string, unknown>)[\"~standard\"];\n\n return (\n typeof standard === \"object\" &&\n standard !== null &&\n (standard as Record<string, unknown>)[\"version\"] === 1 &&\n typeof (standard as Record<string, unknown>)[\"validate\"] === \"function\"\n );\n}\n\n/**\n * Schema for validating JavaScript identifiers (workflow names, activity names, etc.)\n * Allows: letters, digits, underscore, dollar sign\n * Must start with: letter, underscore, or dollar sign\n */\nconst identifierSchema = z\n .string()\n .min(1)\n .regex(/^[a-zA-Z_$][a-zA-Z0-9_$]*$/, \"must be a valid JavaScript identifier\");\n\n/**\n * Extract a clean, single-line error message from a Zod validation error.\n *\n * Uses `error.issues` directly (compatible with Zod v4+) rather than parsing\n * `error.message` as JSON, which was a Zod v3 implementation detail.\n */\nfunction getCleanErrorMessage(error: z.ZodError): string {\n const issues = error.issues;\n if (!issues || issues.length === 0) {\n return error.message;\n }\n\n const firstIssue = issues[0];\n if (!firstIssue) {\n return error.message;\n }\n\n // For record key validation errors (invalid_key), surface the nested issue message\n if (\n firstIssue.code === \"invalid_key\" &&\n \"issues\" in firstIssue &&\n Array.isArray((firstIssue as { issues?: unknown[] }).issues) &&\n (firstIssue as { issues: { message?: string }[] }).issues.length > 0\n ) {\n const nestedMessage = (firstIssue as { issues: { message?: string }[] }).issues[0]?.message;\n if (nestedMessage) {\n return nestedMessage;\n }\n }\n\n return firstIssue.message ?? error.message;\n}\n\n/**\n * Schema for validating activity definitions\n * Checks that input and output are Standard Schema compatible schemas\n */\nconst activityDefinitionSchema = z.object({\n input: z.custom<StandardSchemaV1>((val) => isStandardSchema(val), {\n message: \"input must be a Standard Schema compatible schema (e.g., Zod, Valibot, ArkType)\",\n }),\n output: z.custom<StandardSchemaV1>((val) => isStandardSchema(val), {\n message: \"output must be a Standard Schema compatible schema (e.g., Zod, Valibot, ArkType)\",\n }),\n});\n\n/**\n * Schema for validating signal definitions\n */\nconst signalDefinitionSchema = z.object({\n input: z.custom<StandardSchemaV1>((val) => isStandardSchema(val), {\n message: \"input must be a Standard Schema compatible schema (e.g., Zod, Valibot, ArkType)\",\n }),\n});\n\n/**\n * Schema for validating query definitions\n */\nconst queryDefinitionSchema = z.object({\n input: z.custom<StandardSchemaV1>((val) => isStandardSchema(val), {\n message: \"input must be a Standard Schema compatible schema (e.g., Zod, Valibot, ArkType)\",\n }),\n output: z.custom<StandardSchemaV1>((val) => isStandardSchema(val), {\n message: \"output must be a Standard Schema compatible schema (e.g., Zod, Valibot, ArkType)\",\n }),\n});\n\n/**\n * Schema for validating update definitions\n */\nconst updateDefinitionSchema = z.object({\n input: z.custom<StandardSchemaV1>((val) => isStandardSchema(val), {\n message: \"input must be a Standard Schema compatible schema (e.g., Zod, Valibot, ArkType)\",\n }),\n output: z.custom<StandardSchemaV1>((val) => isStandardSchema(val), {\n message: \"output must be a Standard Schema compatible schema (e.g., Zod, Valibot, ArkType)\",\n }),\n});\n\n/**\n * Schema for validating workflow definitions\n */\nconst workflowDefinitionSchema = z.object({\n input: z.custom<StandardSchemaV1>((val) => isStandardSchema(val), {\n message: \"input must be a Standard Schema compatible schema (e.g., Zod, Valibot, ArkType)\",\n }),\n output: z.custom<StandardSchemaV1>((val) => isStandardSchema(val), {\n message: \"output must be a Standard Schema compatible schema (e.g., Zod, Valibot, ArkType)\",\n }),\n activities: z.record(identifierSchema, activityDefinitionSchema).optional(),\n signals: z.record(identifierSchema, signalDefinitionSchema).optional(),\n queries: z.record(identifierSchema, queryDefinitionSchema).optional(),\n updates: z.record(identifierSchema, updateDefinitionSchema).optional(),\n});\n\n/**\n * Schema for validating a contract definition structure\n */\nconst contractValidationSchema = z\n .object({\n taskQueue: z.string().trim().min(1, \"taskQueue cannot be empty\"),\n workflows: z\n .record(identifierSchema, workflowDefinitionSchema)\n .refine((workflows) => Object.keys(workflows).length > 0, {\n message: \"at least one workflow is required\",\n }),\n activities: z.record(identifierSchema, activityDefinitionSchema).optional(),\n })\n .superRefine((contract, ctx) => {\n // Check for conflicts between global and workflow-specific activities\n if (!contract.activities) {\n return;\n }\n\n for (const [workflowName, workflow] of Object.entries(contract.workflows)) {\n if (workflow.activities) {\n for (const activityName of Object.keys(workflow.activities)) {\n if (activityName in contract.activities) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message: `workflow \"${workflowName}\" has activity \"${activityName}\" that conflicts with a global activity. Consider renaming the workflow-specific activity or removing the global activity \"${activityName}\".`,\n });\n return;\n }\n }\n }\n }\n });\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0CA,SAAgB,eACd,YACW;AACX,QAAO;;;;;;;;;;;;;;;;;;;;;;;;;AA0BT,SAAgB,aAA+C,YAA8B;AAC3F,QAAO;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BT,SAAgB,YAA4C,YAA4B;AACtF,QAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+BT,SAAgB,aAA+C,YAA8B;AAC3F,QAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoCT,SAAgB,eACd,YACW;AACX,QAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsDT,SAAgB,eACd,YACW;CAEX,MAAM,mBAAmB,yBAAyB,UAAU,WAAW;AAEvE,KAAI,CAAC,iBAAiB,SAAS;EAC7B,MAAM,eAAe,qBAAqB,iBAAiB,MAAM;AACjE,QAAM,IAAI,MAAM,+BAA+B,eAAe;;AAGhE,QAAO;;;;;AAMT,SAAS,iBAAiB,OAA2C;AAEnE,KACG,OAAO,UAAU,YAAY,OAAO,UAAU,cAC/C,UAAU,QACV,EAAE,eAAe,OAEjB,QAAO;CAGT,MAAM,WAAY,MAAkC;AAEpD,QACE,OAAO,aAAa,YACpB,aAAa,QACZ,SAAqC,eAAe,KACrD,OAAQ,SAAqC,gBAAgB;;;;;;;AASjE,MAAM,mBAAmB,EACtB,QAAQ,CACR,IAAI,EAAE,CACN,MAAM,8BAA8B,wCAAwC;;;;;;;AAQ/E,SAAS,qBAAqB,OAA2B;CACvD,MAAM,SAAS,MAAM;AACrB,K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EAAE,OAAO,kBAAkB,yBAAyB,CAAC,UAAU;CAC3E,SAAS,EAAE,OAAO,kBAAkB,uBAAuB,CAAC,UAAU;CACtE,SAAS,EAAE,OAAO,kBAAkB,sBAAsB,CAAC,UAAU;CACrE,SAAS,EAAE,OAAO,kBAAkB,uBAAuB,CAAC,UAAU;CACvE,CAAC;;;;AAKF,MAAM,2BAA2B,EAC9B,OAAO;CACN,WAAW,EAAE,QAAQ,CAAC,MAAM,CAAC,IAAI,GAAG,4BAA4B;CAChE,WAAW,EACR,OAAO,kBAAkB,yBAAyB,CAClD,QAAQ,cAAc,OAAO,KAAK,UAAU,CAAC,SAAS,GAAG,EACxD,SAAS,qCACV,CAAC;CACJ,YAAY,EAAE,OAAO,kBAAkB,yBAAyB,CAAC,UAAU;CAC5E,CAAC,CACD,aAAa,UAAU,QAAQ;AAE9B,KAAI,CAAC,SAAS,WACZ;AAGF,MAAK,MAAM,CAAC,cAAc,aAAa,OAAO,QAAQ,SAAS,UAAU,CACvE,KAAI,SAAS,YACX;OAAK,MAAM,gBAAgB,OAAO,KAAK,SAAS,WAAW,CACzD,KAAI,gBAAgB,SAAS,YAAY;AACvC,OAAI,SAAS;IACX,MAAM,EAAE,aAAa;IACrB,SAAS,aAAa,aAAa,kBAAkB,aAAa,6HAA6H,aAAa;IAC7M,CAAC;AACF;;;EAKR"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@temporal-contract/contract",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Contract builder for temporal-contract",
5
5
  "keywords": [
6
6
  "contract",
@@ -40,16 +40,18 @@
40
40
  ],
41
41
  "dependencies": {
42
42
  "@standard-schema/spec": "1.1.0",
43
- "zod": "4.3.5"
43
+ "zod": "4.3.6"
44
44
  },
45
45
  "devDependencies": {
46
- "@vitest/coverage-v8": "4.0.17",
46
+ "@vitest/coverage-v8": "4.0.18",
47
47
  "arktype": "2.1.29",
48
- "tsdown": "0.20.0-beta.4",
48
+ "tsdown": "0.20.3",
49
+ "typedoc": "0.28.17",
50
+ "typedoc-plugin-markdown": "4.10.0",
49
51
  "typescript": "5.9.3",
50
52
  "valibot": "1.2.0",
51
- "vitest": "4.0.17",
52
- "@temporal-contract/tsconfig": "0.1.0",
53
+ "vitest": "4.0.18",
54
+ "@temporal-contract/tsconfig": "0.2.0",
53
55
  "@temporal-contract/typedoc": "0.1.0"
54
56
  },
55
57
  "scripts": {