@temporal-contract/contract 0.0.4 → 0.0.6

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,6 +1,29 @@
1
1
  let zod = require("zod");
2
2
 
3
3
  //#region src/builder.ts
4
+ function defineActivity(definition) {
5
+ return definition;
6
+ }
7
+ function defineSignal(definition) {
8
+ return definition;
9
+ }
10
+ function defineQuery(definition) {
11
+ return definition;
12
+ }
13
+ function defineUpdate(definition) {
14
+ return definition;
15
+ }
16
+ function defineWorkflow(definition) {
17
+ return definition;
18
+ }
19
+ function defineContract(definition) {
20
+ const validationResult = contractValidationSchema.safeParse(definition);
21
+ if (!validationResult.success) {
22
+ const cleanMessage = getCleanErrorMessage(validationResult.error);
23
+ throw new Error(`Contract error: ${cleanMessage}`);
24
+ }
25
+ return definition;
26
+ }
4
27
  /**
5
28
  * Check if a value is a Standard Schema compatible schema
6
29
  */
@@ -18,7 +41,7 @@ const identifierSchema = zod.z.string().min(1).regex(/^[a-zA-Z_$][a-zA-Z0-9_$]*$
18
41
  /**
19
42
  * Extract clean error message from Zod validation error
20
43
  */
21
- const getCleanErrorMessage = (error) => {
44
+ function getCleanErrorMessage(error) {
22
45
  try {
23
46
  const parsed = JSON.parse(error.message);
24
47
  if (Array.isArray(parsed) && parsed.length > 0) {
@@ -31,7 +54,7 @@ const getCleanErrorMessage = (error) => {
31
54
  }
32
55
  } catch {}
33
56
  return error.message;
34
- };
57
+ }
35
58
  /**
36
59
  * Schema for validating activity definitions
37
60
  * Checks that input and output are Standard Schema compatible schemas
@@ -88,106 +111,6 @@ const contractValidationSchema = zod.z.object({
88
111
  }
89
112
  }
90
113
  });
91
- /**
92
- * Builder for creating activity definitions
93
- *
94
- * @example
95
- * ```ts
96
- * const myActivity = defineActivity({
97
- * input: z.tuple([z.object({ name: z.string() })]),
98
- * output: z.object({ greeting: z.string() }),
99
- * });
100
- * ```
101
- */
102
- const defineActivity = (definition) => {
103
- return definition;
104
- };
105
- /**
106
- * Builder for creating signal definitions
107
- *
108
- * @example
109
- * ```ts
110
- * const mySignal = defineSignal({
111
- * input: z.object({ message: z.string() }),
112
- * });
113
- * ```
114
- */
115
- const defineSignal = (definition) => {
116
- return definition;
117
- };
118
- /**
119
- * Builder for creating query definitions
120
- *
121
- * @example
122
- * ```ts
123
- * const myQuery = defineQuery({
124
- * input: z.object({ id: z.string() }),
125
- * output: z.object({ status: z.string() }),
126
- * });
127
- * ```
128
- */
129
- const defineQuery = (definition) => {
130
- return definition;
131
- };
132
- /**
133
- * Builder for creating update definitions
134
- *
135
- * @example
136
- * ```ts
137
- * const myUpdate = defineUpdate({
138
- * input: z.object({ value: z.number() }),
139
- * output: z.object({ newValue: z.number() }),
140
- * });
141
- * ```
142
- */
143
- const defineUpdate = (definition) => {
144
- return definition;
145
- };
146
- /**
147
- * Builder for creating workflow definitions
148
- *
149
- * @example
150
- * ```ts
151
- * const myWorkflow = defineWorkflow({
152
- * input: z.tuple([z.object({ orderId: z.string() })]),
153
- * output: z.object({ status: z.string() }),
154
- * activities: {
155
- * processPayment: defineActivity({
156
- * input: z.tuple([z.object({ amount: z.number() })]),
157
- * output: z.object({ success: z.boolean() }),
158
- * }),
159
- * },
160
- * });
161
- * ```
162
- */
163
- const defineWorkflow = (definition) => {
164
- return definition;
165
- };
166
- /**
167
- * Builder for creating a complete contract
168
- *
169
- * @example
170
- * ```ts
171
- * const myContract = defineContract({
172
- * taskQueue: 'my-service',
173
- * workflows: {
174
- * processOrder: defineWorkflow({ ... }),
175
- * sendNotification: defineWorkflow({ ... }),
176
- * },
177
- * activities: {
178
- * sendEmail: defineActivity({ ... }),
179
- * },
180
- * });
181
- * ```
182
- */
183
- const defineContract = (definition) => {
184
- const validationResult = contractValidationSchema.safeParse(definition);
185
- if (!validationResult.success) {
186
- const cleanMessage = getCleanErrorMessage(validationResult.error);
187
- throw new Error(`Contract error: ${cleanMessage}`);
188
- }
189
- return definition;
190
- };
191
114
 
192
115
  //#endregion
193
116
  //#region src/nexus-types.ts
package/dist/index.d.cts CHANGED
@@ -55,157 +55,7 @@ interface ContractDefinition<TWorkflows extends Record<string, WorkflowDefinitio
55
55
  readonly activities?: TActivities;
56
56
  }
57
57
  /**
58
- * Infer input type from a definition (worker perspective)
59
- * Worker receives the output type (after input schema parsing/transformation)
60
- */
61
- type WorkerInferInput<T extends {
62
- input: AnySchema;
63
- }> = StandardSchemaV1.InferOutput<T["input"]>;
64
- /**
65
- * Infer output type from a definition (worker perspective)
66
- * Worker returns the input type (before output schema parsing/transformation)
67
- */
68
- type WorkerInferOutput<T extends {
69
- output: AnySchema;
70
- }> = StandardSchemaV1.InferInput<T["output"]>;
71
- /**
72
- * Infer input type from a definition (client perspective)
73
- * Client sends the input type (before input schema parsing/transformation)
74
- */
75
- type ClientInferInput<T extends {
76
- input: AnySchema;
77
- }> = StandardSchemaV1.InferInput<T["input"]>;
78
- /**
79
- * Infer output type from a definition (client perspective)
80
- * Client receives the output type (after output schema parsing/transformation)
81
- */
82
- type ClientInferOutput<T extends {
83
- output: AnySchema;
84
- }> = StandardSchemaV1.InferOutput<T["output"]>;
85
- /**
86
- * WORKER PERSPECTIVE
87
- * Worker receives z.output of input (parsed data) and returns z.input of output (raw data)
88
- */
89
- /**
90
- * Infer workflow function signature from worker perspective
91
- * Worker receives z.input and returns z.output
92
- */
93
- type WorkerInferWorkflow<TWorkflow extends WorkflowDefinition> = (args: WorkerInferInput<TWorkflow>) => Promise<WorkerInferOutput<TWorkflow>>;
94
- /**
95
- * Infer activity function signature from worker perspective
96
- * Worker receives z.input and returns z.output
97
- */
98
- type WorkerInferActivity<TActivity extends ActivityDefinition> = (args: WorkerInferInput<TActivity>) => Promise<WorkerInferOutput<TActivity>>;
99
- /**
100
- * Infer signal handler signature from worker perspective
101
- * Worker receives z.input
102
- */
103
- type WorkerInferSignal<TSignal extends SignalDefinition> = (args: WorkerInferInput<TSignal>) => Promise<void>;
104
- /**
105
- * Infer query handler signature from worker perspective
106
- * Worker receives z.input and returns z.output
107
- */
108
- type WorkerInferQuery<TQuery extends QueryDefinition> = (args: WorkerInferInput<TQuery>) => Promise<WorkerInferOutput<TQuery>>;
109
- /**
110
- * Infer update handler signature from worker perspective
111
- * Worker receives z.input and returns z.output
112
- */
113
- type WorkerInferUpdate<TUpdate extends UpdateDefinition> = (args: WorkerInferInput<TUpdate>) => Promise<WorkerInferOutput<TUpdate>>;
114
- /**
115
- * CLIENT PERSPECTIVE
116
- * Client sends z.output and receives z.input
117
- */
118
- /**
119
- * Infer workflow function signature from client perspective
120
- * Client sends z.output and receives z.input
121
- */
122
- type ClientInferWorkflow<TWorkflow extends WorkflowDefinition> = (args: ClientInferInput<TWorkflow>) => Promise<ClientInferOutput<TWorkflow>>;
123
- /**
124
- * Infer activity function signature from client perspective
125
- * Client sends z.output and receives z.input
126
- */
127
- type ClientInferActivity<TActivity extends ActivityDefinition> = (args: ClientInferInput<TActivity>) => Promise<ClientInferOutput<TActivity>>;
128
- /**
129
- * Infer signal handler signature from client perspective
130
- * Client sends z.output
131
- */
132
- type ClientInferSignal<TSignal extends SignalDefinition> = (args: ClientInferInput<TSignal>) => Promise<void>;
133
- /**
134
- * Infer query handler signature from client perspective
135
- * Client sends z.output and receives z.input
136
- */
137
- type ClientInferQuery<TQuery extends QueryDefinition> = (args: ClientInferInput<TQuery>) => Promise<ClientInferOutput<TQuery>>;
138
- /**
139
- * Infer update handler signature from client perspective
140
- * Client sends z.output and receives z.input
141
- */
142
- type ClientInferUpdate<TUpdate extends UpdateDefinition> = (args: ClientInferInput<TUpdate>) => Promise<ClientInferOutput<TUpdate>>;
143
- /**
144
- * WORKER PERSPECTIVE - Contract-level types
145
- */
146
- /**
147
- * Infer all workflows from a contract (worker perspective)
148
- */
149
- type WorkerInferWorkflows<TContract extends ContractDefinition> = { [K in keyof TContract["workflows"]]: WorkerInferWorkflow<TContract["workflows"][K]> };
150
- /**
151
- * Infer all activities from a contract (worker perspective)
152
- */
153
- type WorkerInferActivities<TContract extends ContractDefinition> = TContract["activities"] extends Record<string, ActivityDefinition> ? { [K in keyof TContract["activities"]]: WorkerInferActivity<TContract["activities"][K]> } : {};
154
- /**
155
- * Infer activities from a workflow definition (worker perspective)
156
- */
157
- type WorkerInferWorkflowActivities<T extends WorkflowDefinition> = T["activities"] extends Record<string, ActivityDefinition> ? { [K in keyof T["activities"]]: WorkerInferActivity<T["activities"][K]> } : {};
158
- /**
159
- * Infer signals from a workflow definition (worker perspective)
160
- */
161
- type WorkerInferWorkflowSignals<T extends WorkflowDefinition> = T["signals"] extends Record<string, SignalDefinition> ? { [K in keyof T["signals"]]: WorkerInferSignal<T["signals"][K]> } : {};
162
- /**
163
- * Infer queries from a workflow definition (worker perspective)
164
- */
165
- type WorkerInferWorkflowQueries<T extends WorkflowDefinition> = T["queries"] extends Record<string, QueryDefinition> ? { [K in keyof T["queries"]]: WorkerInferQuery<T["queries"][K]> } : {};
166
- /**
167
- * Infer updates from a workflow definition (worker perspective)
168
- */
169
- type WorkerInferWorkflowUpdates<T extends WorkflowDefinition> = T["updates"] extends Record<string, UpdateDefinition> ? { [K in keyof T["updates"]]: WorkerInferUpdate<T["updates"][K]> } : {};
170
- /**
171
- * Infer all activities available in a workflow context (worker perspective)
172
- * Combines workflow-specific activities with global activities
173
- */
174
- type WorkerInferWorkflowContextActivities<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"]> = WorkerInferWorkflowActivities<TContract["workflows"][TWorkflowName]> & WorkerInferActivities<TContract>;
175
- /**
176
- * CLIENT PERSPECTIVE - Contract-level types
177
- */
178
- /**
179
- * Infer all workflows from a contract (client perspective)
180
- */
181
- type ClientInferWorkflows<TContract extends ContractDefinition> = { [K in keyof TContract["workflows"]]: ClientInferWorkflow<TContract["workflows"][K]> };
182
- /**
183
- * Infer all activities from a contract (client perspective)
184
- */
185
- type ClientInferActivities<TContract extends ContractDefinition> = TContract["activities"] extends Record<string, ActivityDefinition> ? { [K in keyof TContract["activities"]]: ClientInferActivity<TContract["activities"][K]> } : {};
186
- /**
187
- * Infer activities from a workflow definition (client perspective)
188
- */
189
- type ClientInferWorkflowActivities<T extends WorkflowDefinition> = T["activities"] extends Record<string, ActivityDefinition> ? { [K in keyof T["activities"]]: ClientInferActivity<T["activities"][K]> } : {};
190
- /**
191
- * Infer signals from a workflow definition (client perspective)
192
- */
193
- type ClientInferWorkflowSignals<T extends WorkflowDefinition> = T["signals"] extends Record<string, SignalDefinition> ? { [K in keyof T["signals"]]: ClientInferSignal<T["signals"][K]> } : {};
194
- /**
195
- * Infer queries from a workflow definition (client perspective)
196
- */
197
- type ClientInferWorkflowQueries<T extends WorkflowDefinition> = T["queries"] extends Record<string, QueryDefinition> ? { [K in keyof T["queries"]]: ClientInferQuery<T["queries"][K]> } : {};
198
- /**
199
- * Infer updates from a workflow definition (client perspective)
200
- */
201
- type ClientInferWorkflowUpdates<T extends WorkflowDefinition> = T["updates"] extends Record<string, UpdateDefinition> ? { [K in keyof T["updates"]]: ClientInferUpdate<T["updates"][K]> } : {};
202
- /**
203
- * Infer all activities available in a workflow context (client perspective)
204
- * Combines workflow-specific activities with global activities
205
- */
206
- type ClientInferWorkflowContextActivities<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"]> = ClientInferWorkflowActivities<TContract["workflows"][TWorkflowName]> & ClientInferActivities<TContract>;
207
- /**
208
- * UTILITY TYPES FOR ACTIVITY HANDLERS
58
+ * UTILITY TYPES
209
59
  */
210
60
  /**
211
61
  * Extract workflow names from a contract as a union type
@@ -236,118 +86,14 @@ type InferActivityNames<TContract extends ContractDefinition> = TContract["activ
236
86
  * ```
237
87
  */
238
88
  type InferContractWorkflows<TContract extends ContractDefinition> = TContract["workflows"];
239
- /**
240
- * Infer the handler type for a global activity from a contract
241
- *
242
- * @example
243
- * ```typescript
244
- * const log: ActivityHandler<typeof myContract, "log"> = async ({ level, message }) => {
245
- * logger[level](message);
246
- * };
247
- * ```
248
- */
249
- type ActivityHandler<TContract extends ContractDefinition, TActivityName extends keyof TContract["activities"]> = TContract["activities"] extends Record<string, ActivityDefinition> ? (args: WorkerInferInput<TContract["activities"][TActivityName]>) => Promise<WorkerInferOutput<TContract["activities"][TActivityName]>> : never;
250
- /**
251
- * Infer the handler type for a workflow-specific activity from a contract
252
- *
253
- * @example
254
- * ```typescript
255
- * const processPayment: WorkflowActivityHandler<
256
- * typeof myContract,
257
- * "processOrder",
258
- * "processPayment"
259
- * > = async ({ customerId, amount }) => {
260
- * // Implementation
261
- * return { transactionId, status: "success" as const, paidAmount: amount };
262
- * };
263
- * ```
264
- */
265
- type WorkflowActivityHandler<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"], TActivityName extends keyof TContract["workflows"][TWorkflowName]["activities"]> = TContract["workflows"][TWorkflowName]["activities"] extends Record<string, ActivityDefinition> ? (args: WorkerInferInput<TContract["workflows"][TWorkflowName]["activities"][TActivityName]>) => Promise<WorkerInferOutput<TContract["workflows"][TWorkflowName]["activities"][TActivityName]>> : never;
266
89
  //#endregion
267
90
  //#region src/builder.d.ts
268
- /**
269
- * Builder for creating activity definitions
270
- *
271
- * @example
272
- * ```ts
273
- * const myActivity = defineActivity({
274
- * input: z.tuple([z.object({ name: z.string() })]),
275
- * output: z.object({ greeting: z.string() }),
276
- * });
277
- * ```
278
- */
279
- declare const defineActivity: <TActivity extends ActivityDefinition>(definition: TActivity) => TActivity;
280
- /**
281
- * Builder for creating signal definitions
282
- *
283
- * @example
284
- * ```ts
285
- * const mySignal = defineSignal({
286
- * input: z.object({ message: z.string() }),
287
- * });
288
- * ```
289
- */
290
- declare const defineSignal: <TSignal extends SignalDefinition>(definition: TSignal) => TSignal;
291
- /**
292
- * Builder for creating query definitions
293
- *
294
- * @example
295
- * ```ts
296
- * const myQuery = defineQuery({
297
- * input: z.object({ id: z.string() }),
298
- * output: z.object({ status: z.string() }),
299
- * });
300
- * ```
301
- */
302
- declare const defineQuery: <TQuery extends QueryDefinition>(definition: TQuery) => TQuery;
303
- /**
304
- * Builder for creating update definitions
305
- *
306
- * @example
307
- * ```ts
308
- * const myUpdate = defineUpdate({
309
- * input: z.object({ value: z.number() }),
310
- * output: z.object({ newValue: z.number() }),
311
- * });
312
- * ```
313
- */
314
- declare const defineUpdate: <TUpdate extends UpdateDefinition>(definition: TUpdate) => TUpdate;
315
- /**
316
- * Builder for creating workflow definitions
317
- *
318
- * @example
319
- * ```ts
320
- * const myWorkflow = defineWorkflow({
321
- * input: z.tuple([z.object({ orderId: z.string() })]),
322
- * output: z.object({ status: z.string() }),
323
- * activities: {
324
- * processPayment: defineActivity({
325
- * input: z.tuple([z.object({ amount: z.number() })]),
326
- * output: z.object({ success: z.boolean() }),
327
- * }),
328
- * },
329
- * });
330
- * ```
331
- */
332
- declare const defineWorkflow: <TWorkflow extends WorkflowDefinition>(definition: TWorkflow) => TWorkflow;
333
- /**
334
- * Builder for creating a complete contract
335
- *
336
- * @example
337
- * ```ts
338
- * const myContract = defineContract({
339
- * taskQueue: 'my-service',
340
- * workflows: {
341
- * processOrder: defineWorkflow({ ... }),
342
- * sendNotification: defineWorkflow({ ... }),
343
- * },
344
- * activities: {
345
- * sendEmail: defineActivity({ ... }),
346
- * },
347
- * });
348
- * ```
349
- */
350
- declare const defineContract: <TContract extends ContractDefinition>(definition: TContract) => TContract;
91
+ declare function defineActivity<TActivity extends ActivityDefinition>(definition: TActivity): TActivity;
92
+ declare function defineSignal<TSignal extends SignalDefinition>(definition: TSignal): TSignal;
93
+ declare function defineQuery<TQuery extends QueryDefinition>(definition: TQuery): TQuery;
94
+ declare function defineUpdate<TUpdate extends UpdateDefinition>(definition: TUpdate): TUpdate;
95
+ declare function defineWorkflow<TWorkflow extends WorkflowDefinition>(definition: TWorkflow): TWorkflow;
96
+ declare function defineContract<TContract extends ContractDefinition>(definition: TContract): TContract;
351
97
  //#endregion
352
98
  //#region src/nexus-types.d.ts
353
99
  /**
@@ -573,4 +319,4 @@ declare function defineNexusOperation<TOperation extends NexusOperationDefinitio
573
319
  */
574
320
  declare function defineNexusService<TService extends NexusServiceDefinition>(definition: TService): TService;
575
321
  //#endregion
576
- export { type ActivityDefinition, type ActivityHandler, type AnySchema, type ClientInferActivities, type ClientInferActivity, type ClientInferInput, type ClientInferNexusOperationInput, type ClientInferNexusOperationInvoker, type ClientInferNexusOperationOutput, type ClientInferNexusServiceOperations, type ClientInferNexusServices, type ClientInferOutput, type ClientInferQuery, type ClientInferSignal, type ClientInferUpdate, type ClientInferWorkflow, type ClientInferWorkflowActivities, type ClientInferWorkflowContextActivities, type ClientInferWorkflowQueries, type ClientInferWorkflowSignals, type ClientInferWorkflowUpdates, type ClientInferWorkflows, type ContractDefinition, type ContractDefinitionWithNexus, type InferActivityNames, type InferContractWorkflows, type InferNexusOperationNames, type InferNexusServiceNames, type InferWorkflowNames, type NexusOperationDefinition, type NexusOperationHandler, type NexusServiceDefinition, type QueryDefinition, type SignalDefinition, type UpdateDefinition, type WorkerInferActivities, type WorkerInferActivity, type WorkerInferInput, type WorkerInferNexusOperationHandler, type WorkerInferNexusOperationInput, type WorkerInferNexusOperationOutput, type WorkerInferNexusServiceHandlers, type WorkerInferNexusServices, type WorkerInferOutput, type WorkerInferQuery, type WorkerInferSignal, type WorkerInferUpdate, type WorkerInferWorkflow, type WorkerInferWorkflowActivities, type WorkerInferWorkflowContextActivities, type WorkerInferWorkflowQueries, type WorkerInferWorkflowSignals, type WorkerInferWorkflowUpdates, type WorkerInferWorkflows, type WorkflowActivityHandler, type WorkflowDefinition, defineActivity, defineContract, defineNexusOperation, defineNexusService, defineQuery, defineSignal, defineUpdate, defineWorkflow };
322
+ export { type ActivityDefinition, type AnySchema, type ClientInferNexusOperationInput, type ClientInferNexusOperationInvoker, type ClientInferNexusOperationOutput, type ClientInferNexusServiceOperations, type ClientInferNexusServices, type ContractDefinition, type ContractDefinitionWithNexus, type InferActivityNames, type InferContractWorkflows, type InferNexusOperationNames, type InferNexusServiceNames, type InferWorkflowNames, type NexusOperationDefinition, type NexusOperationHandler, type NexusServiceDefinition, type QueryDefinition, type SignalDefinition, type UpdateDefinition, type WorkerInferNexusOperationHandler, type WorkerInferNexusOperationInput, type WorkerInferNexusOperationOutput, type WorkerInferNexusServiceHandlers, type WorkerInferNexusServices, type WorkflowDefinition, defineActivity, defineContract, defineNexusOperation, defineNexusService, defineQuery, defineSignal, defineUpdate, defineWorkflow };
package/dist/index.d.mts CHANGED
@@ -55,157 +55,7 @@ interface ContractDefinition<TWorkflows extends Record<string, WorkflowDefinitio
55
55
  readonly activities?: TActivities;
56
56
  }
57
57
  /**
58
- * Infer input type from a definition (worker perspective)
59
- * Worker receives the output type (after input schema parsing/transformation)
60
- */
61
- type WorkerInferInput<T extends {
62
- input: AnySchema;
63
- }> = StandardSchemaV1.InferOutput<T["input"]>;
64
- /**
65
- * Infer output type from a definition (worker perspective)
66
- * Worker returns the input type (before output schema parsing/transformation)
67
- */
68
- type WorkerInferOutput<T extends {
69
- output: AnySchema;
70
- }> = StandardSchemaV1.InferInput<T["output"]>;
71
- /**
72
- * Infer input type from a definition (client perspective)
73
- * Client sends the input type (before input schema parsing/transformation)
74
- */
75
- type ClientInferInput<T extends {
76
- input: AnySchema;
77
- }> = StandardSchemaV1.InferInput<T["input"]>;
78
- /**
79
- * Infer output type from a definition (client perspective)
80
- * Client receives the output type (after output schema parsing/transformation)
81
- */
82
- type ClientInferOutput<T extends {
83
- output: AnySchema;
84
- }> = StandardSchemaV1.InferOutput<T["output"]>;
85
- /**
86
- * WORKER PERSPECTIVE
87
- * Worker receives z.output of input (parsed data) and returns z.input of output (raw data)
88
- */
89
- /**
90
- * Infer workflow function signature from worker perspective
91
- * Worker receives z.input and returns z.output
92
- */
93
- type WorkerInferWorkflow<TWorkflow extends WorkflowDefinition> = (args: WorkerInferInput<TWorkflow>) => Promise<WorkerInferOutput<TWorkflow>>;
94
- /**
95
- * Infer activity function signature from worker perspective
96
- * Worker receives z.input and returns z.output
97
- */
98
- type WorkerInferActivity<TActivity extends ActivityDefinition> = (args: WorkerInferInput<TActivity>) => Promise<WorkerInferOutput<TActivity>>;
99
- /**
100
- * Infer signal handler signature from worker perspective
101
- * Worker receives z.input
102
- */
103
- type WorkerInferSignal<TSignal extends SignalDefinition> = (args: WorkerInferInput<TSignal>) => Promise<void>;
104
- /**
105
- * Infer query handler signature from worker perspective
106
- * Worker receives z.input and returns z.output
107
- */
108
- type WorkerInferQuery<TQuery extends QueryDefinition> = (args: WorkerInferInput<TQuery>) => Promise<WorkerInferOutput<TQuery>>;
109
- /**
110
- * Infer update handler signature from worker perspective
111
- * Worker receives z.input and returns z.output
112
- */
113
- type WorkerInferUpdate<TUpdate extends UpdateDefinition> = (args: WorkerInferInput<TUpdate>) => Promise<WorkerInferOutput<TUpdate>>;
114
- /**
115
- * CLIENT PERSPECTIVE
116
- * Client sends z.output and receives z.input
117
- */
118
- /**
119
- * Infer workflow function signature from client perspective
120
- * Client sends z.output and receives z.input
121
- */
122
- type ClientInferWorkflow<TWorkflow extends WorkflowDefinition> = (args: ClientInferInput<TWorkflow>) => Promise<ClientInferOutput<TWorkflow>>;
123
- /**
124
- * Infer activity function signature from client perspective
125
- * Client sends z.output and receives z.input
126
- */
127
- type ClientInferActivity<TActivity extends ActivityDefinition> = (args: ClientInferInput<TActivity>) => Promise<ClientInferOutput<TActivity>>;
128
- /**
129
- * Infer signal handler signature from client perspective
130
- * Client sends z.output
131
- */
132
- type ClientInferSignal<TSignal extends SignalDefinition> = (args: ClientInferInput<TSignal>) => Promise<void>;
133
- /**
134
- * Infer query handler signature from client perspective
135
- * Client sends z.output and receives z.input
136
- */
137
- type ClientInferQuery<TQuery extends QueryDefinition> = (args: ClientInferInput<TQuery>) => Promise<ClientInferOutput<TQuery>>;
138
- /**
139
- * Infer update handler signature from client perspective
140
- * Client sends z.output and receives z.input
141
- */
142
- type ClientInferUpdate<TUpdate extends UpdateDefinition> = (args: ClientInferInput<TUpdate>) => Promise<ClientInferOutput<TUpdate>>;
143
- /**
144
- * WORKER PERSPECTIVE - Contract-level types
145
- */
146
- /**
147
- * Infer all workflows from a contract (worker perspective)
148
- */
149
- type WorkerInferWorkflows<TContract extends ContractDefinition> = { [K in keyof TContract["workflows"]]: WorkerInferWorkflow<TContract["workflows"][K]> };
150
- /**
151
- * Infer all activities from a contract (worker perspective)
152
- */
153
- type WorkerInferActivities<TContract extends ContractDefinition> = TContract["activities"] extends Record<string, ActivityDefinition> ? { [K in keyof TContract["activities"]]: WorkerInferActivity<TContract["activities"][K]> } : {};
154
- /**
155
- * Infer activities from a workflow definition (worker perspective)
156
- */
157
- type WorkerInferWorkflowActivities<T extends WorkflowDefinition> = T["activities"] extends Record<string, ActivityDefinition> ? { [K in keyof T["activities"]]: WorkerInferActivity<T["activities"][K]> } : {};
158
- /**
159
- * Infer signals from a workflow definition (worker perspective)
160
- */
161
- type WorkerInferWorkflowSignals<T extends WorkflowDefinition> = T["signals"] extends Record<string, SignalDefinition> ? { [K in keyof T["signals"]]: WorkerInferSignal<T["signals"][K]> } : {};
162
- /**
163
- * Infer queries from a workflow definition (worker perspective)
164
- */
165
- type WorkerInferWorkflowQueries<T extends WorkflowDefinition> = T["queries"] extends Record<string, QueryDefinition> ? { [K in keyof T["queries"]]: WorkerInferQuery<T["queries"][K]> } : {};
166
- /**
167
- * Infer updates from a workflow definition (worker perspective)
168
- */
169
- type WorkerInferWorkflowUpdates<T extends WorkflowDefinition> = T["updates"] extends Record<string, UpdateDefinition> ? { [K in keyof T["updates"]]: WorkerInferUpdate<T["updates"][K]> } : {};
170
- /**
171
- * Infer all activities available in a workflow context (worker perspective)
172
- * Combines workflow-specific activities with global activities
173
- */
174
- type WorkerInferWorkflowContextActivities<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"]> = WorkerInferWorkflowActivities<TContract["workflows"][TWorkflowName]> & WorkerInferActivities<TContract>;
175
- /**
176
- * CLIENT PERSPECTIVE - Contract-level types
177
- */
178
- /**
179
- * Infer all workflows from a contract (client perspective)
180
- */
181
- type ClientInferWorkflows<TContract extends ContractDefinition> = { [K in keyof TContract["workflows"]]: ClientInferWorkflow<TContract["workflows"][K]> };
182
- /**
183
- * Infer all activities from a contract (client perspective)
184
- */
185
- type ClientInferActivities<TContract extends ContractDefinition> = TContract["activities"] extends Record<string, ActivityDefinition> ? { [K in keyof TContract["activities"]]: ClientInferActivity<TContract["activities"][K]> } : {};
186
- /**
187
- * Infer activities from a workflow definition (client perspective)
188
- */
189
- type ClientInferWorkflowActivities<T extends WorkflowDefinition> = T["activities"] extends Record<string, ActivityDefinition> ? { [K in keyof T["activities"]]: ClientInferActivity<T["activities"][K]> } : {};
190
- /**
191
- * Infer signals from a workflow definition (client perspective)
192
- */
193
- type ClientInferWorkflowSignals<T extends WorkflowDefinition> = T["signals"] extends Record<string, SignalDefinition> ? { [K in keyof T["signals"]]: ClientInferSignal<T["signals"][K]> } : {};
194
- /**
195
- * Infer queries from a workflow definition (client perspective)
196
- */
197
- type ClientInferWorkflowQueries<T extends WorkflowDefinition> = T["queries"] extends Record<string, QueryDefinition> ? { [K in keyof T["queries"]]: ClientInferQuery<T["queries"][K]> } : {};
198
- /**
199
- * Infer updates from a workflow definition (client perspective)
200
- */
201
- type ClientInferWorkflowUpdates<T extends WorkflowDefinition> = T["updates"] extends Record<string, UpdateDefinition> ? { [K in keyof T["updates"]]: ClientInferUpdate<T["updates"][K]> } : {};
202
- /**
203
- * Infer all activities available in a workflow context (client perspective)
204
- * Combines workflow-specific activities with global activities
205
- */
206
- type ClientInferWorkflowContextActivities<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"]> = ClientInferWorkflowActivities<TContract["workflows"][TWorkflowName]> & ClientInferActivities<TContract>;
207
- /**
208
- * UTILITY TYPES FOR ACTIVITY HANDLERS
58
+ * UTILITY TYPES
209
59
  */
210
60
  /**
211
61
  * Extract workflow names from a contract as a union type
@@ -236,118 +86,14 @@ type InferActivityNames<TContract extends ContractDefinition> = TContract["activ
236
86
  * ```
237
87
  */
238
88
  type InferContractWorkflows<TContract extends ContractDefinition> = TContract["workflows"];
239
- /**
240
- * Infer the handler type for a global activity from a contract
241
- *
242
- * @example
243
- * ```typescript
244
- * const log: ActivityHandler<typeof myContract, "log"> = async ({ level, message }) => {
245
- * logger[level](message);
246
- * };
247
- * ```
248
- */
249
- type ActivityHandler<TContract extends ContractDefinition, TActivityName extends keyof TContract["activities"]> = TContract["activities"] extends Record<string, ActivityDefinition> ? (args: WorkerInferInput<TContract["activities"][TActivityName]>) => Promise<WorkerInferOutput<TContract["activities"][TActivityName]>> : never;
250
- /**
251
- * Infer the handler type for a workflow-specific activity from a contract
252
- *
253
- * @example
254
- * ```typescript
255
- * const processPayment: WorkflowActivityHandler<
256
- * typeof myContract,
257
- * "processOrder",
258
- * "processPayment"
259
- * > = async ({ customerId, amount }) => {
260
- * // Implementation
261
- * return { transactionId, status: "success" as const, paidAmount: amount };
262
- * };
263
- * ```
264
- */
265
- type WorkflowActivityHandler<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"], TActivityName extends keyof TContract["workflows"][TWorkflowName]["activities"]> = TContract["workflows"][TWorkflowName]["activities"] extends Record<string, ActivityDefinition> ? (args: WorkerInferInput<TContract["workflows"][TWorkflowName]["activities"][TActivityName]>) => Promise<WorkerInferOutput<TContract["workflows"][TWorkflowName]["activities"][TActivityName]>> : never;
266
89
  //#endregion
267
90
  //#region src/builder.d.ts
268
- /**
269
- * Builder for creating activity definitions
270
- *
271
- * @example
272
- * ```ts
273
- * const myActivity = defineActivity({
274
- * input: z.tuple([z.object({ name: z.string() })]),
275
- * output: z.object({ greeting: z.string() }),
276
- * });
277
- * ```
278
- */
279
- declare const defineActivity: <TActivity extends ActivityDefinition>(definition: TActivity) => TActivity;
280
- /**
281
- * Builder for creating signal definitions
282
- *
283
- * @example
284
- * ```ts
285
- * const mySignal = defineSignal({
286
- * input: z.object({ message: z.string() }),
287
- * });
288
- * ```
289
- */
290
- declare const defineSignal: <TSignal extends SignalDefinition>(definition: TSignal) => TSignal;
291
- /**
292
- * Builder for creating query definitions
293
- *
294
- * @example
295
- * ```ts
296
- * const myQuery = defineQuery({
297
- * input: z.object({ id: z.string() }),
298
- * output: z.object({ status: z.string() }),
299
- * });
300
- * ```
301
- */
302
- declare const defineQuery: <TQuery extends QueryDefinition>(definition: TQuery) => TQuery;
303
- /**
304
- * Builder for creating update definitions
305
- *
306
- * @example
307
- * ```ts
308
- * const myUpdate = defineUpdate({
309
- * input: z.object({ value: z.number() }),
310
- * output: z.object({ newValue: z.number() }),
311
- * });
312
- * ```
313
- */
314
- declare const defineUpdate: <TUpdate extends UpdateDefinition>(definition: TUpdate) => TUpdate;
315
- /**
316
- * Builder for creating workflow definitions
317
- *
318
- * @example
319
- * ```ts
320
- * const myWorkflow = defineWorkflow({
321
- * input: z.tuple([z.object({ orderId: z.string() })]),
322
- * output: z.object({ status: z.string() }),
323
- * activities: {
324
- * processPayment: defineActivity({
325
- * input: z.tuple([z.object({ amount: z.number() })]),
326
- * output: z.object({ success: z.boolean() }),
327
- * }),
328
- * },
329
- * });
330
- * ```
331
- */
332
- declare const defineWorkflow: <TWorkflow extends WorkflowDefinition>(definition: TWorkflow) => TWorkflow;
333
- /**
334
- * Builder for creating a complete contract
335
- *
336
- * @example
337
- * ```ts
338
- * const myContract = defineContract({
339
- * taskQueue: 'my-service',
340
- * workflows: {
341
- * processOrder: defineWorkflow({ ... }),
342
- * sendNotification: defineWorkflow({ ... }),
343
- * },
344
- * activities: {
345
- * sendEmail: defineActivity({ ... }),
346
- * },
347
- * });
348
- * ```
349
- */
350
- declare const defineContract: <TContract extends ContractDefinition>(definition: TContract) => TContract;
91
+ declare function defineActivity<TActivity extends ActivityDefinition>(definition: TActivity): TActivity;
92
+ declare function defineSignal<TSignal extends SignalDefinition>(definition: TSignal): TSignal;
93
+ declare function defineQuery<TQuery extends QueryDefinition>(definition: TQuery): TQuery;
94
+ declare function defineUpdate<TUpdate extends UpdateDefinition>(definition: TUpdate): TUpdate;
95
+ declare function defineWorkflow<TWorkflow extends WorkflowDefinition>(definition: TWorkflow): TWorkflow;
96
+ declare function defineContract<TContract extends ContractDefinition>(definition: TContract): TContract;
351
97
  //#endregion
352
98
  //#region src/nexus-types.d.ts
353
99
  /**
@@ -573,4 +319,4 @@ declare function defineNexusOperation<TOperation extends NexusOperationDefinitio
573
319
  */
574
320
  declare function defineNexusService<TService extends NexusServiceDefinition>(definition: TService): TService;
575
321
  //#endregion
576
- export { type ActivityDefinition, type ActivityHandler, type AnySchema, type ClientInferActivities, type ClientInferActivity, type ClientInferInput, type ClientInferNexusOperationInput, type ClientInferNexusOperationInvoker, type ClientInferNexusOperationOutput, type ClientInferNexusServiceOperations, type ClientInferNexusServices, type ClientInferOutput, type ClientInferQuery, type ClientInferSignal, type ClientInferUpdate, type ClientInferWorkflow, type ClientInferWorkflowActivities, type ClientInferWorkflowContextActivities, type ClientInferWorkflowQueries, type ClientInferWorkflowSignals, type ClientInferWorkflowUpdates, type ClientInferWorkflows, type ContractDefinition, type ContractDefinitionWithNexus, type InferActivityNames, type InferContractWorkflows, type InferNexusOperationNames, type InferNexusServiceNames, type InferWorkflowNames, type NexusOperationDefinition, type NexusOperationHandler, type NexusServiceDefinition, type QueryDefinition, type SignalDefinition, type UpdateDefinition, type WorkerInferActivities, type WorkerInferActivity, type WorkerInferInput, type WorkerInferNexusOperationHandler, type WorkerInferNexusOperationInput, type WorkerInferNexusOperationOutput, type WorkerInferNexusServiceHandlers, type WorkerInferNexusServices, type WorkerInferOutput, type WorkerInferQuery, type WorkerInferSignal, type WorkerInferUpdate, type WorkerInferWorkflow, type WorkerInferWorkflowActivities, type WorkerInferWorkflowContextActivities, type WorkerInferWorkflowQueries, type WorkerInferWorkflowSignals, type WorkerInferWorkflowUpdates, type WorkerInferWorkflows, type WorkflowActivityHandler, type WorkflowDefinition, defineActivity, defineContract, defineNexusOperation, defineNexusService, defineQuery, defineSignal, defineUpdate, defineWorkflow };
322
+ export { type ActivityDefinition, type AnySchema, type ClientInferNexusOperationInput, type ClientInferNexusOperationInvoker, type ClientInferNexusOperationOutput, type ClientInferNexusServiceOperations, type ClientInferNexusServices, type ContractDefinition, type ContractDefinitionWithNexus, type InferActivityNames, type InferContractWorkflows, type InferNexusOperationNames, type InferNexusServiceNames, type InferWorkflowNames, type NexusOperationDefinition, type NexusOperationHandler, type NexusServiceDefinition, type QueryDefinition, type SignalDefinition, type UpdateDefinition, type WorkerInferNexusOperationHandler, type WorkerInferNexusOperationInput, type WorkerInferNexusOperationOutput, type WorkerInferNexusServiceHandlers, type WorkerInferNexusServices, type WorkflowDefinition, defineActivity, defineContract, defineNexusOperation, defineNexusService, defineQuery, defineSignal, defineUpdate, defineWorkflow };
package/dist/index.mjs CHANGED
@@ -1,6 +1,29 @@
1
1
  import { z } from "zod";
2
2
 
3
3
  //#region src/builder.ts
4
+ function defineActivity(definition) {
5
+ return definition;
6
+ }
7
+ function defineSignal(definition) {
8
+ return definition;
9
+ }
10
+ function defineQuery(definition) {
11
+ return definition;
12
+ }
13
+ function defineUpdate(definition) {
14
+ return definition;
15
+ }
16
+ function defineWorkflow(definition) {
17
+ return definition;
18
+ }
19
+ function defineContract(definition) {
20
+ const validationResult = contractValidationSchema.safeParse(definition);
21
+ if (!validationResult.success) {
22
+ const cleanMessage = getCleanErrorMessage(validationResult.error);
23
+ throw new Error(`Contract error: ${cleanMessage}`);
24
+ }
25
+ return definition;
26
+ }
4
27
  /**
5
28
  * Check if a value is a Standard Schema compatible schema
6
29
  */
@@ -18,7 +41,7 @@ const identifierSchema = z.string().min(1).regex(/^[a-zA-Z_$][a-zA-Z0-9_$]*$/, "
18
41
  /**
19
42
  * Extract clean error message from Zod validation error
20
43
  */
21
- const getCleanErrorMessage = (error) => {
44
+ function getCleanErrorMessage(error) {
22
45
  try {
23
46
  const parsed = JSON.parse(error.message);
24
47
  if (Array.isArray(parsed) && parsed.length > 0) {
@@ -31,7 +54,7 @@ const getCleanErrorMessage = (error) => {
31
54
  }
32
55
  } catch {}
33
56
  return error.message;
34
- };
57
+ }
35
58
  /**
36
59
  * Schema for validating activity definitions
37
60
  * Checks that input and output are Standard Schema compatible schemas
@@ -88,106 +111,6 @@ const contractValidationSchema = z.object({
88
111
  }
89
112
  }
90
113
  });
91
- /**
92
- * Builder for creating activity definitions
93
- *
94
- * @example
95
- * ```ts
96
- * const myActivity = defineActivity({
97
- * input: z.tuple([z.object({ name: z.string() })]),
98
- * output: z.object({ greeting: z.string() }),
99
- * });
100
- * ```
101
- */
102
- const defineActivity = (definition) => {
103
- return definition;
104
- };
105
- /**
106
- * Builder for creating signal definitions
107
- *
108
- * @example
109
- * ```ts
110
- * const mySignal = defineSignal({
111
- * input: z.object({ message: z.string() }),
112
- * });
113
- * ```
114
- */
115
- const defineSignal = (definition) => {
116
- return definition;
117
- };
118
- /**
119
- * Builder for creating query definitions
120
- *
121
- * @example
122
- * ```ts
123
- * const myQuery = defineQuery({
124
- * input: z.object({ id: z.string() }),
125
- * output: z.object({ status: z.string() }),
126
- * });
127
- * ```
128
- */
129
- const defineQuery = (definition) => {
130
- return definition;
131
- };
132
- /**
133
- * Builder for creating update definitions
134
- *
135
- * @example
136
- * ```ts
137
- * const myUpdate = defineUpdate({
138
- * input: z.object({ value: z.number() }),
139
- * output: z.object({ newValue: z.number() }),
140
- * });
141
- * ```
142
- */
143
- const defineUpdate = (definition) => {
144
- return definition;
145
- };
146
- /**
147
- * Builder for creating workflow definitions
148
- *
149
- * @example
150
- * ```ts
151
- * const myWorkflow = defineWorkflow({
152
- * input: z.tuple([z.object({ orderId: z.string() })]),
153
- * output: z.object({ status: z.string() }),
154
- * activities: {
155
- * processPayment: defineActivity({
156
- * input: z.tuple([z.object({ amount: z.number() })]),
157
- * output: z.object({ success: z.boolean() }),
158
- * }),
159
- * },
160
- * });
161
- * ```
162
- */
163
- const defineWorkflow = (definition) => {
164
- return definition;
165
- };
166
- /**
167
- * Builder for creating a complete contract
168
- *
169
- * @example
170
- * ```ts
171
- * const myContract = defineContract({
172
- * taskQueue: 'my-service',
173
- * workflows: {
174
- * processOrder: defineWorkflow({ ... }),
175
- * sendNotification: defineWorkflow({ ... }),
176
- * },
177
- * activities: {
178
- * sendEmail: defineActivity({ ... }),
179
- * },
180
- * });
181
- * ```
182
- */
183
- const defineContract = (definition) => {
184
- const validationResult = contractValidationSchema.safeParse(definition);
185
- if (!validationResult.success) {
186
- const cleanMessage = getCleanErrorMessage(validationResult.error);
187
- throw new Error(`Contract error: ${cleanMessage}`);
188
- }
189
- return definition;
190
- };
191
114
 
192
115
  //#endregion
193
116
  //#region src/nexus-types.ts
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@temporal-contract/contract",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "description": "Contract builder for temporal-contract",
5
5
  "keywords": [
6
+ "contract",
6
7
  "temporal",
7
- "typescript",
8
- "contract"
8
+ "typescript"
9
9
  ],
10
10
  "homepage": "https://github.com/btravers/temporal-contract#readme",
11
11
  "bugs": {
@@ -17,7 +17,7 @@
17
17
  "directory": "packages/contract"
18
18
  },
19
19
  "license": "MIT",
20
- "author": "Benoit TRAVERS <benoit.travers.frgmail.com>",
20
+ "author": "Benoit TRAVERS <benoit.travers.fr@gmail.com>",
21
21
  "type": "module",
22
22
  "exports": {
23
23
  ".": {
@@ -39,17 +39,17 @@
39
39
  "dist"
40
40
  ],
41
41
  "dependencies": {
42
- "@standard-schema/spec": "1.0.0",
43
- "zod": "4.1.13"
42
+ "@standard-schema/spec": "1.1.0",
43
+ "zod": "4.2.1"
44
44
  },
45
45
  "devDependencies": {
46
- "@vitest/coverage-v8": "4.0.15",
47
- "arktype": "2.1.28",
48
- "tsdown": "0.17.3",
46
+ "@vitest/coverage-v8": "4.0.16",
47
+ "arktype": "2.1.29",
48
+ "tsdown": "0.18.1",
49
49
  "typescript": "5.9.3",
50
50
  "valibot": "1.2.0",
51
- "vitest": "4.0.15",
52
- "@temporal-contract/tsconfig": "0.0.4"
51
+ "vitest": "4.0.16",
52
+ "@temporal-contract/tsconfig": "0.0.6"
53
53
  },
54
54
  "scripts": {
55
55
  "build": "tsdown src/index.ts --format cjs,esm --dts --clean",