@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/README.md
CHANGED
|
@@ -13,18 +13,20 @@ pnpm add @temporal-contract/contract zod
|
|
|
13
13
|
## Quick Example
|
|
14
14
|
|
|
15
15
|
```typescript
|
|
16
|
-
import { defineContract } from
|
|
17
|
-
import { z } from
|
|
16
|
+
import { defineContract } from "@temporal-contract/contract";
|
|
17
|
+
import { z } from "zod";
|
|
18
18
|
|
|
19
19
|
export const myContract = defineContract({
|
|
20
|
-
taskQueue:
|
|
20
|
+
taskQueue: "orders",
|
|
21
21
|
workflows: {
|
|
22
22
|
processOrder: {
|
|
23
23
|
input: z.object({ orderId: z.string() }),
|
|
24
24
|
output: z.object({ success: z.boolean() }),
|
|
25
|
-
activities: {
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
activities: {
|
|
26
|
+
/* ... */
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
},
|
|
28
30
|
});
|
|
29
31
|
```
|
|
30
32
|
|
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
|
|
211
|
+
throw new Error(`Contract validation failed: ${cleanMessage}`);
|
|
24
212
|
}
|
|
25
213
|
return definition;
|
|
26
214
|
}
|
|
@@ -112,125 +300,9 @@ const contractValidationSchema = zod.z.object({
|
|
|
112
300
|
}
|
|
113
301
|
});
|
|
114
302
|
|
|
115
|
-
//#endregion
|
|
116
|
-
//#region src/nexus-types.ts
|
|
117
|
-
/**
|
|
118
|
-
* BUILDER FUNCTIONS (Proposed API)
|
|
119
|
-
* These would be added to builder.ts when Nexus support is implemented
|
|
120
|
-
*/
|
|
121
|
-
/**
|
|
122
|
-
* Builder for creating Nexus operation definitions
|
|
123
|
-
*
|
|
124
|
-
* @template TOperation - A NexusOperationDefinition containing input and output schemas
|
|
125
|
-
* @param definition - The Nexus operation definition with typed input/output schemas
|
|
126
|
-
* @returns The same definition with preserved types
|
|
127
|
-
*
|
|
128
|
-
* @example
|
|
129
|
-
* ```typescript
|
|
130
|
-
* const processPayment = defineNexusOperation({
|
|
131
|
-
* input: z.object({ amount: z.number(), customerId: z.string() }),
|
|
132
|
-
* output: z.object({ transactionId: z.string(), status: z.enum(['success', 'failed']) }),
|
|
133
|
-
* });
|
|
134
|
-
* ```
|
|
135
|
-
*/
|
|
136
|
-
function defineNexusOperation(definition) {
|
|
137
|
-
return definition;
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* Builder for creating Nexus service definitions
|
|
141
|
-
*
|
|
142
|
-
* @template TService - A NexusServiceDefinition containing a record of operations
|
|
143
|
-
* @param definition - The Nexus service definition with typed operations
|
|
144
|
-
* @returns The same definition with preserved types
|
|
145
|
-
*
|
|
146
|
-
* @example
|
|
147
|
-
* ```typescript
|
|
148
|
-
* const PaymentService = defineNexusService({
|
|
149
|
-
* operations: {
|
|
150
|
-
* processPayment: defineNexusOperation({ ... }),
|
|
151
|
-
* refundPayment: defineNexusOperation({ ... }),
|
|
152
|
-
* },
|
|
153
|
-
* });
|
|
154
|
-
* ```
|
|
155
|
-
*/
|
|
156
|
-
function defineNexusService(definition) {
|
|
157
|
-
return definition;
|
|
158
|
-
}
|
|
159
|
-
/**
|
|
160
|
-
* USAGE EXAMPLE
|
|
161
|
-
*
|
|
162
|
-
* This example demonstrates the complete type-safe Nexus workflow:
|
|
163
|
-
*
|
|
164
|
-
* ```typescript
|
|
165
|
-
* import { defineContract, defineNexusService, defineNexusOperation } from '@temporal-contract/contract';
|
|
166
|
-
* import { z } from 'zod';
|
|
167
|
-
*
|
|
168
|
-
* // 1. Define contract with Nexus service
|
|
169
|
-
* const paymentContract = defineContract({
|
|
170
|
-
* taskQueue: 'payments',
|
|
171
|
-
* workflows: { ... },
|
|
172
|
-
* nexusServices: {
|
|
173
|
-
* PaymentService: defineNexusService({
|
|
174
|
-
* operations: {
|
|
175
|
-
* processPayment: defineNexusOperation({
|
|
176
|
-
* input: z.object({
|
|
177
|
-
* amount: z.number().positive(),
|
|
178
|
-
* customerId: z.string().uuid(),
|
|
179
|
-
* }),
|
|
180
|
-
* output: z.object({
|
|
181
|
-
* transactionId: z.string(),
|
|
182
|
-
* status: z.enum(['success', 'failed']),
|
|
183
|
-
* }),
|
|
184
|
-
* }),
|
|
185
|
-
* },
|
|
186
|
-
* }),
|
|
187
|
-
* },
|
|
188
|
-
* });
|
|
189
|
-
*
|
|
190
|
-
* // 2. Worker implementation (type-safe handlers)
|
|
191
|
-
* import { createNexusHandlers } from '@temporal-contract/worker';
|
|
192
|
-
*
|
|
193
|
-
* const nexusHandlers = createNexusHandlers(paymentContract, {
|
|
194
|
-
* PaymentService: {
|
|
195
|
-
* processPayment: async ({ amount, customerId }) => {
|
|
196
|
-
* // ✅ Fully typed parameters
|
|
197
|
-
* // ✅ Input automatically validated
|
|
198
|
-
* const payment = await processPaymentInDatabase(customerId, amount);
|
|
199
|
-
* // ✅ Return value validated against schema
|
|
200
|
-
* return {
|
|
201
|
-
* transactionId: payment.id,
|
|
202
|
-
* status: 'success',
|
|
203
|
-
* };
|
|
204
|
-
* },
|
|
205
|
-
* },
|
|
206
|
-
* });
|
|
207
|
-
*
|
|
208
|
-
* // 3. Client usage (type-safe invocation)
|
|
209
|
-
* import { createNexusClient } from '@temporal-contract/client';
|
|
210
|
-
*
|
|
211
|
-
* const nexusClient = createNexusClient<typeof paymentContract>(connection, {
|
|
212
|
-
* namespace: 'payments-ns',
|
|
213
|
-
* });
|
|
214
|
-
*
|
|
215
|
-
* // ✅ Fully typed invocation
|
|
216
|
-
* const result = await nexusClient.invoke('PaymentService', 'processPayment', {
|
|
217
|
-
* amount: 100,
|
|
218
|
-
* customerId: 'cust-123',
|
|
219
|
-
* });
|
|
220
|
-
*
|
|
221
|
-
* // ❌ TypeScript errors caught at compile time
|
|
222
|
-
* await nexusClient.invoke('PaymentService', 'processPayment', {
|
|
223
|
-
* amount: -50, // Error: amount must be positive
|
|
224
|
-
* customerId: 'invalid', // Error: customerId must be UUID
|
|
225
|
-
* });
|
|
226
|
-
* ```
|
|
227
|
-
*/
|
|
228
|
-
|
|
229
303
|
//#endregion
|
|
230
304
|
exports.defineActivity = defineActivity;
|
|
231
305
|
exports.defineContract = defineContract;
|
|
232
|
-
exports.defineNexusOperation = defineNexusOperation;
|
|
233
|
-
exports.defineNexusService = defineNexusService;
|
|
234
306
|
exports.defineQuery = defineQuery;
|
|
235
307
|
exports.defineSignal = defineSignal;
|
|
236
308
|
exports.defineUpdate = defineUpdate;
|