@temporal-contract/contract 0.0.6 → 0.0.7

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,26 +1,214 @@
1
1
  let zod = require("zod");
2
2
 
3
3
  //#region src/builder.ts
4
+ /**
5
+ * Define a Temporal activity with type-safe input and output schemas.
6
+ *
7
+ * Activities are the building blocks of Temporal workflows that execute business logic
8
+ * and interact with external services. This function preserves TypeScript types while
9
+ * providing a consistent structure for activity definitions.
10
+ *
11
+ * @template TActivity - The activity definition type with input/output schemas
12
+ * @param definition - The activity definition containing input and output schemas
13
+ * @returns The same definition with preserved types for type inference
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * import { defineActivity } from '@temporal-contract/contract';
18
+ * import { z } from 'zod';
19
+ *
20
+ * export const sendEmail = defineActivity({
21
+ * input: z.object({
22
+ * to: z.string().email(),
23
+ * subject: z.string(),
24
+ * body: z.string(),
25
+ * }),
26
+ * output: z.object({
27
+ * messageId: z.string(),
28
+ * sentAt: z.date(),
29
+ * }),
30
+ * });
31
+ * ```
32
+ */
4
33
  function defineActivity(definition) {
5
34
  return definition;
6
35
  }
36
+ /**
37
+ * Define a Temporal signal with type-safe input schema.
38
+ *
39
+ * Signals are asynchronous messages sent to running workflows to update their state
40
+ * or trigger certain behaviors. This function ensures type safety for signal payloads.
41
+ *
42
+ * @template TSignal - The signal definition type with input schema
43
+ * @param definition - The signal definition containing input schema
44
+ * @returns The same definition with preserved types for type inference
45
+ *
46
+ * @example
47
+ * ```typescript
48
+ * import { defineSignal } from '@temporal-contract/contract';
49
+ * import { z } from 'zod';
50
+ *
51
+ * export const approveOrder = defineSignal({
52
+ * input: z.object({
53
+ * orderId: z.string(),
54
+ * approvedBy: z.string(),
55
+ * }),
56
+ * });
57
+ * ```
58
+ */
7
59
  function defineSignal(definition) {
8
60
  return definition;
9
61
  }
62
+ /**
63
+ * Define a Temporal query with type-safe input and output schemas.
64
+ *
65
+ * Queries allow you to read the current state of a running workflow without
66
+ * modifying it. They are synchronous and should not perform any mutations.
67
+ *
68
+ * @template TQuery - The query definition type with input/output schemas
69
+ * @param definition - The query definition containing input and output schemas
70
+ * @returns The same definition with preserved types for type inference
71
+ *
72
+ * @example
73
+ * ```typescript
74
+ * import { defineQuery } from '@temporal-contract/contract';
75
+ * import { z } from 'zod';
76
+ *
77
+ * export const getOrderStatus = defineQuery({
78
+ * input: z.object({ orderId: z.string() }),
79
+ * output: z.object({
80
+ * status: z.enum(['pending', 'processing', 'completed', 'failed']),
81
+ * updatedAt: z.date(),
82
+ * }),
83
+ * });
84
+ * ```
85
+ */
10
86
  function defineQuery(definition) {
11
87
  return definition;
12
88
  }
89
+ /**
90
+ * Define a Temporal update with type-safe input and output schemas.
91
+ *
92
+ * Updates are similar to signals but return a value and wait for the workflow
93
+ * to process them before completing. They provide a synchronous way to modify
94
+ * workflow state and get immediate feedback.
95
+ *
96
+ * @template TUpdate - The update definition type with input/output schemas
97
+ * @param definition - The update definition containing input and output schemas
98
+ * @returns The same definition with preserved types for type inference
99
+ *
100
+ * @example
101
+ * ```typescript
102
+ * import { defineUpdate } from '@temporal-contract/contract';
103
+ * import { z } from 'zod';
104
+ *
105
+ * export const updateOrderQuantity = defineUpdate({
106
+ * input: z.object({
107
+ * orderId: z.string(),
108
+ * newQuantity: z.number().positive(),
109
+ * }),
110
+ * output: z.object({
111
+ * success: z.boolean(),
112
+ * totalPrice: z.number(),
113
+ * }),
114
+ * });
115
+ * ```
116
+ */
13
117
  function defineUpdate(definition) {
14
118
  return definition;
15
119
  }
120
+ /**
121
+ * Define a Temporal workflow with type-safe input, output, and associated operations.
122
+ *
123
+ * Workflows are durable functions that orchestrate activities, handle timeouts,
124
+ * and manage long-running processes. This function provides type safety for the
125
+ * entire workflow definition including activities, signals, queries, and updates.
126
+ *
127
+ * @template TWorkflow - The workflow definition type with all associated schemas
128
+ * @param definition - The workflow definition containing input, output, and operations
129
+ * @returns The same definition with preserved types for type inference
130
+ *
131
+ * @example
132
+ * ```typescript
133
+ * import { defineWorkflow, defineActivity, defineSignal } from '@temporal-contract/contract';
134
+ * import { z } from 'zod';
135
+ *
136
+ * export const processOrder = defineWorkflow({
137
+ * input: z.object({ orderId: z.string() }),
138
+ * output: z.object({ success: z.boolean() }),
139
+ * activities: {
140
+ * validatePayment: defineActivity({
141
+ * input: z.object({ orderId: z.string() }),
142
+ * output: z.object({ valid: z.boolean() }),
143
+ * }),
144
+ * },
145
+ * signals: {
146
+ * cancel: defineSignal({
147
+ * input: z.object({ reason: z.string() }),
148
+ * }),
149
+ * },
150
+ * });
151
+ * ```
152
+ */
16
153
  function defineWorkflow(definition) {
17
154
  return definition;
18
155
  }
156
+ /**
157
+ * Define a complete Temporal contract with type-safe workflows and activities.
158
+ *
159
+ * A contract is the central definition that ties together your Temporal application's
160
+ * workflows and activities. It provides:
161
+ * - Type safety across client, worker, and workflow code
162
+ * - Automatic validation at runtime
163
+ * - Compile-time verification of implementations
164
+ * - Clear API boundaries and documentation
165
+ *
166
+ * The contract validates the structure and ensures:
167
+ * - Task queue is specified
168
+ * - At least one workflow is defined
169
+ * - Valid JavaScript identifiers are used
170
+ * - No conflicts between global and workflow-specific activities
171
+ * - All schemas implement the Standard Schema specification
172
+ *
173
+ * @template TContract - The contract definition type
174
+ * @param definition - The complete contract definition
175
+ * @returns The same definition with preserved types for type inference
176
+ * @throws {Error} If the contract structure is invalid
177
+ *
178
+ * @example
179
+ * ```typescript
180
+ * import { defineContract } from '@temporal-contract/contract';
181
+ * import { z } from 'zod';
182
+ *
183
+ * export const myContract = defineContract({
184
+ * taskQueue: 'orders',
185
+ * workflows: {
186
+ * processOrder: {
187
+ * input: z.object({ orderId: z.string() }),
188
+ * output: z.object({ success: z.boolean() }),
189
+ * activities: {
190
+ * chargePayment: {
191
+ * input: z.object({ amount: z.number() }),
192
+ * output: z.object({ transactionId: z.string() }),
193
+ * },
194
+ * },
195
+ * },
196
+ * },
197
+ * // Optional global activities shared across workflows
198
+ * activities: {
199
+ * logEvent: {
200
+ * input: z.object({ message: z.string() }),
201
+ * output: z.void(),
202
+ * },
203
+ * },
204
+ * });
205
+ * ```
206
+ */
19
207
  function defineContract(definition) {
20
208
  const validationResult = contractValidationSchema.safeParse(definition);
21
209
  if (!validationResult.success) {
22
210
  const cleanMessage = getCleanErrorMessage(validationResult.error);
23
- throw new Error(`Contract error: ${cleanMessage}`);
211
+ throw new Error(`Contract validation failed: ${cleanMessage}`);
24
212
  }
25
213
  return definition;
26
214
  }
package/dist/index.d.cts CHANGED
@@ -88,11 +88,199 @@ type InferActivityNames<TContract extends ContractDefinition> = TContract["activ
88
88
  type InferContractWorkflows<TContract extends ContractDefinition> = TContract["workflows"];
89
89
  //#endregion
90
90
  //#region src/builder.d.ts
91
+ /**
92
+ * Define a Temporal activity with type-safe input and output schemas.
93
+ *
94
+ * Activities are the building blocks of Temporal workflows that execute business logic
95
+ * and interact with external services. This function preserves TypeScript types while
96
+ * providing a consistent structure for activity definitions.
97
+ *
98
+ * @template TActivity - The activity definition type with input/output schemas
99
+ * @param definition - The activity definition containing input and output schemas
100
+ * @returns The same definition with preserved types for type inference
101
+ *
102
+ * @example
103
+ * ```typescript
104
+ * import { defineActivity } from '@temporal-contract/contract';
105
+ * import { z } from 'zod';
106
+ *
107
+ * export const sendEmail = defineActivity({
108
+ * input: z.object({
109
+ * to: z.string().email(),
110
+ * subject: z.string(),
111
+ * body: z.string(),
112
+ * }),
113
+ * output: z.object({
114
+ * messageId: z.string(),
115
+ * sentAt: z.date(),
116
+ * }),
117
+ * });
118
+ * ```
119
+ */
91
120
  declare function defineActivity<TActivity extends ActivityDefinition>(definition: TActivity): TActivity;
121
+ /**
122
+ * Define a Temporal signal with type-safe input schema.
123
+ *
124
+ * Signals are asynchronous messages sent to running workflows to update their state
125
+ * or trigger certain behaviors. This function ensures type safety for signal payloads.
126
+ *
127
+ * @template TSignal - The signal definition type with input schema
128
+ * @param definition - The signal definition containing input schema
129
+ * @returns The same definition with preserved types for type inference
130
+ *
131
+ * @example
132
+ * ```typescript
133
+ * import { defineSignal } from '@temporal-contract/contract';
134
+ * import { z } from 'zod';
135
+ *
136
+ * export const approveOrder = defineSignal({
137
+ * input: z.object({
138
+ * orderId: z.string(),
139
+ * approvedBy: z.string(),
140
+ * }),
141
+ * });
142
+ * ```
143
+ */
92
144
  declare function defineSignal<TSignal extends SignalDefinition>(definition: TSignal): TSignal;
145
+ /**
146
+ * Define a Temporal query with type-safe input and output schemas.
147
+ *
148
+ * Queries allow you to read the current state of a running workflow without
149
+ * modifying it. They are synchronous and should not perform any mutations.
150
+ *
151
+ * @template TQuery - The query definition type with input/output schemas
152
+ * @param definition - The query definition containing input and output schemas
153
+ * @returns The same definition with preserved types for type inference
154
+ *
155
+ * @example
156
+ * ```typescript
157
+ * import { defineQuery } from '@temporal-contract/contract';
158
+ * import { z } from 'zod';
159
+ *
160
+ * export const getOrderStatus = defineQuery({
161
+ * input: z.object({ orderId: z.string() }),
162
+ * output: z.object({
163
+ * status: z.enum(['pending', 'processing', 'completed', 'failed']),
164
+ * updatedAt: z.date(),
165
+ * }),
166
+ * });
167
+ * ```
168
+ */
93
169
  declare function defineQuery<TQuery extends QueryDefinition>(definition: TQuery): TQuery;
170
+ /**
171
+ * Define a Temporal update with type-safe input and output schemas.
172
+ *
173
+ * Updates are similar to signals but return a value and wait for the workflow
174
+ * to process them before completing. They provide a synchronous way to modify
175
+ * workflow state and get immediate feedback.
176
+ *
177
+ * @template TUpdate - The update definition type with input/output schemas
178
+ * @param definition - The update definition containing input and output schemas
179
+ * @returns The same definition with preserved types for type inference
180
+ *
181
+ * @example
182
+ * ```typescript
183
+ * import { defineUpdate } from '@temporal-contract/contract';
184
+ * import { z } from 'zod';
185
+ *
186
+ * export const updateOrderQuantity = defineUpdate({
187
+ * input: z.object({
188
+ * orderId: z.string(),
189
+ * newQuantity: z.number().positive(),
190
+ * }),
191
+ * output: z.object({
192
+ * success: z.boolean(),
193
+ * totalPrice: z.number(),
194
+ * }),
195
+ * });
196
+ * ```
197
+ */
94
198
  declare function defineUpdate<TUpdate extends UpdateDefinition>(definition: TUpdate): TUpdate;
199
+ /**
200
+ * Define a Temporal workflow with type-safe input, output, and associated operations.
201
+ *
202
+ * Workflows are durable functions that orchestrate activities, handle timeouts,
203
+ * and manage long-running processes. This function provides type safety for the
204
+ * entire workflow definition including activities, signals, queries, and updates.
205
+ *
206
+ * @template TWorkflow - The workflow definition type with all associated schemas
207
+ * @param definition - The workflow definition containing input, output, and operations
208
+ * @returns The same definition with preserved types for type inference
209
+ *
210
+ * @example
211
+ * ```typescript
212
+ * import { defineWorkflow, defineActivity, defineSignal } from '@temporal-contract/contract';
213
+ * import { z } from 'zod';
214
+ *
215
+ * export const processOrder = defineWorkflow({
216
+ * input: z.object({ orderId: z.string() }),
217
+ * output: z.object({ success: z.boolean() }),
218
+ * activities: {
219
+ * validatePayment: defineActivity({
220
+ * input: z.object({ orderId: z.string() }),
221
+ * output: z.object({ valid: z.boolean() }),
222
+ * }),
223
+ * },
224
+ * signals: {
225
+ * cancel: defineSignal({
226
+ * input: z.object({ reason: z.string() }),
227
+ * }),
228
+ * },
229
+ * });
230
+ * ```
231
+ */
95
232
  declare function defineWorkflow<TWorkflow extends WorkflowDefinition>(definition: TWorkflow): TWorkflow;
233
+ /**
234
+ * Define a complete Temporal contract with type-safe workflows and activities.
235
+ *
236
+ * A contract is the central definition that ties together your Temporal application's
237
+ * workflows and activities. It provides:
238
+ * - Type safety across client, worker, and workflow code
239
+ * - Automatic validation at runtime
240
+ * - Compile-time verification of implementations
241
+ * - Clear API boundaries and documentation
242
+ *
243
+ * The contract validates the structure and ensures:
244
+ * - Task queue is specified
245
+ * - At least one workflow is defined
246
+ * - Valid JavaScript identifiers are used
247
+ * - No conflicts between global and workflow-specific activities
248
+ * - All schemas implement the Standard Schema specification
249
+ *
250
+ * @template TContract - The contract definition type
251
+ * @param definition - The complete contract definition
252
+ * @returns The same definition with preserved types for type inference
253
+ * @throws {Error} If the contract structure is invalid
254
+ *
255
+ * @example
256
+ * ```typescript
257
+ * import { defineContract } from '@temporal-contract/contract';
258
+ * import { z } from 'zod';
259
+ *
260
+ * export const myContract = defineContract({
261
+ * taskQueue: 'orders',
262
+ * workflows: {
263
+ * processOrder: {
264
+ * input: z.object({ orderId: z.string() }),
265
+ * output: z.object({ success: z.boolean() }),
266
+ * activities: {
267
+ * chargePayment: {
268
+ * input: z.object({ amount: z.number() }),
269
+ * output: z.object({ transactionId: z.string() }),
270
+ * },
271
+ * },
272
+ * },
273
+ * },
274
+ * // Optional global activities shared across workflows
275
+ * activities: {
276
+ * logEvent: {
277
+ * input: z.object({ message: z.string() }),
278
+ * output: z.void(),
279
+ * },
280
+ * },
281
+ * });
282
+ * ```
283
+ */
96
284
  declare function defineContract<TContract extends ContractDefinition>(definition: TContract): TContract;
97
285
  //#endregion
98
286
  //#region src/nexus-types.d.ts
package/dist/index.d.mts CHANGED
@@ -88,11 +88,199 @@ type InferActivityNames<TContract extends ContractDefinition> = TContract["activ
88
88
  type InferContractWorkflows<TContract extends ContractDefinition> = TContract["workflows"];
89
89
  //#endregion
90
90
  //#region src/builder.d.ts
91
+ /**
92
+ * Define a Temporal activity with type-safe input and output schemas.
93
+ *
94
+ * Activities are the building blocks of Temporal workflows that execute business logic
95
+ * and interact with external services. This function preserves TypeScript types while
96
+ * providing a consistent structure for activity definitions.
97
+ *
98
+ * @template TActivity - The activity definition type with input/output schemas
99
+ * @param definition - The activity definition containing input and output schemas
100
+ * @returns The same definition with preserved types for type inference
101
+ *
102
+ * @example
103
+ * ```typescript
104
+ * import { defineActivity } from '@temporal-contract/contract';
105
+ * import { z } from 'zod';
106
+ *
107
+ * export const sendEmail = defineActivity({
108
+ * input: z.object({
109
+ * to: z.string().email(),
110
+ * subject: z.string(),
111
+ * body: z.string(),
112
+ * }),
113
+ * output: z.object({
114
+ * messageId: z.string(),
115
+ * sentAt: z.date(),
116
+ * }),
117
+ * });
118
+ * ```
119
+ */
91
120
  declare function defineActivity<TActivity extends ActivityDefinition>(definition: TActivity): TActivity;
121
+ /**
122
+ * Define a Temporal signal with type-safe input schema.
123
+ *
124
+ * Signals are asynchronous messages sent to running workflows to update their state
125
+ * or trigger certain behaviors. This function ensures type safety for signal payloads.
126
+ *
127
+ * @template TSignal - The signal definition type with input schema
128
+ * @param definition - The signal definition containing input schema
129
+ * @returns The same definition with preserved types for type inference
130
+ *
131
+ * @example
132
+ * ```typescript
133
+ * import { defineSignal } from '@temporal-contract/contract';
134
+ * import { z } from 'zod';
135
+ *
136
+ * export const approveOrder = defineSignal({
137
+ * input: z.object({
138
+ * orderId: z.string(),
139
+ * approvedBy: z.string(),
140
+ * }),
141
+ * });
142
+ * ```
143
+ */
92
144
  declare function defineSignal<TSignal extends SignalDefinition>(definition: TSignal): TSignal;
145
+ /**
146
+ * Define a Temporal query with type-safe input and output schemas.
147
+ *
148
+ * Queries allow you to read the current state of a running workflow without
149
+ * modifying it. They are synchronous and should not perform any mutations.
150
+ *
151
+ * @template TQuery - The query definition type with input/output schemas
152
+ * @param definition - The query definition containing input and output schemas
153
+ * @returns The same definition with preserved types for type inference
154
+ *
155
+ * @example
156
+ * ```typescript
157
+ * import { defineQuery } from '@temporal-contract/contract';
158
+ * import { z } from 'zod';
159
+ *
160
+ * export const getOrderStatus = defineQuery({
161
+ * input: z.object({ orderId: z.string() }),
162
+ * output: z.object({
163
+ * status: z.enum(['pending', 'processing', 'completed', 'failed']),
164
+ * updatedAt: z.date(),
165
+ * }),
166
+ * });
167
+ * ```
168
+ */
93
169
  declare function defineQuery<TQuery extends QueryDefinition>(definition: TQuery): TQuery;
170
+ /**
171
+ * Define a Temporal update with type-safe input and output schemas.
172
+ *
173
+ * Updates are similar to signals but return a value and wait for the workflow
174
+ * to process them before completing. They provide a synchronous way to modify
175
+ * workflow state and get immediate feedback.
176
+ *
177
+ * @template TUpdate - The update definition type with input/output schemas
178
+ * @param definition - The update definition containing input and output schemas
179
+ * @returns The same definition with preserved types for type inference
180
+ *
181
+ * @example
182
+ * ```typescript
183
+ * import { defineUpdate } from '@temporal-contract/contract';
184
+ * import { z } from 'zod';
185
+ *
186
+ * export const updateOrderQuantity = defineUpdate({
187
+ * input: z.object({
188
+ * orderId: z.string(),
189
+ * newQuantity: z.number().positive(),
190
+ * }),
191
+ * output: z.object({
192
+ * success: z.boolean(),
193
+ * totalPrice: z.number(),
194
+ * }),
195
+ * });
196
+ * ```
197
+ */
94
198
  declare function defineUpdate<TUpdate extends UpdateDefinition>(definition: TUpdate): TUpdate;
199
+ /**
200
+ * Define a Temporal workflow with type-safe input, output, and associated operations.
201
+ *
202
+ * Workflows are durable functions that orchestrate activities, handle timeouts,
203
+ * and manage long-running processes. This function provides type safety for the
204
+ * entire workflow definition including activities, signals, queries, and updates.
205
+ *
206
+ * @template TWorkflow - The workflow definition type with all associated schemas
207
+ * @param definition - The workflow definition containing input, output, and operations
208
+ * @returns The same definition with preserved types for type inference
209
+ *
210
+ * @example
211
+ * ```typescript
212
+ * import { defineWorkflow, defineActivity, defineSignal } from '@temporal-contract/contract';
213
+ * import { z } from 'zod';
214
+ *
215
+ * export const processOrder = defineWorkflow({
216
+ * input: z.object({ orderId: z.string() }),
217
+ * output: z.object({ success: z.boolean() }),
218
+ * activities: {
219
+ * validatePayment: defineActivity({
220
+ * input: z.object({ orderId: z.string() }),
221
+ * output: z.object({ valid: z.boolean() }),
222
+ * }),
223
+ * },
224
+ * signals: {
225
+ * cancel: defineSignal({
226
+ * input: z.object({ reason: z.string() }),
227
+ * }),
228
+ * },
229
+ * });
230
+ * ```
231
+ */
95
232
  declare function defineWorkflow<TWorkflow extends WorkflowDefinition>(definition: TWorkflow): TWorkflow;
233
+ /**
234
+ * Define a complete Temporal contract with type-safe workflows and activities.
235
+ *
236
+ * A contract is the central definition that ties together your Temporal application's
237
+ * workflows and activities. It provides:
238
+ * - Type safety across client, worker, and workflow code
239
+ * - Automatic validation at runtime
240
+ * - Compile-time verification of implementations
241
+ * - Clear API boundaries and documentation
242
+ *
243
+ * The contract validates the structure and ensures:
244
+ * - Task queue is specified
245
+ * - At least one workflow is defined
246
+ * - Valid JavaScript identifiers are used
247
+ * - No conflicts between global and workflow-specific activities
248
+ * - All schemas implement the Standard Schema specification
249
+ *
250
+ * @template TContract - The contract definition type
251
+ * @param definition - The complete contract definition
252
+ * @returns The same definition with preserved types for type inference
253
+ * @throws {Error} If the contract structure is invalid
254
+ *
255
+ * @example
256
+ * ```typescript
257
+ * import { defineContract } from '@temporal-contract/contract';
258
+ * import { z } from 'zod';
259
+ *
260
+ * export const myContract = defineContract({
261
+ * taskQueue: 'orders',
262
+ * workflows: {
263
+ * processOrder: {
264
+ * input: z.object({ orderId: z.string() }),
265
+ * output: z.object({ success: z.boolean() }),
266
+ * activities: {
267
+ * chargePayment: {
268
+ * input: z.object({ amount: z.number() }),
269
+ * output: z.object({ transactionId: z.string() }),
270
+ * },
271
+ * },
272
+ * },
273
+ * },
274
+ * // Optional global activities shared across workflows
275
+ * activities: {
276
+ * logEvent: {
277
+ * input: z.object({ message: z.string() }),
278
+ * output: z.void(),
279
+ * },
280
+ * },
281
+ * });
282
+ * ```
283
+ */
96
284
  declare function defineContract<TContract extends ContractDefinition>(definition: TContract): TContract;
97
285
  //#endregion
98
286
  //#region src/nexus-types.d.ts
package/dist/index.mjs CHANGED
@@ -1,26 +1,214 @@
1
1
  import { z } from "zod";
2
2
 
3
3
  //#region src/builder.ts
4
+ /**
5
+ * Define a Temporal activity with type-safe input and output schemas.
6
+ *
7
+ * Activities are the building blocks of Temporal workflows that execute business logic
8
+ * and interact with external services. This function preserves TypeScript types while
9
+ * providing a consistent structure for activity definitions.
10
+ *
11
+ * @template TActivity - The activity definition type with input/output schemas
12
+ * @param definition - The activity definition containing input and output schemas
13
+ * @returns The same definition with preserved types for type inference
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * import { defineActivity } from '@temporal-contract/contract';
18
+ * import { z } from 'zod';
19
+ *
20
+ * export const sendEmail = defineActivity({
21
+ * input: z.object({
22
+ * to: z.string().email(),
23
+ * subject: z.string(),
24
+ * body: z.string(),
25
+ * }),
26
+ * output: z.object({
27
+ * messageId: z.string(),
28
+ * sentAt: z.date(),
29
+ * }),
30
+ * });
31
+ * ```
32
+ */
4
33
  function defineActivity(definition) {
5
34
  return definition;
6
35
  }
36
+ /**
37
+ * Define a Temporal signal with type-safe input schema.
38
+ *
39
+ * Signals are asynchronous messages sent to running workflows to update their state
40
+ * or trigger certain behaviors. This function ensures type safety for signal payloads.
41
+ *
42
+ * @template TSignal - The signal definition type with input schema
43
+ * @param definition - The signal definition containing input schema
44
+ * @returns The same definition with preserved types for type inference
45
+ *
46
+ * @example
47
+ * ```typescript
48
+ * import { defineSignal } from '@temporal-contract/contract';
49
+ * import { z } from 'zod';
50
+ *
51
+ * export const approveOrder = defineSignal({
52
+ * input: z.object({
53
+ * orderId: z.string(),
54
+ * approvedBy: z.string(),
55
+ * }),
56
+ * });
57
+ * ```
58
+ */
7
59
  function defineSignal(definition) {
8
60
  return definition;
9
61
  }
62
+ /**
63
+ * Define a Temporal query with type-safe input and output schemas.
64
+ *
65
+ * Queries allow you to read the current state of a running workflow without
66
+ * modifying it. They are synchronous and should not perform any mutations.
67
+ *
68
+ * @template TQuery - The query definition type with input/output schemas
69
+ * @param definition - The query definition containing input and output schemas
70
+ * @returns The same definition with preserved types for type inference
71
+ *
72
+ * @example
73
+ * ```typescript
74
+ * import { defineQuery } from '@temporal-contract/contract';
75
+ * import { z } from 'zod';
76
+ *
77
+ * export const getOrderStatus = defineQuery({
78
+ * input: z.object({ orderId: z.string() }),
79
+ * output: z.object({
80
+ * status: z.enum(['pending', 'processing', 'completed', 'failed']),
81
+ * updatedAt: z.date(),
82
+ * }),
83
+ * });
84
+ * ```
85
+ */
10
86
  function defineQuery(definition) {
11
87
  return definition;
12
88
  }
89
+ /**
90
+ * Define a Temporal update with type-safe input and output schemas.
91
+ *
92
+ * Updates are similar to signals but return a value and wait for the workflow
93
+ * to process them before completing. They provide a synchronous way to modify
94
+ * workflow state and get immediate feedback.
95
+ *
96
+ * @template TUpdate - The update definition type with input/output schemas
97
+ * @param definition - The update definition containing input and output schemas
98
+ * @returns The same definition with preserved types for type inference
99
+ *
100
+ * @example
101
+ * ```typescript
102
+ * import { defineUpdate } from '@temporal-contract/contract';
103
+ * import { z } from 'zod';
104
+ *
105
+ * export const updateOrderQuantity = defineUpdate({
106
+ * input: z.object({
107
+ * orderId: z.string(),
108
+ * newQuantity: z.number().positive(),
109
+ * }),
110
+ * output: z.object({
111
+ * success: z.boolean(),
112
+ * totalPrice: z.number(),
113
+ * }),
114
+ * });
115
+ * ```
116
+ */
13
117
  function defineUpdate(definition) {
14
118
  return definition;
15
119
  }
120
+ /**
121
+ * Define a Temporal workflow with type-safe input, output, and associated operations.
122
+ *
123
+ * Workflows are durable functions that orchestrate activities, handle timeouts,
124
+ * and manage long-running processes. This function provides type safety for the
125
+ * entire workflow definition including activities, signals, queries, and updates.
126
+ *
127
+ * @template TWorkflow - The workflow definition type with all associated schemas
128
+ * @param definition - The workflow definition containing input, output, and operations
129
+ * @returns The same definition with preserved types for type inference
130
+ *
131
+ * @example
132
+ * ```typescript
133
+ * import { defineWorkflow, defineActivity, defineSignal } from '@temporal-contract/contract';
134
+ * import { z } from 'zod';
135
+ *
136
+ * export const processOrder = defineWorkflow({
137
+ * input: z.object({ orderId: z.string() }),
138
+ * output: z.object({ success: z.boolean() }),
139
+ * activities: {
140
+ * validatePayment: defineActivity({
141
+ * input: z.object({ orderId: z.string() }),
142
+ * output: z.object({ valid: z.boolean() }),
143
+ * }),
144
+ * },
145
+ * signals: {
146
+ * cancel: defineSignal({
147
+ * input: z.object({ reason: z.string() }),
148
+ * }),
149
+ * },
150
+ * });
151
+ * ```
152
+ */
16
153
  function defineWorkflow(definition) {
17
154
  return definition;
18
155
  }
156
+ /**
157
+ * Define a complete Temporal contract with type-safe workflows and activities.
158
+ *
159
+ * A contract is the central definition that ties together your Temporal application's
160
+ * workflows and activities. It provides:
161
+ * - Type safety across client, worker, and workflow code
162
+ * - Automatic validation at runtime
163
+ * - Compile-time verification of implementations
164
+ * - Clear API boundaries and documentation
165
+ *
166
+ * The contract validates the structure and ensures:
167
+ * - Task queue is specified
168
+ * - At least one workflow is defined
169
+ * - Valid JavaScript identifiers are used
170
+ * - No conflicts between global and workflow-specific activities
171
+ * - All schemas implement the Standard Schema specification
172
+ *
173
+ * @template TContract - The contract definition type
174
+ * @param definition - The complete contract definition
175
+ * @returns The same definition with preserved types for type inference
176
+ * @throws {Error} If the contract structure is invalid
177
+ *
178
+ * @example
179
+ * ```typescript
180
+ * import { defineContract } from '@temporal-contract/contract';
181
+ * import { z } from 'zod';
182
+ *
183
+ * export const myContract = defineContract({
184
+ * taskQueue: 'orders',
185
+ * workflows: {
186
+ * processOrder: {
187
+ * input: z.object({ orderId: z.string() }),
188
+ * output: z.object({ success: z.boolean() }),
189
+ * activities: {
190
+ * chargePayment: {
191
+ * input: z.object({ amount: z.number() }),
192
+ * output: z.object({ transactionId: z.string() }),
193
+ * },
194
+ * },
195
+ * },
196
+ * },
197
+ * // Optional global activities shared across workflows
198
+ * activities: {
199
+ * logEvent: {
200
+ * input: z.object({ message: z.string() }),
201
+ * output: z.void(),
202
+ * },
203
+ * },
204
+ * });
205
+ * ```
206
+ */
19
207
  function defineContract(definition) {
20
208
  const validationResult = contractValidationSchema.safeParse(definition);
21
209
  if (!validationResult.success) {
22
210
  const cleanMessage = getCleanErrorMessage(validationResult.error);
23
- throw new Error(`Contract error: ${cleanMessage}`);
211
+ throw new Error(`Contract validation failed: ${cleanMessage}`);
24
212
  }
25
213
  return definition;
26
214
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@temporal-contract/contract",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "description": "Contract builder for temporal-contract",
5
5
  "keywords": [
6
6
  "contract",
@@ -49,7 +49,7 @@
49
49
  "typescript": "5.9.3",
50
50
  "valibot": "1.2.0",
51
51
  "vitest": "4.0.16",
52
- "@temporal-contract/tsconfig": "0.0.6"
52
+ "@temporal-contract/tsconfig": "0.0.7"
53
53
  },
54
54
  "scripts": {
55
55
  "build": "tsdown src/index.ts --format cjs,esm --dts --clean",