@temporal-contract/contract 0.0.6 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -6
- package/dist/index.cjs +189 -117
- package/dist/index.d.cts +146 -183
- package/dist/index.d.mts +146 -183
- package/dist/index.mjs +190 -116
- package/package.json +8 -6
package/dist/index.d.cts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
2
2
|
|
|
3
3
|
//#region src/types.d.ts
|
|
4
|
-
|
|
5
4
|
/**
|
|
6
5
|
* Base types for validation schemas
|
|
7
6
|
* Any schema that implements the Standard Schema specification
|
|
@@ -88,235 +87,199 @@ type InferActivityNames<TContract extends ContractDefinition> = TContract["activ
|
|
|
88
87
|
type InferContractWorkflows<TContract extends ContractDefinition> = TContract["workflows"];
|
|
89
88
|
//#endregion
|
|
90
89
|
//#region src/builder.d.ts
|
|
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;
|
|
97
|
-
//#endregion
|
|
98
|
-
//#region src/nexus-types.d.ts
|
|
99
90
|
/**
|
|
100
|
-
*
|
|
101
|
-
* Similar to ActivityDefinition but for cross-namespace operations
|
|
91
|
+
* Define a Temporal activity with type-safe input and output schemas.
|
|
102
92
|
*
|
|
103
|
-
*
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
* input: z.object({ amount: z.number(), customerId: z.string() }),
|
|
107
|
-
* output: z.object({ transactionId: z.string(), status: z.enum(['success', 'failed']) }),
|
|
108
|
-
* };
|
|
109
|
-
* ```
|
|
110
|
-
*/
|
|
111
|
-
interface NexusOperationDefinition<TInput extends AnySchema = AnySchema, TOutput extends AnySchema = AnySchema> {
|
|
112
|
-
readonly input: TInput;
|
|
113
|
-
readonly output: TOutput;
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Definition of a Nexus service containing multiple operations
|
|
93
|
+
* Activities are the building blocks of Temporal workflows that execute business logic
|
|
94
|
+
* and interact with external services. This function preserves TypeScript types while
|
|
95
|
+
* providing a consistent structure for activity definitions.
|
|
117
96
|
*
|
|
118
|
-
* @
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
* operations: {
|
|
122
|
-
* processPayment: { input: ..., output: ... },
|
|
123
|
-
* refundPayment: { input: ..., output: ... },
|
|
124
|
-
* },
|
|
125
|
-
* };
|
|
126
|
-
* ```
|
|
127
|
-
*/
|
|
128
|
-
interface NexusServiceDefinition<TOperations extends Record<string, NexusOperationDefinition> = Record<string, NexusOperationDefinition>> {
|
|
129
|
-
readonly operations: TOperations;
|
|
130
|
-
}
|
|
131
|
-
/**
|
|
132
|
-
* Extended ContractDefinition that includes Nexus services
|
|
133
|
-
* This would replace the current ContractDefinition when Nexus support is added
|
|
97
|
+
* @template TActivity - The activity definition type with input/output schemas
|
|
98
|
+
* @param definition - The activity definition containing input and output schemas
|
|
99
|
+
* @returns The same definition with preserved types for type inference
|
|
134
100
|
*
|
|
135
101
|
* @example
|
|
136
102
|
* ```typescript
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
*
|
|
141
|
-
*
|
|
142
|
-
*
|
|
143
|
-
*
|
|
144
|
-
*
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
*
|
|
103
|
+
* import { defineActivity } from '@temporal-contract/contract';
|
|
104
|
+
* import { z } from 'zod';
|
|
105
|
+
*
|
|
106
|
+
* export const sendEmail = defineActivity({
|
|
107
|
+
* input: z.object({
|
|
108
|
+
* to: z.string().email(),
|
|
109
|
+
* subject: z.string(),
|
|
110
|
+
* body: z.string(),
|
|
111
|
+
* }),
|
|
112
|
+
* output: z.object({
|
|
113
|
+
* messageId: z.string(),
|
|
114
|
+
* sentAt: z.date(),
|
|
115
|
+
* }),
|
|
148
116
|
* });
|
|
149
117
|
* ```
|
|
150
118
|
*/
|
|
151
|
-
|
|
152
|
-
readonly taskQueue: string;
|
|
153
|
-
readonly workflows: TWorkflows;
|
|
154
|
-
readonly activities?: TActivities;
|
|
155
|
-
readonly nexusServices?: TNexusServices;
|
|
156
|
-
}
|
|
157
|
-
/**
|
|
158
|
-
* WORKER PERSPECTIVE - Nexus operation handler type inference
|
|
159
|
-
* Worker receives the parsed input and returns the raw output
|
|
160
|
-
*/
|
|
161
|
-
/**
|
|
162
|
-
* Infer input type from a Nexus operation definition (worker perspective)
|
|
163
|
-
*/
|
|
164
|
-
type WorkerInferNexusOperationInput<T extends NexusOperationDefinition> = StandardSchemaV1.InferOutput<T["input"]>;
|
|
165
|
-
/**
|
|
166
|
-
* Infer output type from a Nexus operation definition (worker perspective)
|
|
167
|
-
*/
|
|
168
|
-
type WorkerInferNexusOperationOutput<T extends NexusOperationDefinition> = StandardSchemaV1.InferInput<T["output"]>;
|
|
119
|
+
declare function defineActivity<TActivity extends ActivityDefinition>(definition: TActivity): TActivity;
|
|
169
120
|
/**
|
|
170
|
-
*
|
|
121
|
+
* Define a Temporal signal with type-safe input schema.
|
|
171
122
|
*
|
|
172
|
-
*
|
|
173
|
-
*
|
|
174
|
-
* type ProcessPaymentHandler = WorkerInferNexusOperationHandler<typeof processPaymentOperation>;
|
|
175
|
-
* // (args: { amount: number; customerId: string }) => Promise<{ transactionId: string; status: 'success' | 'failed' }>
|
|
176
|
-
* ```
|
|
177
|
-
*/
|
|
178
|
-
type WorkerInferNexusOperationHandler<TOperation extends NexusOperationDefinition> = (args: WorkerInferNexusOperationInput<TOperation>) => Promise<WorkerInferNexusOperationOutput<TOperation>>;
|
|
179
|
-
/**
|
|
180
|
-
* Infer all operation handlers for a Nexus service (worker perspective)
|
|
123
|
+
* Signals are asynchronous messages sent to running workflows to update their state
|
|
124
|
+
* or trigger certain behaviors. This function ensures type safety for signal payloads.
|
|
181
125
|
*
|
|
182
|
-
* @
|
|
183
|
-
*
|
|
184
|
-
*
|
|
185
|
-
* // {
|
|
186
|
-
* // processPayment: (args: { ... }) => Promise<{ ... }>;
|
|
187
|
-
* // refundPayment: (args: { ... }) => Promise<{ ... }>;
|
|
188
|
-
* // }
|
|
189
|
-
* ```
|
|
190
|
-
*/
|
|
191
|
-
type WorkerInferNexusServiceHandlers<T extends NexusServiceDefinition> = { [K in keyof T["operations"]]: WorkerInferNexusOperationHandler<T["operations"][K]> };
|
|
192
|
-
/**
|
|
193
|
-
* Infer all Nexus service handlers from a contract (worker perspective)
|
|
126
|
+
* @template TSignal - The signal definition type with input schema
|
|
127
|
+
* @param definition - The signal definition containing input schema
|
|
128
|
+
* @returns The same definition with preserved types for type inference
|
|
194
129
|
*
|
|
195
130
|
* @example
|
|
196
131
|
* ```typescript
|
|
197
|
-
*
|
|
198
|
-
*
|
|
199
|
-
* // PaymentService: { processPayment: ..., refundPayment: ... };
|
|
200
|
-
* // InventoryService: { reserveItems: ..., releaseItems: ... };
|
|
201
|
-
* // }
|
|
202
|
-
* ```
|
|
203
|
-
*/
|
|
204
|
-
type WorkerInferNexusServices<TContract extends ContractDefinitionWithNexus> = TContract["nexusServices"] extends Record<string, NexusServiceDefinition> ? { [K in keyof TContract["nexusServices"]]: WorkerInferNexusServiceHandlers<TContract["nexusServices"][K]> } : {};
|
|
205
|
-
/**
|
|
206
|
-
* CLIENT PERSPECTIVE - Nexus operation client type inference
|
|
207
|
-
* Client sends the raw input and receives the parsed output
|
|
208
|
-
*/
|
|
209
|
-
/**
|
|
210
|
-
* Infer input type from a Nexus operation definition (client perspective)
|
|
211
|
-
*/
|
|
212
|
-
type ClientInferNexusOperationInput<T extends NexusOperationDefinition> = StandardSchemaV1.InferInput<T["input"]>;
|
|
213
|
-
/**
|
|
214
|
-
* Infer output type from a Nexus operation definition (client perspective)
|
|
215
|
-
*/
|
|
216
|
-
type ClientInferNexusOperationOutput<T extends NexusOperationDefinition> = StandardSchemaV1.InferOutput<T["output"]>;
|
|
217
|
-
/**
|
|
218
|
-
* Infer the client function signature for a Nexus operation (client perspective)
|
|
132
|
+
* import { defineSignal } from '@temporal-contract/contract';
|
|
133
|
+
* import { z } from 'zod';
|
|
219
134
|
*
|
|
220
|
-
*
|
|
221
|
-
*
|
|
222
|
-
*
|
|
223
|
-
*
|
|
135
|
+
* export const approveOrder = defineSignal({
|
|
136
|
+
* input: z.object({
|
|
137
|
+
* orderId: z.string(),
|
|
138
|
+
* approvedBy: z.string(),
|
|
139
|
+
* }),
|
|
140
|
+
* });
|
|
224
141
|
* ```
|
|
225
142
|
*/
|
|
226
|
-
|
|
227
|
-
/**
|
|
228
|
-
* Infer all operation invokers for a Nexus service (client perspective)
|
|
229
|
-
*/
|
|
230
|
-
type ClientInferNexusServiceOperations<T extends NexusServiceDefinition> = { [K in keyof T["operations"]]: ClientInferNexusOperationInvoker<T["operations"][K]> };
|
|
143
|
+
declare function defineSignal<TSignal extends SignalDefinition>(definition: TSignal): TSignal;
|
|
231
144
|
/**
|
|
232
|
-
*
|
|
145
|
+
* Define a Temporal query with type-safe input and output schemas.
|
|
233
146
|
*
|
|
234
|
-
*
|
|
235
|
-
*
|
|
236
|
-
* type AllNexusOperations = ClientInferNexusServices<typeof myContract>;
|
|
237
|
-
* // {
|
|
238
|
-
* // PaymentService: { processPayment: ..., refundPayment: ... };
|
|
239
|
-
* // InventoryService: { reserveItems: ..., releaseItems: ... };
|
|
240
|
-
* // }
|
|
241
|
-
* ```
|
|
242
|
-
*/
|
|
243
|
-
type ClientInferNexusServices<TContract extends ContractDefinitionWithNexus> = TContract["nexusServices"] extends Record<string, NexusServiceDefinition> ? { [K in keyof TContract["nexusServices"]]: ClientInferNexusServiceOperations<TContract["nexusServices"][K]> } : {};
|
|
244
|
-
/**
|
|
245
|
-
* UTILITY TYPES
|
|
246
|
-
*/
|
|
247
|
-
/**
|
|
248
|
-
* Extract service names from a contract as a union type
|
|
147
|
+
* Queries allow you to read the current state of a running workflow without
|
|
148
|
+
* modifying it. They are synchronous and should not perform any mutations.
|
|
249
149
|
*
|
|
250
|
-
* @
|
|
251
|
-
*
|
|
252
|
-
*
|
|
253
|
-
* // "PaymentService" | "InventoryService"
|
|
254
|
-
* ```
|
|
255
|
-
*/
|
|
256
|
-
type InferNexusServiceNames<TContract extends ContractDefinitionWithNexus> = TContract["nexusServices"] extends Record<string, NexusServiceDefinition> ? keyof TContract["nexusServices"] & string : never;
|
|
257
|
-
/**
|
|
258
|
-
* Extract operation names from a service as a union type
|
|
150
|
+
* @template TQuery - The query definition type with input/output schemas
|
|
151
|
+
* @param definition - The query definition containing input and output schemas
|
|
152
|
+
* @returns The same definition with preserved types for type inference
|
|
259
153
|
*
|
|
260
154
|
* @example
|
|
261
155
|
* ```typescript
|
|
262
|
-
*
|
|
263
|
-
*
|
|
156
|
+
* import { defineQuery } from '@temporal-contract/contract';
|
|
157
|
+
* import { z } from 'zod';
|
|
158
|
+
*
|
|
159
|
+
* export const getOrderStatus = defineQuery({
|
|
160
|
+
* input: z.object({ orderId: z.string() }),
|
|
161
|
+
* output: z.object({
|
|
162
|
+
* status: z.enum(['pending', 'processing', 'completed', 'failed']),
|
|
163
|
+
* updatedAt: z.date(),
|
|
164
|
+
* }),
|
|
165
|
+
* });
|
|
264
166
|
* ```
|
|
265
167
|
*/
|
|
266
|
-
|
|
168
|
+
declare function defineQuery<TQuery extends QueryDefinition>(definition: TQuery): TQuery;
|
|
267
169
|
/**
|
|
268
|
-
*
|
|
170
|
+
* Define a Temporal update with type-safe input and output schemas.
|
|
171
|
+
*
|
|
172
|
+
* Updates are similar to signals but return a value and wait for the workflow
|
|
173
|
+
* to process them before completing. They provide a synchronous way to modify
|
|
174
|
+
* workflow state and get immediate feedback.
|
|
175
|
+
*
|
|
176
|
+
* @template TUpdate - The update definition type with input/output schemas
|
|
177
|
+
* @param definition - The update definition containing input and output schemas
|
|
178
|
+
* @returns The same definition with preserved types for type inference
|
|
269
179
|
*
|
|
270
180
|
* @example
|
|
271
181
|
* ```typescript
|
|
272
|
-
*
|
|
273
|
-
*
|
|
274
|
-
*
|
|
275
|
-
*
|
|
276
|
-
*
|
|
277
|
-
*
|
|
278
|
-
*
|
|
279
|
-
*
|
|
182
|
+
* import { defineUpdate } from '@temporal-contract/contract';
|
|
183
|
+
* import { z } from 'zod';
|
|
184
|
+
*
|
|
185
|
+
* export const updateOrderQuantity = defineUpdate({
|
|
186
|
+
* input: z.object({
|
|
187
|
+
* orderId: z.string(),
|
|
188
|
+
* newQuantity: z.number().positive(),
|
|
189
|
+
* }),
|
|
190
|
+
* output: z.object({
|
|
191
|
+
* success: z.boolean(),
|
|
192
|
+
* totalPrice: z.number(),
|
|
193
|
+
* }),
|
|
194
|
+
* });
|
|
280
195
|
* ```
|
|
281
196
|
*/
|
|
282
|
-
|
|
283
|
-
/**
|
|
284
|
-
* BUILDER FUNCTIONS (Proposed API)
|
|
285
|
-
* These would be added to builder.ts when Nexus support is implemented
|
|
286
|
-
*/
|
|
197
|
+
declare function defineUpdate<TUpdate extends UpdateDefinition>(definition: TUpdate): TUpdate;
|
|
287
198
|
/**
|
|
288
|
-
*
|
|
199
|
+
* Define a Temporal workflow with type-safe input, output, and associated operations.
|
|
200
|
+
*
|
|
201
|
+
* Workflows are durable functions that orchestrate activities, handle timeouts,
|
|
202
|
+
* and manage long-running processes. This function provides type safety for the
|
|
203
|
+
* entire workflow definition including activities, signals, queries, and updates.
|
|
289
204
|
*
|
|
290
|
-
* @template
|
|
291
|
-
* @param definition - The
|
|
292
|
-
* @returns The same definition with preserved types
|
|
205
|
+
* @template TWorkflow - The workflow definition type with all associated schemas
|
|
206
|
+
* @param definition - The workflow definition containing input, output, and operations
|
|
207
|
+
* @returns The same definition with preserved types for type inference
|
|
293
208
|
*
|
|
294
209
|
* @example
|
|
295
210
|
* ```typescript
|
|
296
|
-
*
|
|
297
|
-
*
|
|
298
|
-
*
|
|
211
|
+
* import { defineWorkflow, defineActivity, defineSignal } from '@temporal-contract/contract';
|
|
212
|
+
* import { z } from 'zod';
|
|
213
|
+
*
|
|
214
|
+
* export const processOrder = defineWorkflow({
|
|
215
|
+
* input: z.object({ orderId: z.string() }),
|
|
216
|
+
* output: z.object({ success: z.boolean() }),
|
|
217
|
+
* activities: {
|
|
218
|
+
* validatePayment: defineActivity({
|
|
219
|
+
* input: z.object({ orderId: z.string() }),
|
|
220
|
+
* output: z.object({ valid: z.boolean() }),
|
|
221
|
+
* }),
|
|
222
|
+
* },
|
|
223
|
+
* signals: {
|
|
224
|
+
* cancel: defineSignal({
|
|
225
|
+
* input: z.object({ reason: z.string() }),
|
|
226
|
+
* }),
|
|
227
|
+
* },
|
|
299
228
|
* });
|
|
300
229
|
* ```
|
|
301
230
|
*/
|
|
302
|
-
declare function
|
|
231
|
+
declare function defineWorkflow<TWorkflow extends WorkflowDefinition>(definition: TWorkflow): TWorkflow;
|
|
303
232
|
/**
|
|
304
|
-
*
|
|
233
|
+
* Define a complete Temporal contract with type-safe workflows and activities.
|
|
234
|
+
*
|
|
235
|
+
* A contract is the central definition that ties together your Temporal application's
|
|
236
|
+
* workflows and activities. It provides:
|
|
237
|
+
* - Type safety across client, worker, and workflow code
|
|
238
|
+
* - Automatic validation at runtime
|
|
239
|
+
* - Compile-time verification of implementations
|
|
240
|
+
* - Clear API boundaries and documentation
|
|
241
|
+
*
|
|
242
|
+
* The contract validates the structure and ensures:
|
|
243
|
+
* - Task queue is specified
|
|
244
|
+
* - At least one workflow is defined
|
|
245
|
+
* - Valid JavaScript identifiers are used
|
|
246
|
+
* - No conflicts between global and workflow-specific activities
|
|
247
|
+
* - All schemas implement the Standard Schema specification
|
|
305
248
|
*
|
|
306
|
-
* @template
|
|
307
|
-
* @param definition - The
|
|
308
|
-
* @returns The same definition with preserved types
|
|
249
|
+
* @template TContract - The contract definition type
|
|
250
|
+
* @param definition - The complete contract definition
|
|
251
|
+
* @returns The same definition with preserved types for type inference
|
|
252
|
+
* @throws {Error} If the contract structure is invalid
|
|
309
253
|
*
|
|
310
254
|
* @example
|
|
311
255
|
* ```typescript
|
|
312
|
-
*
|
|
313
|
-
*
|
|
314
|
-
*
|
|
315
|
-
*
|
|
256
|
+
* import { defineContract } from '@temporal-contract/contract';
|
|
257
|
+
* import { z } from 'zod';
|
|
258
|
+
*
|
|
259
|
+
* export const myContract = defineContract({
|
|
260
|
+
* taskQueue: 'orders',
|
|
261
|
+
* workflows: {
|
|
262
|
+
* processOrder: {
|
|
263
|
+
* input: z.object({ orderId: z.string() }),
|
|
264
|
+
* output: z.object({ success: z.boolean() }),
|
|
265
|
+
* activities: {
|
|
266
|
+
* chargePayment: {
|
|
267
|
+
* input: z.object({ amount: z.number() }),
|
|
268
|
+
* output: z.object({ transactionId: z.string() }),
|
|
269
|
+
* },
|
|
270
|
+
* },
|
|
271
|
+
* },
|
|
272
|
+
* },
|
|
273
|
+
* // Optional global activities shared across workflows
|
|
274
|
+
* activities: {
|
|
275
|
+
* logEvent: {
|
|
276
|
+
* input: z.object({ message: z.string() }),
|
|
277
|
+
* output: z.void(),
|
|
278
|
+
* },
|
|
316
279
|
* },
|
|
317
280
|
* });
|
|
318
281
|
* ```
|
|
319
282
|
*/
|
|
320
|
-
declare function
|
|
283
|
+
declare function defineContract<TContract extends ContractDefinition>(definition: TContract): TContract;
|
|
321
284
|
//#endregion
|
|
322
|
-
export { type ActivityDefinition, type AnySchema, type
|
|
285
|
+
export { type ActivityDefinition, type AnySchema, type ContractDefinition, type InferActivityNames, type InferContractWorkflows, type InferWorkflowNames, type QueryDefinition, type SignalDefinition, type UpdateDefinition, type WorkflowDefinition, defineActivity, defineContract, defineQuery, defineSignal, defineUpdate, defineWorkflow };
|