@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/dist/index.d.mts 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
- * Definition of a Nexus operation
101
- * Similar to ActivityDefinition but for cross-namespace operations
91
+ * Define a Temporal activity with type-safe input and output schemas.
102
92
  *
103
- * @example
104
- * ```typescript
105
- * const processPayment = {
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
- * @example
119
- * ```typescript
120
- * const PaymentService = {
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
- * const contract = defineContract({
138
- * taskQueue: 'payments',
139
- * workflows: { ... },
140
- * activities: { ... },
141
- * nexusServices: {
142
- * PaymentService: {
143
- * operations: {
144
- * processPayment: { input: ..., output: ... },
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
- interface ContractDefinitionWithNexus<TWorkflows extends Record<string, WorkflowDefinition> = Record<string, WorkflowDefinition>, TActivities extends Record<string, ActivityDefinition> = Record<string, ActivityDefinition>, TNexusServices extends Record<string, NexusServiceDefinition> = Record<string, NexusServiceDefinition>> {
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
- * Infer the handler function signature for a Nexus operation (worker perspective)
121
+ * Define a Temporal signal with type-safe input schema.
171
122
  *
172
- * @example
173
- * ```typescript
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
- * @example
183
- * ```typescript
184
- * type PaymentServiceHandlers = WorkerInferNexusServiceHandlers<typeof PaymentService>;
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
- * type AllNexusHandlers = WorkerInferNexusServices<typeof myContract>;
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
- * @example
221
- * ```typescript
222
- * type ProcessPaymentClient = ClientInferNexusOperationInvoker<typeof processPaymentOperation>;
223
- * // (args: { amount: number; customerId: string }) => Promise<{ transactionId: string; status: 'success' | 'failed' }>
135
+ * export const approveOrder = defineSignal({
136
+ * input: z.object({
137
+ * orderId: z.string(),
138
+ * approvedBy: z.string(),
139
+ * }),
140
+ * });
224
141
  * ```
225
142
  */
226
- type ClientInferNexusOperationInvoker<TOperation extends NexusOperationDefinition> = (args: ClientInferNexusOperationInput<TOperation>) => Promise<ClientInferNexusOperationOutput<TOperation>>;
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
- * Infer all Nexus service operations from a contract (client perspective)
145
+ * Define a Temporal query with type-safe input and output schemas.
233
146
  *
234
- * @example
235
- * ```typescript
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
- * @example
251
- * ```typescript
252
- * type MyServiceNames = InferNexusServiceNames<typeof myContract>;
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
- * type PaymentOperations = InferNexusOperationNames<typeof myContract, "PaymentService">;
263
- * // "processPayment" | "refundPayment"
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
- type InferNexusOperationNames<TContract extends ContractDefinitionWithNexus, TServiceName extends InferNexusServiceNames<TContract>> = TContract["nexusServices"] extends Record<string, NexusServiceDefinition> ? keyof TContract["nexusServices"][TServiceName]["operations"] & string : never;
168
+ declare function defineQuery<TQuery extends QueryDefinition>(definition: TQuery): TQuery;
267
169
  /**
268
- * Infer the handler type for a specific Nexus operation (worker perspective)
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
- * const processPayment: NexusOperationHandler<
273
- * typeof myContract,
274
- * "PaymentService",
275
- * "processPayment"
276
- * > = async ({ amount, customerId }) => {
277
- * // Implementation
278
- * return { transactionId: 'tx-123', status: 'success' };
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
- type NexusOperationHandler<TContract extends ContractDefinitionWithNexus, TServiceName extends InferNexusServiceNames<TContract>, TOperationName extends InferNexusOperationNames<TContract, TServiceName>> = TContract["nexusServices"] extends Record<string, NexusServiceDefinition> ? (args: WorkerInferNexusOperationInput<TContract["nexusServices"][TServiceName]["operations"][TOperationName]>) => Promise<WorkerInferNexusOperationOutput<TContract["nexusServices"][TServiceName]["operations"][TOperationName]>> : never;
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
- * Builder for creating Nexus operation definitions
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 TOperation - A NexusOperationDefinition containing input and output schemas
291
- * @param definition - The Nexus operation definition with typed input/output schemas
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
- * const processPayment = defineNexusOperation({
297
- * input: z.object({ amount: z.number(), customerId: z.string() }),
298
- * output: z.object({ transactionId: z.string(), status: z.enum(['success', 'failed']) }),
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 defineNexusOperation<TOperation extends NexusOperationDefinition>(definition: TOperation): TOperation;
231
+ declare function defineWorkflow<TWorkflow extends WorkflowDefinition>(definition: TWorkflow): TWorkflow;
303
232
  /**
304
- * Builder for creating Nexus service definitions
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 TService - A NexusServiceDefinition containing a record of operations
307
- * @param definition - The Nexus service definition with typed operations
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
- * const PaymentService = defineNexusService({
313
- * operations: {
314
- * processPayment: defineNexusOperation({ ... }),
315
- * refundPayment: defineNexusOperation({ ... }),
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 defineNexusService<TService extends NexusServiceDefinition>(definition: TService): TService;
283
+ declare function defineContract<TContract extends ContractDefinition>(definition: TContract): TContract;
321
284
  //#endregion
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 };
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 };