@temporal-contract/worker 0.0.1
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/.turbo/turbo-build.log +25 -0
- package/LICENSE +21 -0
- package/README.md +128 -0
- package/dist/activity.cjs +8 -0
- package/dist/activity.d.cts +2 -0
- package/dist/activity.d.mts +2 -0
- package/dist/activity.mjs +3 -0
- package/dist/errors-CqX81ysy.d.cts +302 -0
- package/dist/errors-oc-Iiwmu.d.mts +302 -0
- package/dist/handler-B3KY0uDx.cjs +492 -0
- package/dist/handler-aA2RFdV7.mjs +409 -0
- package/dist/workflow.cjs +11 -0
- package/dist/workflow.d.cts +2 -0
- package/dist/workflow.d.mts +2 -0
- package/dist/workflow.mjs +3 -0
- package/package.json +72 -0
- package/src/activity.ts +15 -0
- package/src/errors.ts +164 -0
- package/src/handler.spec.ts +525 -0
- package/src/handler.ts +621 -0
- package/src/workflow.ts +20 -0
- package/tsconfig.json +9 -0
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import { ActivityOptions, WorkflowInfo } from "@temporalio/workflow";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { ActivityDefinition, ContractDefinition, QueryDefinition, SignalDefinition, UpdateDefinition, WorkerInferInput, WorkerInferOutput, WorkerInferWorkflowContextActivities } from "@temporal-contract/contract";
|
|
4
|
+
|
|
5
|
+
//#region src/handler.d.ts
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Workflow context with typed activities (workflow + global) and workflow info
|
|
9
|
+
* Note: activities is typed as 'any' to work around TypeScript generic type inference limitations with Zod tuples
|
|
10
|
+
*/
|
|
11
|
+
interface WorkflowContext<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"]> {
|
|
12
|
+
activities: WorkerInferWorkflowContextActivities<TContract, TWorkflowName>;
|
|
13
|
+
info: WorkflowInfo;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Workflow implementation function (receives context + typed args as tuple)
|
|
17
|
+
* Note: We use 'any' for args to work around TypeScript limitations with generic Zod tuple inference
|
|
18
|
+
* The actual type will be enforced at runtime by Zod validation
|
|
19
|
+
*/
|
|
20
|
+
type WorkflowImplementation<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"]> = (context: WorkflowContext<TContract, TWorkflowName>, args: WorkerInferInput<TContract["workflows"][TWorkflowName]>) => Promise<WorkerInferOutput<TContract["workflows"][TWorkflowName]>>;
|
|
21
|
+
/**
|
|
22
|
+
* Raw activity implementation function (receives typed args as tuple)
|
|
23
|
+
* Note: We use 'any' for args/return to work around TypeScript limitations with generic Zod tuple inference
|
|
24
|
+
* The actual types will be enforced at runtime by Zod validation
|
|
25
|
+
*/
|
|
26
|
+
type RawActivityImplementation<TActivity extends ActivityDefinition> = (args: WorkerInferInput<TActivity>) => Promise<WorkerInferOutput<TActivity>>;
|
|
27
|
+
/**
|
|
28
|
+
* Signal handler implementation
|
|
29
|
+
*/
|
|
30
|
+
type SignalHandlerImplementation<TSignal extends SignalDefinition> = (args: WorkerInferInput<TSignal>) => void | Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Query handler implementation
|
|
33
|
+
*/
|
|
34
|
+
type QueryHandlerImplementation<TQuery extends QueryDefinition> = (args: WorkerInferInput<TQuery>) => WorkerInferOutput<TQuery>;
|
|
35
|
+
/**
|
|
36
|
+
* Update handler implementation
|
|
37
|
+
*/
|
|
38
|
+
type UpdateHandlerImplementation<TUpdate extends UpdateDefinition> = (args: WorkerInferInput<TUpdate>) => Promise<WorkerInferOutput<TUpdate>>;
|
|
39
|
+
/**
|
|
40
|
+
* Map of all activity implementations for a contract (global + all workflow-specific)
|
|
41
|
+
*/
|
|
42
|
+
type ActivityImplementations<T extends ContractDefinition> = (T["activities"] extends Record<string, ActivityDefinition> ? { [K in keyof T["activities"]]: RawActivityImplementation<T["activities"][K]> } : {}) & UnionToIntersection<{ [K in keyof T["workflows"]]: T["workflows"][K]["activities"] extends Record<string, ActivityDefinition> ? { [A in keyof T["workflows"][K]["activities"]]: RawActivityImplementation<T["workflows"][K]["activities"][A]> } : {} }[keyof T["workflows"]]>;
|
|
43
|
+
/**
|
|
44
|
+
* Utility type to convert union to intersection
|
|
45
|
+
*/
|
|
46
|
+
type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
|
|
47
|
+
/**
|
|
48
|
+
* Options for creating activities handler
|
|
49
|
+
*/
|
|
50
|
+
interface DeclareActivitiesHandlerOptions<T extends ContractDefinition> {
|
|
51
|
+
contract: T;
|
|
52
|
+
activities: ActivityImplementations<T>;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Activities handler ready for Temporal Worker
|
|
56
|
+
*/
|
|
57
|
+
interface ActivitiesHandler<T extends ContractDefinition> {
|
|
58
|
+
contract: T;
|
|
59
|
+
activities: Record<string, (...args: unknown[]) => Promise<unknown>>;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Options for declaring a workflow implementation
|
|
63
|
+
*/
|
|
64
|
+
interface DeclareWorkflowOptions<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"]> {
|
|
65
|
+
workflowName: TWorkflowName;
|
|
66
|
+
contract: TContract;
|
|
67
|
+
implementation: WorkflowImplementation<TContract, TWorkflowName>;
|
|
68
|
+
/**
|
|
69
|
+
* Default activity options applied to all activities in this workflow.
|
|
70
|
+
* These will be merged with the default startToCloseTimeout of 60 seconds.
|
|
71
|
+
* For more control, you can override specific Temporal ActivityOptions like:
|
|
72
|
+
* - startToCloseTimeout: Maximum time for activity execution
|
|
73
|
+
* - scheduleToCloseTimeout: End-to-end timeout including queuing
|
|
74
|
+
* - scheduleToStartTimeout: Maximum time activity can wait in queue
|
|
75
|
+
* - heartbeatTimeout: Time between heartbeats before considering activity dead
|
|
76
|
+
* - retry: Retry policy for failed activities
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```ts
|
|
80
|
+
* activityOptions: {
|
|
81
|
+
* startToCloseTimeout: '5m',
|
|
82
|
+
* retry: { maximumAttempts: 3 }
|
|
83
|
+
* }
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
activityOptions?: ActivityOptions;
|
|
87
|
+
/**
|
|
88
|
+
* Signal handlers (if defined in workflow)
|
|
89
|
+
*/
|
|
90
|
+
signals?: TContract["workflows"][TWorkflowName]["signals"] extends Record<string, SignalDefinition> ? { [K in keyof TContract["workflows"][TWorkflowName]["signals"]]: SignalHandlerImplementation<TContract["workflows"][TWorkflowName]["signals"][K]> } : never;
|
|
91
|
+
/**
|
|
92
|
+
* Query handlers (if defined in workflow)
|
|
93
|
+
*/
|
|
94
|
+
queries?: TContract["workflows"][TWorkflowName]["queries"] extends Record<string, QueryDefinition> ? { [K in keyof TContract["workflows"][TWorkflowName]["queries"]]: QueryHandlerImplementation<TContract["workflows"][TWorkflowName]["queries"][K]> } : never;
|
|
95
|
+
/**
|
|
96
|
+
* Update handlers (if defined in workflow)
|
|
97
|
+
*/
|
|
98
|
+
updates?: TContract["workflows"][TWorkflowName]["updates"] extends Record<string, UpdateDefinition> ? { [K in keyof TContract["workflows"][TWorkflowName]["updates"]]: UpdateHandlerImplementation<TContract["workflows"][TWorkflowName]["updates"][K]> } : never;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Create a typed activities handler with automatic validation
|
|
102
|
+
*
|
|
103
|
+
* This wraps all activity implementations with Zod validation at network boundaries.
|
|
104
|
+
* TypeScript ensures ALL activities (global + workflow-specific) are implemented.
|
|
105
|
+
*
|
|
106
|
+
* Use this to create the activities object for the Temporal Worker.
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* ```ts
|
|
110
|
+
* import { declareActivitiesHandler } from '@temporal-contract/worker';
|
|
111
|
+
* import myContract from './contract';
|
|
112
|
+
*
|
|
113
|
+
* export const activitiesHandler = declareActivitiesHandler({
|
|
114
|
+
* contract: myContract,
|
|
115
|
+
* activities: {
|
|
116
|
+
* // Global activities
|
|
117
|
+
* sendEmail: async (to, subject, body) => {
|
|
118
|
+
* await emailService.send({ to, subject, body });
|
|
119
|
+
* return { sent: true };
|
|
120
|
+
* },
|
|
121
|
+
* // Workflow-specific activities
|
|
122
|
+
* validateInventory: async (orderId) => {
|
|
123
|
+
* const available = await inventory.check(orderId);
|
|
124
|
+
* return { available };
|
|
125
|
+
* },
|
|
126
|
+
* },
|
|
127
|
+
* });
|
|
128
|
+
*
|
|
129
|
+
* // Use with Temporal Worker
|
|
130
|
+
* import { Worker } from '@temporalio/worker';
|
|
131
|
+
*
|
|
132
|
+
* const worker = await Worker.create({
|
|
133
|
+
* workflowsPath: require.resolve('./workflows'),
|
|
134
|
+
* activities: activitiesHandler.activities,
|
|
135
|
+
* taskQueue: activitiesHandler.contract.taskQueue,
|
|
136
|
+
* });
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
declare function declareActivitiesHandler<T extends ContractDefinition>(options: DeclareActivitiesHandlerOptions<T>): ActivitiesHandler<T>;
|
|
140
|
+
/**
|
|
141
|
+
* Create a typed workflow implementation with automatic validation
|
|
142
|
+
*
|
|
143
|
+
* This wraps a workflow implementation with:
|
|
144
|
+
* - Input/output validation
|
|
145
|
+
* - Typed workflow context with activities
|
|
146
|
+
* - Workflow info access
|
|
147
|
+
*
|
|
148
|
+
* Workflows must be defined in separate files and imported by the Temporal Worker
|
|
149
|
+
* via workflowsPath.
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* ```ts
|
|
153
|
+
* // workflows/processOrder.ts
|
|
154
|
+
* import { declareWorkflow } from '@temporal-contract/worker';
|
|
155
|
+
* import myContract from '../contract';
|
|
156
|
+
*
|
|
157
|
+
* export const processOrder = declareWorkflow({
|
|
158
|
+
* workflowName: 'processOrder',
|
|
159
|
+
* contract: myContract,
|
|
160
|
+
* implementation: async (context, orderId, customerId) => {
|
|
161
|
+
* // context.activities: typed activities (workflow + global)
|
|
162
|
+
* // context.info: WorkflowInfo
|
|
163
|
+
*
|
|
164
|
+
* const inventory = await context.activities.validateInventory(orderId);
|
|
165
|
+
*
|
|
166
|
+
* if (!inventory.available) {
|
|
167
|
+
* throw new Error('Out of stock');
|
|
168
|
+
* }
|
|
169
|
+
*
|
|
170
|
+
* const payment = await context.activities.chargePayment(customerId, 100);
|
|
171
|
+
*
|
|
172
|
+
* // Global activity
|
|
173
|
+
* await context.activities.sendEmail(
|
|
174
|
+
* customerId,
|
|
175
|
+
* 'Order processed',
|
|
176
|
+
* 'Your order has been processed'
|
|
177
|
+
* );
|
|
178
|
+
*
|
|
179
|
+
* return {
|
|
180
|
+
* orderId,
|
|
181
|
+
* status: payment.success ? 'success' : 'failed',
|
|
182
|
+
* transactionId: payment.transactionId,
|
|
183
|
+
* };
|
|
184
|
+
* },
|
|
185
|
+
* activityOptions: {
|
|
186
|
+
* startToCloseTimeout: '1 minute',
|
|
187
|
+
* },
|
|
188
|
+
* });
|
|
189
|
+
* ```
|
|
190
|
+
*
|
|
191
|
+
* Then in your worker setup:
|
|
192
|
+
* ```ts
|
|
193
|
+
* // worker.ts
|
|
194
|
+
* import { Worker } from '@temporalio/worker';
|
|
195
|
+
* import { activitiesHandler } from './activities';
|
|
196
|
+
*
|
|
197
|
+
* const worker = await Worker.create({
|
|
198
|
+
* workflowsPath: require.resolve('./workflows'), // Imports processOrder
|
|
199
|
+
* activities: activitiesHandler.activities,
|
|
200
|
+
* taskQueue: activitiesHandler.contract.taskQueue,
|
|
201
|
+
* });
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
declare function declareWorkflow<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"]>(options: DeclareWorkflowOptions<TContract, TWorkflowName>): (args: WorkerInferInput<TContract["workflows"][TWorkflowName]>) => Promise<WorkerInferOutput<TContract["workflows"][TWorkflowName]>>;
|
|
205
|
+
//#endregion
|
|
206
|
+
//#region src/errors.d.ts
|
|
207
|
+
/**
|
|
208
|
+
* Base error class for worker errors
|
|
209
|
+
*/
|
|
210
|
+
declare class WorkerError extends Error {
|
|
211
|
+
constructor(message: string);
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Error thrown when an activity implementation is not found
|
|
215
|
+
*/
|
|
216
|
+
declare class ActivityImplementationNotFoundError extends WorkerError {
|
|
217
|
+
readonly activityName: string;
|
|
218
|
+
readonly availableActivities: readonly string[];
|
|
219
|
+
constructor(activityName: string, availableActivities: readonly string[]);
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Error thrown when an activity definition is not found in the contract
|
|
223
|
+
*/
|
|
224
|
+
declare class ActivityDefinitionNotFoundError extends WorkerError {
|
|
225
|
+
readonly activityName: string;
|
|
226
|
+
readonly availableDefinitions: readonly string[];
|
|
227
|
+
constructor(activityName: string, availableDefinitions: readonly string[]);
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Error thrown when activity input validation fails
|
|
231
|
+
*/
|
|
232
|
+
declare class ActivityInputValidationError extends WorkerError {
|
|
233
|
+
readonly activityName: string;
|
|
234
|
+
readonly zodError: z.ZodError;
|
|
235
|
+
constructor(activityName: string, zodError: z.ZodError);
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Error thrown when activity output validation fails
|
|
239
|
+
*/
|
|
240
|
+
declare class ActivityOutputValidationError extends WorkerError {
|
|
241
|
+
readonly activityName: string;
|
|
242
|
+
readonly zodError: z.ZodError;
|
|
243
|
+
constructor(activityName: string, zodError: z.ZodError);
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Error thrown when workflow input validation fails
|
|
247
|
+
*/
|
|
248
|
+
declare class WorkflowInputValidationError extends WorkerError {
|
|
249
|
+
readonly workflowName: string;
|
|
250
|
+
readonly zodError: z.ZodError;
|
|
251
|
+
constructor(workflowName: string, zodError: z.ZodError);
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Error thrown when workflow output validation fails
|
|
255
|
+
*/
|
|
256
|
+
declare class WorkflowOutputValidationError extends WorkerError {
|
|
257
|
+
readonly workflowName: string;
|
|
258
|
+
readonly zodError: z.ZodError;
|
|
259
|
+
constructor(workflowName: string, zodError: z.ZodError);
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Error thrown when signal input validation fails
|
|
263
|
+
*/
|
|
264
|
+
declare class SignalInputValidationError extends WorkerError {
|
|
265
|
+
readonly signalName: string;
|
|
266
|
+
readonly zodError: z.ZodError;
|
|
267
|
+
constructor(signalName: string, zodError: z.ZodError);
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Error thrown when query input validation fails
|
|
271
|
+
*/
|
|
272
|
+
declare class QueryInputValidationError extends WorkerError {
|
|
273
|
+
readonly queryName: string;
|
|
274
|
+
readonly zodError: z.ZodError;
|
|
275
|
+
constructor(queryName: string, zodError: z.ZodError);
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Error thrown when query output validation fails
|
|
279
|
+
*/
|
|
280
|
+
declare class QueryOutputValidationError extends WorkerError {
|
|
281
|
+
readonly queryName: string;
|
|
282
|
+
readonly zodError: z.ZodError;
|
|
283
|
+
constructor(queryName: string, zodError: z.ZodError);
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Error thrown when update input validation fails
|
|
287
|
+
*/
|
|
288
|
+
declare class UpdateInputValidationError extends WorkerError {
|
|
289
|
+
readonly updateName: string;
|
|
290
|
+
readonly zodError: z.ZodError;
|
|
291
|
+
constructor(updateName: string, zodError: z.ZodError);
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Error thrown when update output validation fails
|
|
295
|
+
*/
|
|
296
|
+
declare class UpdateOutputValidationError extends WorkerError {
|
|
297
|
+
readonly updateName: string;
|
|
298
|
+
readonly zodError: z.ZodError;
|
|
299
|
+
constructor(updateName: string, zodError: z.ZodError);
|
|
300
|
+
}
|
|
301
|
+
//#endregion
|
|
302
|
+
export { declareActivitiesHandler as C, WorkflowImplementation as S, QueryHandlerImplementation as _, QueryInputValidationError as a, UpdateHandlerImplementation as b, UpdateInputValidationError as c, WorkflowInputValidationError as d, WorkflowOutputValidationError as f, DeclareWorkflowOptions as g, DeclareActivitiesHandlerOptions as h, ActivityOutputValidationError as i, UpdateOutputValidationError as l, ActivityImplementations as m, ActivityImplementationNotFoundError as n, QueryOutputValidationError as o, ActivitiesHandler as p, ActivityInputValidationError as r, SignalInputValidationError as s, ActivityDefinitionNotFoundError as t, WorkerError as u, RawActivityImplementation as v, declareWorkflow as w, WorkflowContext as x, SignalHandlerImplementation as y };
|