@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,25 @@
|
|
|
1
|
+
|
|
2
|
+
> @temporal-contract/worker@0.0.1 build /home/runner/work/temporal-contract/temporal-contract/packages/worker
|
|
3
|
+
> tsdown src/activity.ts src/workflow.ts --format cjs,esm --dts --clean
|
|
4
|
+
|
|
5
|
+
[34mℹ[39m tsdown [2mv0.17.2[22m powered by rolldown [2mv1.0.0-beta.53[22m
|
|
6
|
+
[34mℹ[39m entry: [34msrc/activity.ts, src/workflow.ts[39m
|
|
7
|
+
[34mℹ[39m tsconfig: [34mtsconfig.json[39m
|
|
8
|
+
[34mℹ[39m Build start
|
|
9
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[1mworkflow.cjs[22m [2m 0.75 kB[22m [2m│ gzip: 0.19 kB[22m
|
|
10
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[1mactivity.cjs[22m [2m 0.55 kB[22m [2m│ gzip: 0.19 kB[22m
|
|
11
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22mhandler-B3KY0uDx.cjs [2m16.12 kB[22m [2m│ gzip: 2.98 kB[22m
|
|
12
|
+
[34mℹ[39m [33m[CJS][39m 3 files, total: 17.41 kB
|
|
13
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[32m[1mworkflow.d.cts[22m[39m [2m 0.92 kB[22m [2m│ gzip: 0.27 kB[22m
|
|
14
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[32m[1mactivity.d.cts[22m[39m [2m 0.67 kB[22m [2m│ gzip: 0.23 kB[22m
|
|
15
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[32merrors-CqX81ysy.d.cts[39m [2m12.73 kB[22m [2m│ gzip: 2.77 kB[22m
|
|
16
|
+
[34mℹ[39m [33m[CJS][39m 3 files, total: 14.32 kB
|
|
17
|
+
[32m✔[39m Build complete in [32m3501ms[39m
|
|
18
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[1mworkflow.mjs[22m [2m 0.56 kB[22m [2m│ gzip: 0.20 kB[22m
|
|
19
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[1mactivity.mjs[22m [2m 0.42 kB[22m [2m│ gzip: 0.18 kB[22m
|
|
20
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22mhandler-aA2RFdV7.mjs [2m14.29 kB[22m [2m│ gzip: 2.90 kB[22m
|
|
21
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[32m[1mworkflow.d.mts[22m[39m [2m 0.92 kB[22m [2m│ gzip: 0.27 kB[22m
|
|
22
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[32m[1mactivity.d.mts[22m[39m [2m 0.67 kB[22m [2m│ gzip: 0.23 kB[22m
|
|
23
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[32merrors-oc-Iiwmu.d.mts[39m [2m12.73 kB[22m [2m│ gzip: 2.77 kB[22m
|
|
24
|
+
[34mℹ[39m [34m[ESM][39m 6 files, total: 29.59 kB
|
|
25
|
+
[32m✔[39m Build complete in [32m3504ms[39m
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Benoit TRAVERS
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# @temporal-contract/worker
|
|
2
|
+
|
|
3
|
+
> Type-safe worker implementation for Temporal
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @temporal-contract/worker @temporal-contract/contract @temporalio/workflow zod
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Important: Separate Entry Points
|
|
12
|
+
|
|
13
|
+
Use **separate imports** for better tree-shaking:
|
|
14
|
+
|
|
15
|
+
- **`@temporal-contract/worker/activity`** — For activity files
|
|
16
|
+
- **`@temporal-contract/worker/workflow`** — For workflow files
|
|
17
|
+
|
|
18
|
+
## Quick Start
|
|
19
|
+
|
|
20
|
+
### 1. Implement Activities
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
// activities.ts
|
|
24
|
+
import { declareActivitiesHandler } from '@temporal-contract/worker/activity';
|
|
25
|
+
import { myContract } from './contract';
|
|
26
|
+
|
|
27
|
+
export const activities = declareActivitiesHandler({
|
|
28
|
+
contract: myContract,
|
|
29
|
+
activities: {
|
|
30
|
+
sendEmail: async ({ to, subject, body }) => {
|
|
31
|
+
await emailService.send({ to, subject, body });
|
|
32
|
+
return { sent: true };
|
|
33
|
+
},
|
|
34
|
+
processPayment: async ({ customerId, amount }) => {
|
|
35
|
+
const txId = await paymentGateway.charge(customerId, amount);
|
|
36
|
+
return { transactionId: txId, success: true };
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### 2. Implement Workflows
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
// workflows.ts
|
|
46
|
+
import { declareWorkflow } from '@temporal-contract/worker/workflow';
|
|
47
|
+
import { myContract } from './contract';
|
|
48
|
+
|
|
49
|
+
export const processOrder = declareWorkflow({
|
|
50
|
+
workflowName: 'processOrder',
|
|
51
|
+
contract: myContract,
|
|
52
|
+
implementation: async (context, { orderId, customerId }) => {
|
|
53
|
+
const payment = await context.activities.processPayment({ customerId, amount: 100 });
|
|
54
|
+
await context.activities.sendEmail({ to: customerId, subject: 'Confirmed', body: 'Done!' });
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
status: payment.success ? 'success' : 'failed',
|
|
58
|
+
transactionId: payment.transactionId,
|
|
59
|
+
};
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### 3. Start Worker
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
// worker.ts
|
|
68
|
+
import { Worker } from '@temporalio/worker';
|
|
69
|
+
import { activities } from './activities';
|
|
70
|
+
|
|
71
|
+
const worker = await Worker.create({
|
|
72
|
+
workflowsPath: require.resolve('./workflows'),
|
|
73
|
+
activities: activities.activities,
|
|
74
|
+
taskQueue: activities.contract.taskQueue,
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
await worker.run();
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Features
|
|
81
|
+
|
|
82
|
+
✅ **Automatic validation** — Input/output validated with Zod at network boundaries
|
|
83
|
+
✅ **Type inference** — Full TypeScript inference from contract
|
|
84
|
+
✅ **Typed context** — Activities and workflow info fully typed
|
|
85
|
+
✅ **Signals, queries, updates** — Type-safe handlers with validation
|
|
86
|
+
|
|
87
|
+
## Utility Types
|
|
88
|
+
|
|
89
|
+
For cleaner activity signatures:
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
import type { ActivityHandler, WorkflowActivityHandler } from '@temporal-contract/contract';
|
|
93
|
+
|
|
94
|
+
const sendEmail: ActivityHandler<typeof myContract, 'sendEmail'> =
|
|
95
|
+
async ({ to, subject, body }) => {
|
|
96
|
+
// Fully typed without explicit annotations
|
|
97
|
+
return { sent: true };
|
|
98
|
+
};
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
See [Activity Handlers documentation](../../docs/ACTIVITY_HANDLERS.md) for details.
|
|
102
|
+
|
|
103
|
+
## Error Handling
|
|
104
|
+
|
|
105
|
+
Custom error classes with contextual info:
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
import { ActivityDefinitionNotFoundError } from '@temporal-contract/worker';
|
|
109
|
+
|
|
110
|
+
// Errors include helpful context
|
|
111
|
+
if (error instanceof ActivityDefinitionNotFoundError) {
|
|
112
|
+
console.error('Not found:', error.activityName);
|
|
113
|
+
console.error('Available:', error.availableActivities);
|
|
114
|
+
}
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
---
|
|
118
|
+
|
|
119
|
+
## Learn More
|
|
120
|
+
|
|
121
|
+
- [Main README](../../README.md) — Quick start guide
|
|
122
|
+
- [Worker Implementation](../../docs/CONTRACT_HANDLER.md) — Complete guide
|
|
123
|
+
- [Entry Points](../../docs/ENTRY_POINTS.md) — Why separate entry points
|
|
124
|
+
- [Activity Handlers](../../docs/ACTIVITY_HANDLERS.md) — Type utilities
|
|
125
|
+
|
|
126
|
+
## License
|
|
127
|
+
|
|
128
|
+
MIT
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
const require_handler = require('./handler-B3KY0uDx.cjs');
|
|
2
|
+
|
|
3
|
+
exports.ActivityDefinitionNotFoundError = require_handler.ActivityDefinitionNotFoundError;
|
|
4
|
+
exports.ActivityImplementationNotFoundError = require_handler.ActivityImplementationNotFoundError;
|
|
5
|
+
exports.ActivityInputValidationError = require_handler.ActivityInputValidationError;
|
|
6
|
+
exports.ActivityOutputValidationError = require_handler.ActivityOutputValidationError;
|
|
7
|
+
exports.WorkerError = require_handler.WorkerError;
|
|
8
|
+
exports.declareActivitiesHandler = require_handler.declareActivitiesHandler;
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { C as declareActivitiesHandler, h as DeclareActivitiesHandlerOptions, i as ActivityOutputValidationError, m as ActivityImplementations, n as ActivityImplementationNotFoundError, p as ActivitiesHandler, r as ActivityInputValidationError, t as ActivityDefinitionNotFoundError, u as WorkerError, v as RawActivityImplementation } from "./errors-CqX81ysy.cjs";
|
|
2
|
+
export { type ActivitiesHandler, ActivityDefinitionNotFoundError, ActivityImplementationNotFoundError, type ActivityImplementations, ActivityInputValidationError, ActivityOutputValidationError, type DeclareActivitiesHandlerOptions, type RawActivityImplementation, WorkerError, declareActivitiesHandler };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { C as declareActivitiesHandler, h as DeclareActivitiesHandlerOptions, i as ActivityOutputValidationError, m as ActivityImplementations, n as ActivityImplementationNotFoundError, p as ActivitiesHandler, r as ActivityInputValidationError, t as ActivityDefinitionNotFoundError, u as WorkerError, v as RawActivityImplementation } from "./errors-oc-Iiwmu.mjs";
|
|
2
|
+
export { type ActivitiesHandler, ActivityDefinitionNotFoundError, ActivityImplementationNotFoundError, type ActivityImplementations, ActivityInputValidationError, ActivityOutputValidationError, type DeclareActivitiesHandlerOptions, type RawActivityImplementation, WorkerError, declareActivitiesHandler };
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { a as ActivityInputValidationError, f as WorkerError, i as ActivityImplementationNotFoundError, o as ActivityOutputValidationError, r as ActivityDefinitionNotFoundError, t as declareActivitiesHandler } from "./handler-aA2RFdV7.mjs";
|
|
2
|
+
|
|
3
|
+
export { ActivityDefinitionNotFoundError, ActivityImplementationNotFoundError, ActivityInputValidationError, ActivityOutputValidationError, WorkerError, declareActivitiesHandler };
|
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import { ActivityOptions, WorkflowInfo } from "@temporalio/workflow";
|
|
2
|
+
import { ActivityDefinition, ContractDefinition, QueryDefinition, SignalDefinition, UpdateDefinition, WorkerInferInput, WorkerInferOutput, WorkerInferWorkflowContextActivities } from "@temporal-contract/contract";
|
|
3
|
+
import { z } from "zod";
|
|
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 };
|