@temporal-contract/contract 0.0.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.
package/src/types.ts ADDED
@@ -0,0 +1,420 @@
1
+ import type { z } from "zod";
2
+
3
+ /**
4
+ * Base types for validation schemas
5
+ * Constrained to avoid implicit any types
6
+ */
7
+ export type AnyZodSchema = z.ZodType<unknown, unknown>;
8
+
9
+ /**
10
+ * Definition of an activity
11
+ */
12
+ export interface ActivityDefinition<
13
+ TInput extends AnyZodSchema = AnyZodSchema,
14
+ TOutput extends AnyZodSchema = AnyZodSchema,
15
+ > {
16
+ readonly input: TInput;
17
+ readonly output: TOutput;
18
+ }
19
+
20
+ /**
21
+ * Definition of a signal
22
+ */
23
+ export interface SignalDefinition<TInput extends AnyZodSchema = AnyZodSchema> {
24
+ readonly input: TInput;
25
+ }
26
+
27
+ /**
28
+ * Definition of a query
29
+ */
30
+ export interface QueryDefinition<
31
+ TInput extends AnyZodSchema = AnyZodSchema,
32
+ TOutput extends AnyZodSchema = AnyZodSchema,
33
+ > {
34
+ readonly input: TInput;
35
+ readonly output: TOutput;
36
+ }
37
+
38
+ /**
39
+ * Definition of an update
40
+ */
41
+ export interface UpdateDefinition<
42
+ TInput extends AnyZodSchema = AnyZodSchema,
43
+ TOutput extends AnyZodSchema = AnyZodSchema,
44
+ > {
45
+ readonly input: TInput;
46
+ readonly output: TOutput;
47
+ }
48
+
49
+ /**
50
+ * Definition of a workflow
51
+ */
52
+ export interface WorkflowDefinition<
53
+ TActivities extends Record<string, ActivityDefinition> = Record<string, ActivityDefinition>,
54
+ TSignals extends Record<string, SignalDefinition> = Record<string, SignalDefinition>,
55
+ TQueries extends Record<string, QueryDefinition> = Record<string, QueryDefinition>,
56
+ TUpdates extends Record<string, UpdateDefinition> = Record<string, UpdateDefinition>,
57
+ > {
58
+ readonly input: AnyZodSchema;
59
+ readonly output: AnyZodSchema;
60
+ readonly activities?: TActivities;
61
+ readonly signals?: TSignals;
62
+ readonly queries?: TQueries;
63
+ readonly updates?: TUpdates;
64
+ }
65
+
66
+ /**
67
+ * Contract definition containing workflows and optional global activities
68
+ */
69
+ export interface ContractDefinition<
70
+ TWorkflows extends Record<string, WorkflowDefinition> = Record<string, WorkflowDefinition>,
71
+ TActivities extends Record<string, ActivityDefinition> = Record<string, ActivityDefinition>,
72
+ > {
73
+ readonly taskQueue: string;
74
+ readonly workflows: TWorkflows;
75
+ readonly activities?: TActivities;
76
+ }
77
+
78
+ /**
79
+ * Infer input type from a definition (worker perspective)
80
+ * Worker receives z.output (after input schema parsing/transformation)
81
+ */
82
+ export type WorkerInferInput<T extends { input: AnyZodSchema }> = z.output<T["input"]>;
83
+
84
+ /**
85
+ * Infer output type from a definition (worker perspective)
86
+ * Worker returns z.input (before output schema parsing/transformation)
87
+ */
88
+ export type WorkerInferOutput<T extends { output: AnyZodSchema }> = z.input<T["output"]>;
89
+
90
+ /**
91
+ * Infer input type from a definition (client perspective)
92
+ * Client sends z.input (before input schema parsing/transformation)
93
+ */
94
+ export type ClientInferInput<T extends { input: AnyZodSchema }> = z.input<T["input"]>;
95
+
96
+ /**
97
+ * Infer output type from a definition (client perspective)
98
+ * Client receives z.output (after output schema parsing/transformation)
99
+ */
100
+ export type ClientInferOutput<T extends { output: AnyZodSchema }> = z.output<T["output"]>;
101
+
102
+ /**
103
+ * WORKER PERSPECTIVE
104
+ * Worker receives z.output of input (parsed data) and returns z.input of output (raw data)
105
+ */
106
+
107
+ /**
108
+ * Infer workflow function signature from worker perspective
109
+ * Worker receives z.input and returns z.output
110
+ */
111
+ export type WorkerInferWorkflow<TWorkflow extends WorkflowDefinition> = (
112
+ args: WorkerInferInput<TWorkflow>,
113
+ ) => Promise<WorkerInferOutput<TWorkflow>>;
114
+
115
+ /**
116
+ * Infer activity function signature from worker perspective
117
+ * Worker receives z.input and returns z.output
118
+ */
119
+ export type WorkerInferActivity<TActivity extends ActivityDefinition> = (
120
+ args: WorkerInferInput<TActivity>,
121
+ ) => Promise<WorkerInferOutput<TActivity>>;
122
+
123
+ /**
124
+ * Infer signal handler signature from worker perspective
125
+ * Worker receives z.input
126
+ */
127
+ export type WorkerInferSignal<TSignal extends SignalDefinition> = (
128
+ args: WorkerInferInput<TSignal>,
129
+ ) => Promise<void>;
130
+
131
+ /**
132
+ * Infer query handler signature from worker perspective
133
+ * Worker receives z.input and returns z.output
134
+ */
135
+ export type WorkerInferQuery<TQuery extends QueryDefinition> = (
136
+ args: WorkerInferInput<TQuery>,
137
+ ) => Promise<WorkerInferOutput<TQuery>>;
138
+
139
+ /**
140
+ * Infer update handler signature from worker perspective
141
+ * Worker receives z.input and returns z.output
142
+ */
143
+ export type WorkerInferUpdate<TUpdate extends UpdateDefinition> = (
144
+ args: WorkerInferInput<TUpdate>,
145
+ ) => Promise<WorkerInferOutput<TUpdate>>;
146
+
147
+ /**
148
+ * CLIENT PERSPECTIVE
149
+ * Client sends z.output and receives z.input
150
+ */
151
+
152
+ /**
153
+ * Infer workflow function signature from client perspective
154
+ * Client sends z.output and receives z.input
155
+ */
156
+ export type ClientInferWorkflow<TWorkflow extends WorkflowDefinition> = (
157
+ args: ClientInferInput<TWorkflow>,
158
+ ) => Promise<ClientInferOutput<TWorkflow>>;
159
+
160
+ /**
161
+ * Infer activity function signature from client perspective
162
+ * Client sends z.output and receives z.input
163
+ */
164
+ export type ClientInferActivity<TActivity extends ActivityDefinition> = (
165
+ args: ClientInferInput<TActivity>,
166
+ ) => Promise<ClientInferOutput<TActivity>>;
167
+
168
+ /**
169
+ * Infer signal handler signature from client perspective
170
+ * Client sends z.output
171
+ */
172
+ export type ClientInferSignal<TSignal extends SignalDefinition> = (
173
+ args: ClientInferInput<TSignal>,
174
+ ) => Promise<void>;
175
+
176
+ /**
177
+ * Infer query handler signature from client perspective
178
+ * Client sends z.output and receives z.input
179
+ */
180
+ export type ClientInferQuery<TQuery extends QueryDefinition> = (
181
+ args: ClientInferInput<TQuery>,
182
+ ) => Promise<ClientInferOutput<TQuery>>;
183
+
184
+ /**
185
+ * Infer update handler signature from client perspective
186
+ * Client sends z.output and receives z.input
187
+ */
188
+ export type ClientInferUpdate<TUpdate extends UpdateDefinition> = (
189
+ args: ClientInferInput<TUpdate>,
190
+ ) => Promise<ClientInferOutput<TUpdate>>;
191
+
192
+ /**
193
+ * WORKER PERSPECTIVE - Contract-level types
194
+ */
195
+
196
+ /**
197
+ * Infer all workflows from a contract (worker perspective)
198
+ */
199
+ export type WorkerInferWorkflows<TContract extends ContractDefinition> = {
200
+ [K in keyof TContract["workflows"]]: WorkerInferWorkflow<TContract["workflows"][K]>;
201
+ };
202
+
203
+ /**
204
+ * Infer all activities from a contract (worker perspective)
205
+ */
206
+ export type WorkerInferActivities<TContract extends ContractDefinition> =
207
+ TContract["activities"] extends Record<string, ActivityDefinition>
208
+ ? {
209
+ [K in keyof TContract["activities"]]: WorkerInferActivity<TContract["activities"][K]>;
210
+ }
211
+ : {};
212
+
213
+ /**
214
+ * Infer activities from a workflow definition (worker perspective)
215
+ */
216
+ export type WorkerInferWorkflowActivities<T extends WorkflowDefinition> =
217
+ T["activities"] extends Record<string, ActivityDefinition>
218
+ ? {
219
+ [K in keyof T["activities"]]: WorkerInferActivity<T["activities"][K]>;
220
+ }
221
+ : {};
222
+
223
+ /**
224
+ * Infer signals from a workflow definition (worker perspective)
225
+ */
226
+ export type WorkerInferWorkflowSignals<T extends WorkflowDefinition> =
227
+ T["signals"] extends Record<string, SignalDefinition>
228
+ ? {
229
+ [K in keyof T["signals"]]: WorkerInferSignal<T["signals"][K]>;
230
+ }
231
+ : {};
232
+
233
+ /**
234
+ * Infer queries from a workflow definition (worker perspective)
235
+ */
236
+ export type WorkerInferWorkflowQueries<T extends WorkflowDefinition> =
237
+ T["queries"] extends Record<string, QueryDefinition>
238
+ ? {
239
+ [K in keyof T["queries"]]: WorkerInferQuery<T["queries"][K]>;
240
+ }
241
+ : {};
242
+
243
+ /**
244
+ * Infer updates from a workflow definition (worker perspective)
245
+ */
246
+ export type WorkerInferWorkflowUpdates<T extends WorkflowDefinition> =
247
+ T["updates"] extends Record<string, UpdateDefinition>
248
+ ? {
249
+ [K in keyof T["updates"]]: WorkerInferUpdate<T["updates"][K]>;
250
+ }
251
+ : {};
252
+
253
+ /**
254
+ * Infer all activities available in a workflow context (worker perspective)
255
+ * Combines workflow-specific activities with global activities
256
+ */
257
+ export type WorkerInferWorkflowContextActivities<
258
+ TContract extends ContractDefinition,
259
+ TWorkflowName extends keyof TContract["workflows"],
260
+ > = WorkerInferWorkflowActivities<TContract["workflows"][TWorkflowName]> &
261
+ WorkerInferActivities<TContract>;
262
+
263
+ /**
264
+ * CLIENT PERSPECTIVE - Contract-level types
265
+ */
266
+
267
+ /**
268
+ * Infer all workflows from a contract (client perspective)
269
+ */
270
+ export type ClientInferWorkflows<TContract extends ContractDefinition> = {
271
+ [K in keyof TContract["workflows"]]: ClientInferWorkflow<TContract["workflows"][K]>;
272
+ };
273
+
274
+ /**
275
+ * Infer all activities from a contract (client perspective)
276
+ */
277
+ export type ClientInferActivities<TContract extends ContractDefinition> =
278
+ TContract["activities"] extends Record<string, ActivityDefinition>
279
+ ? {
280
+ [K in keyof TContract["activities"]]: ClientInferActivity<TContract["activities"][K]>;
281
+ }
282
+ : {};
283
+
284
+ /**
285
+ * Infer activities from a workflow definition (client perspective)
286
+ */
287
+ export type ClientInferWorkflowActivities<T extends WorkflowDefinition> =
288
+ T["activities"] extends Record<string, ActivityDefinition>
289
+ ? {
290
+ [K in keyof T["activities"]]: ClientInferActivity<T["activities"][K]>;
291
+ }
292
+ : {};
293
+
294
+ /**
295
+ * Infer signals from a workflow definition (client perspective)
296
+ */
297
+ export type ClientInferWorkflowSignals<T extends WorkflowDefinition> =
298
+ T["signals"] extends Record<string, SignalDefinition>
299
+ ? {
300
+ [K in keyof T["signals"]]: ClientInferSignal<T["signals"][K]>;
301
+ }
302
+ : {};
303
+
304
+ /**
305
+ * Infer queries from a workflow definition (client perspective)
306
+ */
307
+ export type ClientInferWorkflowQueries<T extends WorkflowDefinition> =
308
+ T["queries"] extends Record<string, QueryDefinition>
309
+ ? {
310
+ [K in keyof T["queries"]]: ClientInferQuery<T["queries"][K]>;
311
+ }
312
+ : {};
313
+
314
+ /**
315
+ * Infer updates from a workflow definition (client perspective)
316
+ */
317
+ export type ClientInferWorkflowUpdates<T extends WorkflowDefinition> =
318
+ T["updates"] extends Record<string, UpdateDefinition>
319
+ ? {
320
+ [K in keyof T["updates"]]: ClientInferUpdate<T["updates"][K]>;
321
+ }
322
+ : {};
323
+
324
+ /**
325
+ * Infer all activities available in a workflow context (client perspective)
326
+ * Combines workflow-specific activities with global activities
327
+ */
328
+ export type ClientInferWorkflowContextActivities<
329
+ TContract extends ContractDefinition,
330
+ TWorkflowName extends keyof TContract["workflows"],
331
+ > = ClientInferWorkflowActivities<TContract["workflows"][TWorkflowName]> &
332
+ ClientInferActivities<TContract>;
333
+
334
+ /**
335
+ * UTILITY TYPES FOR ACTIVITY HANDLERS
336
+ */
337
+
338
+ /**
339
+ * Extract workflow names from a contract as a union type
340
+ *
341
+ * @example
342
+ * ```typescript
343
+ * type MyWorkflowNames = InferWorkflowNames<typeof myContract>;
344
+ * // "processOrder" | "sendNotification"
345
+ * ```
346
+ */
347
+ export type InferWorkflowNames<TContract extends ContractDefinition> =
348
+ keyof TContract["workflows"] & string;
349
+
350
+ /**
351
+ * Extract activity names from a contract (global activities) as a union type
352
+ *
353
+ * @example
354
+ * ```typescript
355
+ * type MyActivityNames = InferActivityNames<typeof myContract>;
356
+ * // "log" | "sendEmail"
357
+ * ```
358
+ */
359
+ export type InferActivityNames<TContract extends ContractDefinition> =
360
+ TContract["activities"] extends Record<string, ActivityDefinition>
361
+ ? keyof TContract["activities"] & string
362
+ : never;
363
+
364
+ /**
365
+ * Extract all workflows from a contract with their definitions
366
+ *
367
+ * @example
368
+ * ```typescript
369
+ * type MyWorkflows = InferContractWorkflows<typeof myContract>;
370
+ * ```
371
+ */
372
+ export type InferContractWorkflows<TContract extends ContractDefinition> = TContract["workflows"];
373
+
374
+ /**
375
+ * Infer the handler type for a global activity from a contract
376
+ *
377
+ * @example
378
+ * ```typescript
379
+ * const log: ActivityHandler<typeof myContract, "log"> = async ({ level, message }) => {
380
+ * logger[level](message);
381
+ * };
382
+ * ```
383
+ */
384
+ export type ActivityHandler<
385
+ TContract extends ContractDefinition,
386
+ TActivityName extends keyof TContract["activities"],
387
+ > =
388
+ TContract["activities"] extends Record<string, ActivityDefinition>
389
+ ? (
390
+ args: WorkerInferInput<TContract["activities"][TActivityName]>,
391
+ ) => Promise<WorkerInferOutput<TContract["activities"][TActivityName]>>
392
+ : never;
393
+
394
+ /**
395
+ * Infer the handler type for a workflow-specific activity from a contract
396
+ *
397
+ * @example
398
+ * ```typescript
399
+ * const processPayment: WorkflowActivityHandler<
400
+ * typeof myContract,
401
+ * "processOrder",
402
+ * "processPayment"
403
+ * > = async ({ customerId, amount }) => {
404
+ * // Implementation
405
+ * return { transactionId, status: "success" as const, paidAmount: amount };
406
+ * };
407
+ * ```
408
+ */
409
+ export type WorkflowActivityHandler<
410
+ TContract extends ContractDefinition,
411
+ TWorkflowName extends keyof TContract["workflows"],
412
+ TActivityName extends keyof TContract["workflows"][TWorkflowName]["activities"],
413
+ > =
414
+ TContract["workflows"][TWorkflowName]["activities"] extends Record<string, ActivityDefinition>
415
+ ? (
416
+ args: WorkerInferInput<TContract["workflows"][TWorkflowName]["activities"][TActivityName]>,
417
+ ) => Promise<
418
+ WorkerInferOutput<TContract["workflows"][TWorkflowName]["activities"][TActivityName]>
419
+ >
420
+ : never;
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "@temporal-contract/tsconfig/base.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist",
5
+ "rootDir": "./src",
6
+ },
7
+ "include": ["src/**/*"],
8
+ "exclude": ["node_modules", "dist"],
9
+ }