@temporal-contract/contract 0.0.3 → 0.0.4

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
@@ -189,9 +189,125 @@ const defineContract = (definition) => {
189
189
  return definition;
190
190
  };
191
191
 
192
+ //#endregion
193
+ //#region src/nexus-types.ts
194
+ /**
195
+ * BUILDER FUNCTIONS (Proposed API)
196
+ * These would be added to builder.ts when Nexus support is implemented
197
+ */
198
+ /**
199
+ * Builder for creating Nexus operation definitions
200
+ *
201
+ * @template TOperation - A NexusOperationDefinition containing input and output schemas
202
+ * @param definition - The Nexus operation definition with typed input/output schemas
203
+ * @returns The same definition with preserved types
204
+ *
205
+ * @example
206
+ * ```typescript
207
+ * const processPayment = defineNexusOperation({
208
+ * input: z.object({ amount: z.number(), customerId: z.string() }),
209
+ * output: z.object({ transactionId: z.string(), status: z.enum(['success', 'failed']) }),
210
+ * });
211
+ * ```
212
+ */
213
+ function defineNexusOperation(definition) {
214
+ return definition;
215
+ }
216
+ /**
217
+ * Builder for creating Nexus service definitions
218
+ *
219
+ * @template TService - A NexusServiceDefinition containing a record of operations
220
+ * @param definition - The Nexus service definition with typed operations
221
+ * @returns The same definition with preserved types
222
+ *
223
+ * @example
224
+ * ```typescript
225
+ * const PaymentService = defineNexusService({
226
+ * operations: {
227
+ * processPayment: defineNexusOperation({ ... }),
228
+ * refundPayment: defineNexusOperation({ ... }),
229
+ * },
230
+ * });
231
+ * ```
232
+ */
233
+ function defineNexusService(definition) {
234
+ return definition;
235
+ }
236
+ /**
237
+ * USAGE EXAMPLE
238
+ *
239
+ * This example demonstrates the complete type-safe Nexus workflow:
240
+ *
241
+ * ```typescript
242
+ * import { defineContract, defineNexusService, defineNexusOperation } from '@temporal-contract/contract';
243
+ * import { z } from 'zod';
244
+ *
245
+ * // 1. Define contract with Nexus service
246
+ * const paymentContract = defineContract({
247
+ * taskQueue: 'payments',
248
+ * workflows: { ... },
249
+ * nexusServices: {
250
+ * PaymentService: defineNexusService({
251
+ * operations: {
252
+ * processPayment: defineNexusOperation({
253
+ * input: z.object({
254
+ * amount: z.number().positive(),
255
+ * customerId: z.string().uuid(),
256
+ * }),
257
+ * output: z.object({
258
+ * transactionId: z.string(),
259
+ * status: z.enum(['success', 'failed']),
260
+ * }),
261
+ * }),
262
+ * },
263
+ * }),
264
+ * },
265
+ * });
266
+ *
267
+ * // 2. Worker implementation (type-safe handlers)
268
+ * import { createNexusHandlers } from '@temporal-contract/worker';
269
+ *
270
+ * const nexusHandlers = createNexusHandlers(paymentContract, {
271
+ * PaymentService: {
272
+ * processPayment: async ({ amount, customerId }) => {
273
+ * // ✅ Fully typed parameters
274
+ * // ✅ Input automatically validated
275
+ * const payment = await processPaymentInDatabase(customerId, amount);
276
+ * // ✅ Return value validated against schema
277
+ * return {
278
+ * transactionId: payment.id,
279
+ * status: 'success',
280
+ * };
281
+ * },
282
+ * },
283
+ * });
284
+ *
285
+ * // 3. Client usage (type-safe invocation)
286
+ * import { createNexusClient } from '@temporal-contract/client';
287
+ *
288
+ * const nexusClient = createNexusClient<typeof paymentContract>(connection, {
289
+ * namespace: 'payments-ns',
290
+ * });
291
+ *
292
+ * // ✅ Fully typed invocation
293
+ * const result = await nexusClient.invoke('PaymentService', 'processPayment', {
294
+ * amount: 100,
295
+ * customerId: 'cust-123',
296
+ * });
297
+ *
298
+ * // ❌ TypeScript errors caught at compile time
299
+ * await nexusClient.invoke('PaymentService', 'processPayment', {
300
+ * amount: -50, // Error: amount must be positive
301
+ * customerId: 'invalid', // Error: customerId must be UUID
302
+ * });
303
+ * ```
304
+ */
305
+
192
306
  //#endregion
193
307
  exports.defineActivity = defineActivity;
194
308
  exports.defineContract = defineContract;
309
+ exports.defineNexusOperation = defineNexusOperation;
310
+ exports.defineNexusService = defineNexusService;
195
311
  exports.defineQuery = defineQuery;
196
312
  exports.defineSignal = defineSignal;
197
313
  exports.defineUpdate = defineUpdate;
package/dist/index.d.cts CHANGED
@@ -349,4 +349,228 @@ declare const defineWorkflow: <TWorkflow extends WorkflowDefinition>(definition:
349
349
  */
350
350
  declare const defineContract: <TContract extends ContractDefinition>(definition: TContract) => TContract;
351
351
  //#endregion
352
- export { type ActivityDefinition, type ActivityHandler, type AnySchema, type ClientInferActivities, type ClientInferActivity, type ClientInferInput, 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 InferActivityNames, type InferContractWorkflows, type InferWorkflowNames, type QueryDefinition, type SignalDefinition, type UpdateDefinition, type WorkerInferActivities, type WorkerInferActivity, type WorkerInferInput, 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, defineQuery, defineSignal, defineUpdate, defineWorkflow };
352
+ //#region src/nexus-types.d.ts
353
+ /**
354
+ * Definition of a Nexus operation
355
+ * Similar to ActivityDefinition but for cross-namespace operations
356
+ *
357
+ * @example
358
+ * ```typescript
359
+ * const processPayment = {
360
+ * input: z.object({ amount: z.number(), customerId: z.string() }),
361
+ * output: z.object({ transactionId: z.string(), status: z.enum(['success', 'failed']) }),
362
+ * };
363
+ * ```
364
+ */
365
+ interface NexusOperationDefinition<TInput extends AnySchema = AnySchema, TOutput extends AnySchema = AnySchema> {
366
+ readonly input: TInput;
367
+ readonly output: TOutput;
368
+ }
369
+ /**
370
+ * Definition of a Nexus service containing multiple operations
371
+ *
372
+ * @example
373
+ * ```typescript
374
+ * const PaymentService = {
375
+ * operations: {
376
+ * processPayment: { input: ..., output: ... },
377
+ * refundPayment: { input: ..., output: ... },
378
+ * },
379
+ * };
380
+ * ```
381
+ */
382
+ interface NexusServiceDefinition<TOperations extends Record<string, NexusOperationDefinition> = Record<string, NexusOperationDefinition>> {
383
+ readonly operations: TOperations;
384
+ }
385
+ /**
386
+ * Extended ContractDefinition that includes Nexus services
387
+ * This would replace the current ContractDefinition when Nexus support is added
388
+ *
389
+ * @example
390
+ * ```typescript
391
+ * const contract = defineContract({
392
+ * taskQueue: 'payments',
393
+ * workflows: { ... },
394
+ * activities: { ... },
395
+ * nexusServices: {
396
+ * PaymentService: {
397
+ * operations: {
398
+ * processPayment: { input: ..., output: ... },
399
+ * },
400
+ * },
401
+ * },
402
+ * });
403
+ * ```
404
+ */
405
+ 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>> {
406
+ readonly taskQueue: string;
407
+ readonly workflows: TWorkflows;
408
+ readonly activities?: TActivities;
409
+ readonly nexusServices?: TNexusServices;
410
+ }
411
+ /**
412
+ * WORKER PERSPECTIVE - Nexus operation handler type inference
413
+ * Worker receives the parsed input and returns the raw output
414
+ */
415
+ /**
416
+ * Infer input type from a Nexus operation definition (worker perspective)
417
+ */
418
+ type WorkerInferNexusOperationInput<T extends NexusOperationDefinition> = StandardSchemaV1.InferOutput<T["input"]>;
419
+ /**
420
+ * Infer output type from a Nexus operation definition (worker perspective)
421
+ */
422
+ type WorkerInferNexusOperationOutput<T extends NexusOperationDefinition> = StandardSchemaV1.InferInput<T["output"]>;
423
+ /**
424
+ * Infer the handler function signature for a Nexus operation (worker perspective)
425
+ *
426
+ * @example
427
+ * ```typescript
428
+ * type ProcessPaymentHandler = WorkerInferNexusOperationHandler<typeof processPaymentOperation>;
429
+ * // (args: { amount: number; customerId: string }) => Promise<{ transactionId: string; status: 'success' | 'failed' }>
430
+ * ```
431
+ */
432
+ type WorkerInferNexusOperationHandler<TOperation extends NexusOperationDefinition> = (args: WorkerInferNexusOperationInput<TOperation>) => Promise<WorkerInferNexusOperationOutput<TOperation>>;
433
+ /**
434
+ * Infer all operation handlers for a Nexus service (worker perspective)
435
+ *
436
+ * @example
437
+ * ```typescript
438
+ * type PaymentServiceHandlers = WorkerInferNexusServiceHandlers<typeof PaymentService>;
439
+ * // {
440
+ * // processPayment: (args: { ... }) => Promise<{ ... }>;
441
+ * // refundPayment: (args: { ... }) => Promise<{ ... }>;
442
+ * // }
443
+ * ```
444
+ */
445
+ type WorkerInferNexusServiceHandlers<T extends NexusServiceDefinition> = { [K in keyof T["operations"]]: WorkerInferNexusOperationHandler<T["operations"][K]> };
446
+ /**
447
+ * Infer all Nexus service handlers from a contract (worker perspective)
448
+ *
449
+ * @example
450
+ * ```typescript
451
+ * type AllNexusHandlers = WorkerInferNexusServices<typeof myContract>;
452
+ * // {
453
+ * // PaymentService: { processPayment: ..., refundPayment: ... };
454
+ * // InventoryService: { reserveItems: ..., releaseItems: ... };
455
+ * // }
456
+ * ```
457
+ */
458
+ type WorkerInferNexusServices<TContract extends ContractDefinitionWithNexus> = TContract["nexusServices"] extends Record<string, NexusServiceDefinition> ? { [K in keyof TContract["nexusServices"]]: WorkerInferNexusServiceHandlers<TContract["nexusServices"][K]> } : {};
459
+ /**
460
+ * CLIENT PERSPECTIVE - Nexus operation client type inference
461
+ * Client sends the raw input and receives the parsed output
462
+ */
463
+ /**
464
+ * Infer input type from a Nexus operation definition (client perspective)
465
+ */
466
+ type ClientInferNexusOperationInput<T extends NexusOperationDefinition> = StandardSchemaV1.InferInput<T["input"]>;
467
+ /**
468
+ * Infer output type from a Nexus operation definition (client perspective)
469
+ */
470
+ type ClientInferNexusOperationOutput<T extends NexusOperationDefinition> = StandardSchemaV1.InferOutput<T["output"]>;
471
+ /**
472
+ * Infer the client function signature for a Nexus operation (client perspective)
473
+ *
474
+ * @example
475
+ * ```typescript
476
+ * type ProcessPaymentClient = ClientInferNexusOperationInvoker<typeof processPaymentOperation>;
477
+ * // (args: { amount: number; customerId: string }) => Promise<{ transactionId: string; status: 'success' | 'failed' }>
478
+ * ```
479
+ */
480
+ type ClientInferNexusOperationInvoker<TOperation extends NexusOperationDefinition> = (args: ClientInferNexusOperationInput<TOperation>) => Promise<ClientInferNexusOperationOutput<TOperation>>;
481
+ /**
482
+ * Infer all operation invokers for a Nexus service (client perspective)
483
+ */
484
+ type ClientInferNexusServiceOperations<T extends NexusServiceDefinition> = { [K in keyof T["operations"]]: ClientInferNexusOperationInvoker<T["operations"][K]> };
485
+ /**
486
+ * Infer all Nexus service operations from a contract (client perspective)
487
+ *
488
+ * @example
489
+ * ```typescript
490
+ * type AllNexusOperations = ClientInferNexusServices<typeof myContract>;
491
+ * // {
492
+ * // PaymentService: { processPayment: ..., refundPayment: ... };
493
+ * // InventoryService: { reserveItems: ..., releaseItems: ... };
494
+ * // }
495
+ * ```
496
+ */
497
+ type ClientInferNexusServices<TContract extends ContractDefinitionWithNexus> = TContract["nexusServices"] extends Record<string, NexusServiceDefinition> ? { [K in keyof TContract["nexusServices"]]: ClientInferNexusServiceOperations<TContract["nexusServices"][K]> } : {};
498
+ /**
499
+ * UTILITY TYPES
500
+ */
501
+ /**
502
+ * Extract service names from a contract as a union type
503
+ *
504
+ * @example
505
+ * ```typescript
506
+ * type MyServiceNames = InferNexusServiceNames<typeof myContract>;
507
+ * // "PaymentService" | "InventoryService"
508
+ * ```
509
+ */
510
+ type InferNexusServiceNames<TContract extends ContractDefinitionWithNexus> = TContract["nexusServices"] extends Record<string, NexusServiceDefinition> ? keyof TContract["nexusServices"] & string : never;
511
+ /**
512
+ * Extract operation names from a service as a union type
513
+ *
514
+ * @example
515
+ * ```typescript
516
+ * type PaymentOperations = InferNexusOperationNames<typeof myContract, "PaymentService">;
517
+ * // "processPayment" | "refundPayment"
518
+ * ```
519
+ */
520
+ type InferNexusOperationNames<TContract extends ContractDefinitionWithNexus, TServiceName extends InferNexusServiceNames<TContract>> = TContract["nexusServices"] extends Record<string, NexusServiceDefinition> ? keyof TContract["nexusServices"][TServiceName]["operations"] & string : never;
521
+ /**
522
+ * Infer the handler type for a specific Nexus operation (worker perspective)
523
+ *
524
+ * @example
525
+ * ```typescript
526
+ * const processPayment: NexusOperationHandler<
527
+ * typeof myContract,
528
+ * "PaymentService",
529
+ * "processPayment"
530
+ * > = async ({ amount, customerId }) => {
531
+ * // Implementation
532
+ * return { transactionId: 'tx-123', status: 'success' };
533
+ * };
534
+ * ```
535
+ */
536
+ 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;
537
+ /**
538
+ * BUILDER FUNCTIONS (Proposed API)
539
+ * These would be added to builder.ts when Nexus support is implemented
540
+ */
541
+ /**
542
+ * Builder for creating Nexus operation definitions
543
+ *
544
+ * @template TOperation - A NexusOperationDefinition containing input and output schemas
545
+ * @param definition - The Nexus operation definition with typed input/output schemas
546
+ * @returns The same definition with preserved types
547
+ *
548
+ * @example
549
+ * ```typescript
550
+ * const processPayment = defineNexusOperation({
551
+ * input: z.object({ amount: z.number(), customerId: z.string() }),
552
+ * output: z.object({ transactionId: z.string(), status: z.enum(['success', 'failed']) }),
553
+ * });
554
+ * ```
555
+ */
556
+ declare function defineNexusOperation<TOperation extends NexusOperationDefinition>(definition: TOperation): TOperation;
557
+ /**
558
+ * Builder for creating Nexus service definitions
559
+ *
560
+ * @template TService - A NexusServiceDefinition containing a record of operations
561
+ * @param definition - The Nexus service definition with typed operations
562
+ * @returns The same definition with preserved types
563
+ *
564
+ * @example
565
+ * ```typescript
566
+ * const PaymentService = defineNexusService({
567
+ * operations: {
568
+ * processPayment: defineNexusOperation({ ... }),
569
+ * refundPayment: defineNexusOperation({ ... }),
570
+ * },
571
+ * });
572
+ * ```
573
+ */
574
+ declare function defineNexusService<TService extends NexusServiceDefinition>(definition: TService): TService;
575
+ //#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 };
package/dist/index.d.mts CHANGED
@@ -349,4 +349,228 @@ declare const defineWorkflow: <TWorkflow extends WorkflowDefinition>(definition:
349
349
  */
350
350
  declare const defineContract: <TContract extends ContractDefinition>(definition: TContract) => TContract;
351
351
  //#endregion
352
- export { type ActivityDefinition, type ActivityHandler, type AnySchema, type ClientInferActivities, type ClientInferActivity, type ClientInferInput, 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 InferActivityNames, type InferContractWorkflows, type InferWorkflowNames, type QueryDefinition, type SignalDefinition, type UpdateDefinition, type WorkerInferActivities, type WorkerInferActivity, type WorkerInferInput, 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, defineQuery, defineSignal, defineUpdate, defineWorkflow };
352
+ //#region src/nexus-types.d.ts
353
+ /**
354
+ * Definition of a Nexus operation
355
+ * Similar to ActivityDefinition but for cross-namespace operations
356
+ *
357
+ * @example
358
+ * ```typescript
359
+ * const processPayment = {
360
+ * input: z.object({ amount: z.number(), customerId: z.string() }),
361
+ * output: z.object({ transactionId: z.string(), status: z.enum(['success', 'failed']) }),
362
+ * };
363
+ * ```
364
+ */
365
+ interface NexusOperationDefinition<TInput extends AnySchema = AnySchema, TOutput extends AnySchema = AnySchema> {
366
+ readonly input: TInput;
367
+ readonly output: TOutput;
368
+ }
369
+ /**
370
+ * Definition of a Nexus service containing multiple operations
371
+ *
372
+ * @example
373
+ * ```typescript
374
+ * const PaymentService = {
375
+ * operations: {
376
+ * processPayment: { input: ..., output: ... },
377
+ * refundPayment: { input: ..., output: ... },
378
+ * },
379
+ * };
380
+ * ```
381
+ */
382
+ interface NexusServiceDefinition<TOperations extends Record<string, NexusOperationDefinition> = Record<string, NexusOperationDefinition>> {
383
+ readonly operations: TOperations;
384
+ }
385
+ /**
386
+ * Extended ContractDefinition that includes Nexus services
387
+ * This would replace the current ContractDefinition when Nexus support is added
388
+ *
389
+ * @example
390
+ * ```typescript
391
+ * const contract = defineContract({
392
+ * taskQueue: 'payments',
393
+ * workflows: { ... },
394
+ * activities: { ... },
395
+ * nexusServices: {
396
+ * PaymentService: {
397
+ * operations: {
398
+ * processPayment: { input: ..., output: ... },
399
+ * },
400
+ * },
401
+ * },
402
+ * });
403
+ * ```
404
+ */
405
+ 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>> {
406
+ readonly taskQueue: string;
407
+ readonly workflows: TWorkflows;
408
+ readonly activities?: TActivities;
409
+ readonly nexusServices?: TNexusServices;
410
+ }
411
+ /**
412
+ * WORKER PERSPECTIVE - Nexus operation handler type inference
413
+ * Worker receives the parsed input and returns the raw output
414
+ */
415
+ /**
416
+ * Infer input type from a Nexus operation definition (worker perspective)
417
+ */
418
+ type WorkerInferNexusOperationInput<T extends NexusOperationDefinition> = StandardSchemaV1.InferOutput<T["input"]>;
419
+ /**
420
+ * Infer output type from a Nexus operation definition (worker perspective)
421
+ */
422
+ type WorkerInferNexusOperationOutput<T extends NexusOperationDefinition> = StandardSchemaV1.InferInput<T["output"]>;
423
+ /**
424
+ * Infer the handler function signature for a Nexus operation (worker perspective)
425
+ *
426
+ * @example
427
+ * ```typescript
428
+ * type ProcessPaymentHandler = WorkerInferNexusOperationHandler<typeof processPaymentOperation>;
429
+ * // (args: { amount: number; customerId: string }) => Promise<{ transactionId: string; status: 'success' | 'failed' }>
430
+ * ```
431
+ */
432
+ type WorkerInferNexusOperationHandler<TOperation extends NexusOperationDefinition> = (args: WorkerInferNexusOperationInput<TOperation>) => Promise<WorkerInferNexusOperationOutput<TOperation>>;
433
+ /**
434
+ * Infer all operation handlers for a Nexus service (worker perspective)
435
+ *
436
+ * @example
437
+ * ```typescript
438
+ * type PaymentServiceHandlers = WorkerInferNexusServiceHandlers<typeof PaymentService>;
439
+ * // {
440
+ * // processPayment: (args: { ... }) => Promise<{ ... }>;
441
+ * // refundPayment: (args: { ... }) => Promise<{ ... }>;
442
+ * // }
443
+ * ```
444
+ */
445
+ type WorkerInferNexusServiceHandlers<T extends NexusServiceDefinition> = { [K in keyof T["operations"]]: WorkerInferNexusOperationHandler<T["operations"][K]> };
446
+ /**
447
+ * Infer all Nexus service handlers from a contract (worker perspective)
448
+ *
449
+ * @example
450
+ * ```typescript
451
+ * type AllNexusHandlers = WorkerInferNexusServices<typeof myContract>;
452
+ * // {
453
+ * // PaymentService: { processPayment: ..., refundPayment: ... };
454
+ * // InventoryService: { reserveItems: ..., releaseItems: ... };
455
+ * // }
456
+ * ```
457
+ */
458
+ type WorkerInferNexusServices<TContract extends ContractDefinitionWithNexus> = TContract["nexusServices"] extends Record<string, NexusServiceDefinition> ? { [K in keyof TContract["nexusServices"]]: WorkerInferNexusServiceHandlers<TContract["nexusServices"][K]> } : {};
459
+ /**
460
+ * CLIENT PERSPECTIVE - Nexus operation client type inference
461
+ * Client sends the raw input and receives the parsed output
462
+ */
463
+ /**
464
+ * Infer input type from a Nexus operation definition (client perspective)
465
+ */
466
+ type ClientInferNexusOperationInput<T extends NexusOperationDefinition> = StandardSchemaV1.InferInput<T["input"]>;
467
+ /**
468
+ * Infer output type from a Nexus operation definition (client perspective)
469
+ */
470
+ type ClientInferNexusOperationOutput<T extends NexusOperationDefinition> = StandardSchemaV1.InferOutput<T["output"]>;
471
+ /**
472
+ * Infer the client function signature for a Nexus operation (client perspective)
473
+ *
474
+ * @example
475
+ * ```typescript
476
+ * type ProcessPaymentClient = ClientInferNexusOperationInvoker<typeof processPaymentOperation>;
477
+ * // (args: { amount: number; customerId: string }) => Promise<{ transactionId: string; status: 'success' | 'failed' }>
478
+ * ```
479
+ */
480
+ type ClientInferNexusOperationInvoker<TOperation extends NexusOperationDefinition> = (args: ClientInferNexusOperationInput<TOperation>) => Promise<ClientInferNexusOperationOutput<TOperation>>;
481
+ /**
482
+ * Infer all operation invokers for a Nexus service (client perspective)
483
+ */
484
+ type ClientInferNexusServiceOperations<T extends NexusServiceDefinition> = { [K in keyof T["operations"]]: ClientInferNexusOperationInvoker<T["operations"][K]> };
485
+ /**
486
+ * Infer all Nexus service operations from a contract (client perspective)
487
+ *
488
+ * @example
489
+ * ```typescript
490
+ * type AllNexusOperations = ClientInferNexusServices<typeof myContract>;
491
+ * // {
492
+ * // PaymentService: { processPayment: ..., refundPayment: ... };
493
+ * // InventoryService: { reserveItems: ..., releaseItems: ... };
494
+ * // }
495
+ * ```
496
+ */
497
+ type ClientInferNexusServices<TContract extends ContractDefinitionWithNexus> = TContract["nexusServices"] extends Record<string, NexusServiceDefinition> ? { [K in keyof TContract["nexusServices"]]: ClientInferNexusServiceOperations<TContract["nexusServices"][K]> } : {};
498
+ /**
499
+ * UTILITY TYPES
500
+ */
501
+ /**
502
+ * Extract service names from a contract as a union type
503
+ *
504
+ * @example
505
+ * ```typescript
506
+ * type MyServiceNames = InferNexusServiceNames<typeof myContract>;
507
+ * // "PaymentService" | "InventoryService"
508
+ * ```
509
+ */
510
+ type InferNexusServiceNames<TContract extends ContractDefinitionWithNexus> = TContract["nexusServices"] extends Record<string, NexusServiceDefinition> ? keyof TContract["nexusServices"] & string : never;
511
+ /**
512
+ * Extract operation names from a service as a union type
513
+ *
514
+ * @example
515
+ * ```typescript
516
+ * type PaymentOperations = InferNexusOperationNames<typeof myContract, "PaymentService">;
517
+ * // "processPayment" | "refundPayment"
518
+ * ```
519
+ */
520
+ type InferNexusOperationNames<TContract extends ContractDefinitionWithNexus, TServiceName extends InferNexusServiceNames<TContract>> = TContract["nexusServices"] extends Record<string, NexusServiceDefinition> ? keyof TContract["nexusServices"][TServiceName]["operations"] & string : never;
521
+ /**
522
+ * Infer the handler type for a specific Nexus operation (worker perspective)
523
+ *
524
+ * @example
525
+ * ```typescript
526
+ * const processPayment: NexusOperationHandler<
527
+ * typeof myContract,
528
+ * "PaymentService",
529
+ * "processPayment"
530
+ * > = async ({ amount, customerId }) => {
531
+ * // Implementation
532
+ * return { transactionId: 'tx-123', status: 'success' };
533
+ * };
534
+ * ```
535
+ */
536
+ 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;
537
+ /**
538
+ * BUILDER FUNCTIONS (Proposed API)
539
+ * These would be added to builder.ts when Nexus support is implemented
540
+ */
541
+ /**
542
+ * Builder for creating Nexus operation definitions
543
+ *
544
+ * @template TOperation - A NexusOperationDefinition containing input and output schemas
545
+ * @param definition - The Nexus operation definition with typed input/output schemas
546
+ * @returns The same definition with preserved types
547
+ *
548
+ * @example
549
+ * ```typescript
550
+ * const processPayment = defineNexusOperation({
551
+ * input: z.object({ amount: z.number(), customerId: z.string() }),
552
+ * output: z.object({ transactionId: z.string(), status: z.enum(['success', 'failed']) }),
553
+ * });
554
+ * ```
555
+ */
556
+ declare function defineNexusOperation<TOperation extends NexusOperationDefinition>(definition: TOperation): TOperation;
557
+ /**
558
+ * Builder for creating Nexus service definitions
559
+ *
560
+ * @template TService - A NexusServiceDefinition containing a record of operations
561
+ * @param definition - The Nexus service definition with typed operations
562
+ * @returns The same definition with preserved types
563
+ *
564
+ * @example
565
+ * ```typescript
566
+ * const PaymentService = defineNexusService({
567
+ * operations: {
568
+ * processPayment: defineNexusOperation({ ... }),
569
+ * refundPayment: defineNexusOperation({ ... }),
570
+ * },
571
+ * });
572
+ * ```
573
+ */
574
+ declare function defineNexusService<TService extends NexusServiceDefinition>(definition: TService): TService;
575
+ //#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 };
package/dist/index.mjs CHANGED
@@ -190,4 +190,118 @@ const defineContract = (definition) => {
190
190
  };
191
191
 
192
192
  //#endregion
193
- export { defineActivity, defineContract, defineQuery, defineSignal, defineUpdate, defineWorkflow };
193
+ //#region src/nexus-types.ts
194
+ /**
195
+ * BUILDER FUNCTIONS (Proposed API)
196
+ * These would be added to builder.ts when Nexus support is implemented
197
+ */
198
+ /**
199
+ * Builder for creating Nexus operation definitions
200
+ *
201
+ * @template TOperation - A NexusOperationDefinition containing input and output schemas
202
+ * @param definition - The Nexus operation definition with typed input/output schemas
203
+ * @returns The same definition with preserved types
204
+ *
205
+ * @example
206
+ * ```typescript
207
+ * const processPayment = defineNexusOperation({
208
+ * input: z.object({ amount: z.number(), customerId: z.string() }),
209
+ * output: z.object({ transactionId: z.string(), status: z.enum(['success', 'failed']) }),
210
+ * });
211
+ * ```
212
+ */
213
+ function defineNexusOperation(definition) {
214
+ return definition;
215
+ }
216
+ /**
217
+ * Builder for creating Nexus service definitions
218
+ *
219
+ * @template TService - A NexusServiceDefinition containing a record of operations
220
+ * @param definition - The Nexus service definition with typed operations
221
+ * @returns The same definition with preserved types
222
+ *
223
+ * @example
224
+ * ```typescript
225
+ * const PaymentService = defineNexusService({
226
+ * operations: {
227
+ * processPayment: defineNexusOperation({ ... }),
228
+ * refundPayment: defineNexusOperation({ ... }),
229
+ * },
230
+ * });
231
+ * ```
232
+ */
233
+ function defineNexusService(definition) {
234
+ return definition;
235
+ }
236
+ /**
237
+ * USAGE EXAMPLE
238
+ *
239
+ * This example demonstrates the complete type-safe Nexus workflow:
240
+ *
241
+ * ```typescript
242
+ * import { defineContract, defineNexusService, defineNexusOperation } from '@temporal-contract/contract';
243
+ * import { z } from 'zod';
244
+ *
245
+ * // 1. Define contract with Nexus service
246
+ * const paymentContract = defineContract({
247
+ * taskQueue: 'payments',
248
+ * workflows: { ... },
249
+ * nexusServices: {
250
+ * PaymentService: defineNexusService({
251
+ * operations: {
252
+ * processPayment: defineNexusOperation({
253
+ * input: z.object({
254
+ * amount: z.number().positive(),
255
+ * customerId: z.string().uuid(),
256
+ * }),
257
+ * output: z.object({
258
+ * transactionId: z.string(),
259
+ * status: z.enum(['success', 'failed']),
260
+ * }),
261
+ * }),
262
+ * },
263
+ * }),
264
+ * },
265
+ * });
266
+ *
267
+ * // 2. Worker implementation (type-safe handlers)
268
+ * import { createNexusHandlers } from '@temporal-contract/worker';
269
+ *
270
+ * const nexusHandlers = createNexusHandlers(paymentContract, {
271
+ * PaymentService: {
272
+ * processPayment: async ({ amount, customerId }) => {
273
+ * // ✅ Fully typed parameters
274
+ * // ✅ Input automatically validated
275
+ * const payment = await processPaymentInDatabase(customerId, amount);
276
+ * // ✅ Return value validated against schema
277
+ * return {
278
+ * transactionId: payment.id,
279
+ * status: 'success',
280
+ * };
281
+ * },
282
+ * },
283
+ * });
284
+ *
285
+ * // 3. Client usage (type-safe invocation)
286
+ * import { createNexusClient } from '@temporal-contract/client';
287
+ *
288
+ * const nexusClient = createNexusClient<typeof paymentContract>(connection, {
289
+ * namespace: 'payments-ns',
290
+ * });
291
+ *
292
+ * // ✅ Fully typed invocation
293
+ * const result = await nexusClient.invoke('PaymentService', 'processPayment', {
294
+ * amount: 100,
295
+ * customerId: 'cust-123',
296
+ * });
297
+ *
298
+ * // ❌ TypeScript errors caught at compile time
299
+ * await nexusClient.invoke('PaymentService', 'processPayment', {
300
+ * amount: -50, // Error: amount must be positive
301
+ * customerId: 'invalid', // Error: customerId must be UUID
302
+ * });
303
+ * ```
304
+ */
305
+
306
+ //#endregion
307
+ export { defineActivity, defineContract, defineNexusOperation, defineNexusService, defineQuery, defineSignal, defineUpdate, defineWorkflow };
package/package.json CHANGED
@@ -1,8 +1,12 @@
1
1
  {
2
2
  "name": "@temporal-contract/contract",
3
- "version": "0.0.3",
4
- "type": "module",
3
+ "version": "0.0.4",
5
4
  "description": "Contract builder for temporal-contract",
5
+ "keywords": [
6
+ "temporal",
7
+ "typescript",
8
+ "contract"
9
+ ],
6
10
  "homepage": "https://github.com/btravers/temporal-contract#readme",
7
11
  "bugs": {
8
12
  "url": "https://github.com/btravers/temporal-contract/issues"
@@ -12,16 +16,9 @@
12
16
  "url": "https://github.com/btravers/temporal-contract.git",
13
17
  "directory": "packages/contract"
14
18
  },
15
- "author": "Benoit TRAVERS <benoit.travers.frgmail.com>",
16
19
  "license": "MIT",
17
- "keywords": [
18
- "temporal",
19
- "typescript",
20
- "contract"
21
- ],
22
- "main": "./dist/index.cjs",
23
- "module": "./dist/index.mjs",
24
- "types": "./dist/index.d.mts",
20
+ "author": "Benoit TRAVERS <benoit.travers.frgmail.com>",
21
+ "type": "module",
25
22
  "exports": {
26
23
  ".": {
27
24
  "import": {
@@ -35,6 +32,9 @@
35
32
  },
36
33
  "./package.json": "./package.json"
37
34
  },
35
+ "main": "./dist/index.cjs",
36
+ "module": "./dist/index.mjs",
37
+ "types": "./dist/index.d.mts",
38
38
  "files": [
39
39
  "dist"
40
40
  ],
@@ -45,17 +45,17 @@
45
45
  "devDependencies": {
46
46
  "@vitest/coverage-v8": "4.0.15",
47
47
  "arktype": "2.1.28",
48
- "tsdown": "0.17.2",
48
+ "tsdown": "0.17.3",
49
49
  "typescript": "5.9.3",
50
50
  "valibot": "1.2.0",
51
51
  "vitest": "4.0.15",
52
- "@temporal-contract/tsconfig": "0.0.3"
52
+ "@temporal-contract/tsconfig": "0.0.4"
53
53
  },
54
54
  "scripts": {
55
- "dev": "tsdown src/index.ts --format cjs,esm --dts --watch",
56
55
  "build": "tsdown src/index.ts --format cjs,esm --dts --clean",
57
- "typecheck": "tsc --noEmit",
56
+ "dev": "tsdown src/index.ts --format cjs,esm --dts --watch",
58
57
  "test": "vitest run",
59
- "test:watch": "vitest"
58
+ "test:watch": "vitest",
59
+ "typecheck": "tsc --noEmit"
60
60
  }
61
61
  }