@temporal-contract/worker 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 +46 -35
- package/dist/activity-5pVNjW7l.d.cts +161 -0
- package/dist/activity-BUEBZ7SL.d.mts +161 -0
- package/dist/activity.cjs +84 -2
- package/dist/activity.d.cts +3 -98
- package/dist/activity.d.mts +3 -98
- package/dist/activity.mjs +80 -2
- package/dist/{errors-BqYWpdvd.d.cts → errors-CXpHOFmk.d.cts} +0 -1
- package/dist/{errors-C1RFkCuD.d.mts → errors-Vr-sKdW7.d.mts} +0 -1
- package/dist/worker.d.cts +1 -2
- package/dist/worker.d.mts +1 -2
- package/dist/workflow.d.cts +4 -5
- package/dist/workflow.d.mts +4 -5
- package/package.json +17 -14
package/README.md
CHANGED
|
@@ -14,43 +14,50 @@ pnpm add @temporal-contract/worker @temporal-contract/contract @temporalio/workf
|
|
|
14
14
|
|
|
15
15
|
```typescript
|
|
16
16
|
// activities.ts
|
|
17
|
-
import { declareActivitiesHandler } from
|
|
17
|
+
import { declareActivitiesHandler, ActivityError } from "@temporal-contract/worker/activity";
|
|
18
|
+
import { Future, Result } from "@swan-io/boxed";
|
|
18
19
|
|
|
19
20
|
export const activities = declareActivitiesHandler({
|
|
20
21
|
contract: myContract,
|
|
21
22
|
activities: {
|
|
22
|
-
sendEmail:
|
|
23
|
-
|
|
23
|
+
sendEmail: ({ to, body }) => {
|
|
24
|
+
return Future.fromPromise(emailService.send({ to, body }))
|
|
25
|
+
.mapError(
|
|
26
|
+
(error) =>
|
|
27
|
+
new ActivityError(
|
|
28
|
+
"EMAIL_FAILED",
|
|
29
|
+
error instanceof Error ? error.message : "Failed to send email",
|
|
30
|
+
error,
|
|
31
|
+
),
|
|
32
|
+
)
|
|
33
|
+
.mapOk(() => ({ sent: true }));
|
|
34
|
+
},
|
|
35
|
+
},
|
|
24
36
|
});
|
|
25
37
|
|
|
26
38
|
// workflows.ts
|
|
27
|
-
import { declareWorkflow } from
|
|
39
|
+
import { declareWorkflow } from "@temporal-contract/worker/workflow";
|
|
28
40
|
|
|
29
41
|
export const processOrder = declareWorkflow({
|
|
30
|
-
workflowName:
|
|
42
|
+
workflowName: "processOrder",
|
|
31
43
|
contract: myContract,
|
|
32
|
-
implementation: async (
|
|
33
|
-
|
|
44
|
+
implementation: async ({ activities }, input) => {
|
|
45
|
+
// Activities return plain values (Result is unwrapped internally)
|
|
46
|
+
await activities.sendEmail({ to: "user@example.com", body: "Done!" });
|
|
34
47
|
return { success: true };
|
|
35
|
-
}
|
|
48
|
+
},
|
|
36
49
|
});
|
|
37
50
|
|
|
38
51
|
// worker.ts
|
|
39
|
-
import {
|
|
40
|
-
import {
|
|
41
|
-
import
|
|
42
|
-
import myContract from './contract';
|
|
52
|
+
import { Worker } from "@temporalio/worker";
|
|
53
|
+
import { activities } from "./activities";
|
|
54
|
+
import myContract from "./contract";
|
|
43
55
|
|
|
44
56
|
async function run() {
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
const worker = await createWorker({
|
|
50
|
-
contract: myContract,
|
|
51
|
-
connection,
|
|
52
|
-
workflowsPath: require.resolve('./workflows'),
|
|
57
|
+
const worker = await Worker.create({
|
|
58
|
+
workflowsPath: require.resolve("./workflows"),
|
|
53
59
|
activities,
|
|
60
|
+
taskQueue: myContract.taskQueue,
|
|
54
61
|
});
|
|
55
62
|
|
|
56
63
|
await worker.run();
|
|
@@ -65,33 +72,37 @@ Execute child workflows with type-safe Future/Result pattern. Supports both same
|
|
|
65
72
|
|
|
66
73
|
```typescript
|
|
67
74
|
// workflows.ts
|
|
68
|
-
import { declareWorkflow } from
|
|
75
|
+
import { declareWorkflow } from "@temporal-contract/worker/workflow";
|
|
69
76
|
|
|
70
77
|
export const parentWorkflow = declareWorkflow({
|
|
71
|
-
workflowName:
|
|
78
|
+
workflowName: "parentWorkflow",
|
|
72
79
|
contract: myContract,
|
|
73
|
-
implementation: async (
|
|
80
|
+
implementation: async ({ executeChildWorkflow }, input) => {
|
|
74
81
|
// Execute child workflow from same contract and wait for result
|
|
75
|
-
const childResult = await
|
|
82
|
+
const childResult = await executeChildWorkflow(myContract, "processPayment", {
|
|
76
83
|
workflowId: `payment-${input.orderId}`,
|
|
77
|
-
args: { amount: input.totalAmount }
|
|
84
|
+
args: { amount: input.totalAmount },
|
|
78
85
|
});
|
|
79
86
|
|
|
80
87
|
childResult.match({
|
|
81
|
-
Ok: (output) => console.log(
|
|
82
|
-
Error: (error) => console.error(
|
|
88
|
+
Ok: (output) => console.log("Payment processed:", output),
|
|
89
|
+
Error: (error) => console.error("Payment failed:", error),
|
|
83
90
|
});
|
|
84
91
|
|
|
85
92
|
// Execute child workflow from another contract (another worker)
|
|
86
|
-
const notificationResult = await
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
93
|
+
const notificationResult = await executeChildWorkflow(
|
|
94
|
+
notificationContract,
|
|
95
|
+
"sendNotification",
|
|
96
|
+
{
|
|
97
|
+
workflowId: `notification-${input.orderId}`,
|
|
98
|
+
args: { message: "Order received" },
|
|
99
|
+
},
|
|
100
|
+
);
|
|
90
101
|
|
|
91
102
|
// Or start child workflow without waiting
|
|
92
|
-
const handleResult = await
|
|
103
|
+
const handleResult = await startChildWorkflow(myContract, "sendEmail", {
|
|
93
104
|
workflowId: `email-${input.orderId}`,
|
|
94
|
-
args: { to:
|
|
105
|
+
args: { to: "user@example.com", body: "Order received" },
|
|
95
106
|
});
|
|
96
107
|
|
|
97
108
|
handleResult.match({
|
|
@@ -100,11 +111,11 @@ export const parentWorkflow = declareWorkflow({
|
|
|
100
111
|
const result = await handle.result();
|
|
101
112
|
// ...
|
|
102
113
|
},
|
|
103
|
-
Error: (error) => console.error(
|
|
114
|
+
Error: (error) => console.error("Failed to start:", error),
|
|
104
115
|
});
|
|
105
116
|
|
|
106
117
|
return { success: true };
|
|
107
|
-
}
|
|
118
|
+
},
|
|
108
119
|
});
|
|
109
120
|
```
|
|
110
121
|
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { g as WorkerInferOutput, h as WorkerInferInput } from "./errors-CXpHOFmk.cjs";
|
|
2
|
+
import { ActivityDefinition, ContractDefinition } from "@temporal-contract/contract";
|
|
3
|
+
import { Future, Result } from "@swan-io/boxed";
|
|
4
|
+
|
|
5
|
+
//#region src/activity-utils.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* Extract activity definitions for a specific workflow from a contract
|
|
8
|
+
*
|
|
9
|
+
* This includes both:
|
|
10
|
+
* - Workflow-specific activities defined under workflow.activities
|
|
11
|
+
* - Global activities defined under contract.activities
|
|
12
|
+
*
|
|
13
|
+
* @param contract - The contract definition
|
|
14
|
+
* @param workflowName - The name of the workflow
|
|
15
|
+
* @returns Activity definitions for the workflow (workflow-specific + global activities merged)
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* const orderWorkflowActivities = getWorkflowActivities(myContract, 'processOrder');
|
|
20
|
+
* // Returns: { processPayment: ActivityDef, reserveInventory: ActivityDef, sendEmail: ActivityDef }
|
|
21
|
+
* // where sendEmail is a global activity
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
declare function getWorkflowActivities<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"]>(contract: TContract, workflowName: TWorkflowName): Record<string, ActivityDefinition>;
|
|
25
|
+
/**
|
|
26
|
+
* Extract all activity names for a specific workflow from a contract
|
|
27
|
+
*
|
|
28
|
+
* @param contract - The contract definition
|
|
29
|
+
* @param workflowName - The name of the workflow
|
|
30
|
+
* @returns Array of activity names (strings) available for the workflow
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* const activityNames = getWorkflowActivityNames(myContract, 'processOrder');
|
|
35
|
+
* // Returns: ['processPayment', 'reserveInventory', 'sendEmail']
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
declare function getWorkflowActivityNames<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"]>(contract: TContract, workflowName: TWorkflowName): string[];
|
|
39
|
+
/**
|
|
40
|
+
* Check if an activity belongs to a specific workflow
|
|
41
|
+
*
|
|
42
|
+
* @param contract - The contract definition
|
|
43
|
+
* @param workflowName - The name of the workflow
|
|
44
|
+
* @param activityName - The name of the activity to check
|
|
45
|
+
* @returns True if the activity is available for the workflow, false otherwise
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* if (isWorkflowActivity(myContract, 'processOrder', 'processPayment')) {
|
|
50
|
+
* // Activity is available for this workflow
|
|
51
|
+
* }
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
declare function isWorkflowActivity<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"]>(contract: TContract, workflowName: TWorkflowName, activityName: string): boolean;
|
|
55
|
+
/**
|
|
56
|
+
* Get all workflow names from a contract
|
|
57
|
+
*
|
|
58
|
+
* @param contract - The contract definition
|
|
59
|
+
* @returns Array of workflow names defined in the contract
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* const workflows = getWorkflowNames(myContract);
|
|
64
|
+
* // Returns: ['processOrder', 'processRefund']
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
declare function getWorkflowNames<TContract extends ContractDefinition>(contract: TContract): Array<keyof TContract["workflows"]>;
|
|
68
|
+
//#endregion
|
|
69
|
+
//#region src/activity.d.ts
|
|
70
|
+
/**
|
|
71
|
+
* Activity error class that should be used to wrap all technical exceptions
|
|
72
|
+
* Forces proper error handling and enables retry policies
|
|
73
|
+
*/
|
|
74
|
+
declare class ActivityError extends Error {
|
|
75
|
+
readonly code: string;
|
|
76
|
+
constructor(code: string, message: string, cause?: unknown);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Activity implementation using Future/Result pattern
|
|
80
|
+
*
|
|
81
|
+
* Returns Future<Result<Output, ActivityError>> for explicit error handling instead of throwing exceptions.
|
|
82
|
+
* All errors must be wrapped in ActivityError to enable proper retry policies.
|
|
83
|
+
*/
|
|
84
|
+
type BoxedActivityImplementation<TActivity extends ActivityDefinition> = (args: WorkerInferInput<TActivity>) => Future<Result<WorkerInferOutput<TActivity>, ActivityError>>;
|
|
85
|
+
/**
|
|
86
|
+
* Map of all activity implementations for a contract (global + all workflow-specific)
|
|
87
|
+
*/
|
|
88
|
+
type ContractBoxedActivitiesImplementations<TContract extends ContractDefinition> = (TContract["activities"] extends Record<string, ActivityDefinition> ? BoxedActivitiesImplementations<TContract["activities"]> : {}) & { [TWorkflow in keyof TContract["workflows"]]: TContract["workflows"][TWorkflow]["activities"] extends Record<string, ActivityDefinition> ? BoxedActivitiesImplementations<TContract["workflows"][TWorkflow]["activities"]> : {} };
|
|
89
|
+
type BoxedActivitiesImplementations<TActivities extends Record<string, ActivityDefinition>> = { [K in keyof TActivities]: BoxedActivityImplementation<TActivities[K]> };
|
|
90
|
+
/**
|
|
91
|
+
* Options for creating activities handler
|
|
92
|
+
*/
|
|
93
|
+
interface DeclareActivitiesHandlerOptions<TContract extends ContractDefinition> {
|
|
94
|
+
contract: TContract;
|
|
95
|
+
activities: ContractBoxedActivitiesImplementations<TContract>;
|
|
96
|
+
}
|
|
97
|
+
type ActivityImplementation<TActivity extends ActivityDefinition> = (args: WorkerInferInput<TActivity>) => Promise<WorkerInferOutput<TActivity>>;
|
|
98
|
+
type ActivitiesImplementations<TActivities extends Record<string, ActivityDefinition>> = { [K in keyof TActivities]: ActivityImplementation<TActivities[K]> };
|
|
99
|
+
type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
|
|
100
|
+
/**
|
|
101
|
+
* Activities handler ready for Temporal Worker
|
|
102
|
+
*
|
|
103
|
+
* Flat structure: all activities (global + all workflow-specific) are at the root level
|
|
104
|
+
*/
|
|
105
|
+
type ActivitiesHandler<TContract extends ContractDefinition> = (TContract["activities"] extends Record<string, ActivityDefinition> ? ActivitiesImplementations<TContract["activities"]> : {}) & UnionToIntersection<{ [TWorkflow in keyof TContract["workflows"]]: TContract["workflows"][TWorkflow]["activities"] extends Record<string, ActivityDefinition> ? ActivitiesImplementations<TContract["workflows"][TWorkflow]["activities"]> : {} }[keyof TContract["workflows"]]>;
|
|
106
|
+
/**
|
|
107
|
+
* Create a typed activities handler with automatic validation and Result pattern
|
|
108
|
+
*
|
|
109
|
+
* This wraps all activity implementations with:
|
|
110
|
+
* - Validation at network boundaries
|
|
111
|
+
* - Result<T, ActivityError> pattern for explicit error handling
|
|
112
|
+
* - Automatic conversion from Result to Promise (throwing on Error)
|
|
113
|
+
*
|
|
114
|
+
* TypeScript ensures ALL activities (global + workflow-specific) are implemented.
|
|
115
|
+
*
|
|
116
|
+
* Use this to create the activities object for the Temporal Worker.
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```ts
|
|
120
|
+
* import { declareActivitiesHandler, ActivityError } from '@temporal-contract/worker/activity';
|
|
121
|
+
* import { Result, Future } from '@swan-io/boxed';
|
|
122
|
+
* import myContract from './contract';
|
|
123
|
+
*
|
|
124
|
+
* export const activities = declareActivitiesHandler({
|
|
125
|
+
* contract: myContract,
|
|
126
|
+
* activities: {
|
|
127
|
+
* // Activity returns Result instead of throwing
|
|
128
|
+
* // All technical exceptions must be wrapped in ActivityError for retry policies
|
|
129
|
+
* sendEmail: (args) => {
|
|
130
|
+
* return Future.make(async resolve => {
|
|
131
|
+
* try {
|
|
132
|
+
* await emailService.send(args);
|
|
133
|
+
* resolve(Result.Ok({ sent: true }));
|
|
134
|
+
* } catch (error) {
|
|
135
|
+
* // Wrap technical errors in ActivityError to enable retries
|
|
136
|
+
* resolve(Result.Error(
|
|
137
|
+
* new ActivityError(
|
|
138
|
+
* 'EMAIL_SEND_FAILED',
|
|
139
|
+
* 'Failed to send email',
|
|
140
|
+
* error // Original error as cause for debugging
|
|
141
|
+
* )
|
|
142
|
+
* ));
|
|
143
|
+
* }
|
|
144
|
+
* });
|
|
145
|
+
* },
|
|
146
|
+
* },
|
|
147
|
+
* });
|
|
148
|
+
*
|
|
149
|
+
* // Use with Temporal Worker
|
|
150
|
+
* import { Worker } from '@temporalio/worker';
|
|
151
|
+
*
|
|
152
|
+
* const worker = await Worker.create({
|
|
153
|
+
* workflowsPath: require.resolve('./workflows'),
|
|
154
|
+
* activities: activities,
|
|
155
|
+
* taskQueue: contract.taskQueue,
|
|
156
|
+
* });
|
|
157
|
+
* ```
|
|
158
|
+
*/
|
|
159
|
+
declare function declareActivitiesHandler<TContract extends ContractDefinition>(options: DeclareActivitiesHandlerOptions<TContract>): ActivitiesHandler<TContract>;
|
|
160
|
+
//#endregion
|
|
161
|
+
export { getWorkflowActivityNames as a, getWorkflowActivities as i, ActivityError as n, getWorkflowNames as o, declareActivitiesHandler as r, isWorkflowActivity as s, ActivitiesHandler as t };
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { g as WorkerInferOutput, h as WorkerInferInput } from "./errors-Vr-sKdW7.mjs";
|
|
2
|
+
import { ActivityDefinition, ContractDefinition } from "@temporal-contract/contract";
|
|
3
|
+
import { Future, Result } from "@swan-io/boxed";
|
|
4
|
+
|
|
5
|
+
//#region src/activity-utils.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* Extract activity definitions for a specific workflow from a contract
|
|
8
|
+
*
|
|
9
|
+
* This includes both:
|
|
10
|
+
* - Workflow-specific activities defined under workflow.activities
|
|
11
|
+
* - Global activities defined under contract.activities
|
|
12
|
+
*
|
|
13
|
+
* @param contract - The contract definition
|
|
14
|
+
* @param workflowName - The name of the workflow
|
|
15
|
+
* @returns Activity definitions for the workflow (workflow-specific + global activities merged)
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* const orderWorkflowActivities = getWorkflowActivities(myContract, 'processOrder');
|
|
20
|
+
* // Returns: { processPayment: ActivityDef, reserveInventory: ActivityDef, sendEmail: ActivityDef }
|
|
21
|
+
* // where sendEmail is a global activity
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
declare function getWorkflowActivities<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"]>(contract: TContract, workflowName: TWorkflowName): Record<string, ActivityDefinition>;
|
|
25
|
+
/**
|
|
26
|
+
* Extract all activity names for a specific workflow from a contract
|
|
27
|
+
*
|
|
28
|
+
* @param contract - The contract definition
|
|
29
|
+
* @param workflowName - The name of the workflow
|
|
30
|
+
* @returns Array of activity names (strings) available for the workflow
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* const activityNames = getWorkflowActivityNames(myContract, 'processOrder');
|
|
35
|
+
* // Returns: ['processPayment', 'reserveInventory', 'sendEmail']
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
declare function getWorkflowActivityNames<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"]>(contract: TContract, workflowName: TWorkflowName): string[];
|
|
39
|
+
/**
|
|
40
|
+
* Check if an activity belongs to a specific workflow
|
|
41
|
+
*
|
|
42
|
+
* @param contract - The contract definition
|
|
43
|
+
* @param workflowName - The name of the workflow
|
|
44
|
+
* @param activityName - The name of the activity to check
|
|
45
|
+
* @returns True if the activity is available for the workflow, false otherwise
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* if (isWorkflowActivity(myContract, 'processOrder', 'processPayment')) {
|
|
50
|
+
* // Activity is available for this workflow
|
|
51
|
+
* }
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
declare function isWorkflowActivity<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"]>(contract: TContract, workflowName: TWorkflowName, activityName: string): boolean;
|
|
55
|
+
/**
|
|
56
|
+
* Get all workflow names from a contract
|
|
57
|
+
*
|
|
58
|
+
* @param contract - The contract definition
|
|
59
|
+
* @returns Array of workflow names defined in the contract
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* const workflows = getWorkflowNames(myContract);
|
|
64
|
+
* // Returns: ['processOrder', 'processRefund']
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
declare function getWorkflowNames<TContract extends ContractDefinition>(contract: TContract): Array<keyof TContract["workflows"]>;
|
|
68
|
+
//#endregion
|
|
69
|
+
//#region src/activity.d.ts
|
|
70
|
+
/**
|
|
71
|
+
* Activity error class that should be used to wrap all technical exceptions
|
|
72
|
+
* Forces proper error handling and enables retry policies
|
|
73
|
+
*/
|
|
74
|
+
declare class ActivityError extends Error {
|
|
75
|
+
readonly code: string;
|
|
76
|
+
constructor(code: string, message: string, cause?: unknown);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Activity implementation using Future/Result pattern
|
|
80
|
+
*
|
|
81
|
+
* Returns Future<Result<Output, ActivityError>> for explicit error handling instead of throwing exceptions.
|
|
82
|
+
* All errors must be wrapped in ActivityError to enable proper retry policies.
|
|
83
|
+
*/
|
|
84
|
+
type BoxedActivityImplementation<TActivity extends ActivityDefinition> = (args: WorkerInferInput<TActivity>) => Future<Result<WorkerInferOutput<TActivity>, ActivityError>>;
|
|
85
|
+
/**
|
|
86
|
+
* Map of all activity implementations for a contract (global + all workflow-specific)
|
|
87
|
+
*/
|
|
88
|
+
type ContractBoxedActivitiesImplementations<TContract extends ContractDefinition> = (TContract["activities"] extends Record<string, ActivityDefinition> ? BoxedActivitiesImplementations<TContract["activities"]> : {}) & { [TWorkflow in keyof TContract["workflows"]]: TContract["workflows"][TWorkflow]["activities"] extends Record<string, ActivityDefinition> ? BoxedActivitiesImplementations<TContract["workflows"][TWorkflow]["activities"]> : {} };
|
|
89
|
+
type BoxedActivitiesImplementations<TActivities extends Record<string, ActivityDefinition>> = { [K in keyof TActivities]: BoxedActivityImplementation<TActivities[K]> };
|
|
90
|
+
/**
|
|
91
|
+
* Options for creating activities handler
|
|
92
|
+
*/
|
|
93
|
+
interface DeclareActivitiesHandlerOptions<TContract extends ContractDefinition> {
|
|
94
|
+
contract: TContract;
|
|
95
|
+
activities: ContractBoxedActivitiesImplementations<TContract>;
|
|
96
|
+
}
|
|
97
|
+
type ActivityImplementation<TActivity extends ActivityDefinition> = (args: WorkerInferInput<TActivity>) => Promise<WorkerInferOutput<TActivity>>;
|
|
98
|
+
type ActivitiesImplementations<TActivities extends Record<string, ActivityDefinition>> = { [K in keyof TActivities]: ActivityImplementation<TActivities[K]> };
|
|
99
|
+
type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
|
|
100
|
+
/**
|
|
101
|
+
* Activities handler ready for Temporal Worker
|
|
102
|
+
*
|
|
103
|
+
* Flat structure: all activities (global + all workflow-specific) are at the root level
|
|
104
|
+
*/
|
|
105
|
+
type ActivitiesHandler<TContract extends ContractDefinition> = (TContract["activities"] extends Record<string, ActivityDefinition> ? ActivitiesImplementations<TContract["activities"]> : {}) & UnionToIntersection<{ [TWorkflow in keyof TContract["workflows"]]: TContract["workflows"][TWorkflow]["activities"] extends Record<string, ActivityDefinition> ? ActivitiesImplementations<TContract["workflows"][TWorkflow]["activities"]> : {} }[keyof TContract["workflows"]]>;
|
|
106
|
+
/**
|
|
107
|
+
* Create a typed activities handler with automatic validation and Result pattern
|
|
108
|
+
*
|
|
109
|
+
* This wraps all activity implementations with:
|
|
110
|
+
* - Validation at network boundaries
|
|
111
|
+
* - Result<T, ActivityError> pattern for explicit error handling
|
|
112
|
+
* - Automatic conversion from Result to Promise (throwing on Error)
|
|
113
|
+
*
|
|
114
|
+
* TypeScript ensures ALL activities (global + workflow-specific) are implemented.
|
|
115
|
+
*
|
|
116
|
+
* Use this to create the activities object for the Temporal Worker.
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```ts
|
|
120
|
+
* import { declareActivitiesHandler, ActivityError } from '@temporal-contract/worker/activity';
|
|
121
|
+
* import { Result, Future } from '@swan-io/boxed';
|
|
122
|
+
* import myContract from './contract';
|
|
123
|
+
*
|
|
124
|
+
* export const activities = declareActivitiesHandler({
|
|
125
|
+
* contract: myContract,
|
|
126
|
+
* activities: {
|
|
127
|
+
* // Activity returns Result instead of throwing
|
|
128
|
+
* // All technical exceptions must be wrapped in ActivityError for retry policies
|
|
129
|
+
* sendEmail: (args) => {
|
|
130
|
+
* return Future.make(async resolve => {
|
|
131
|
+
* try {
|
|
132
|
+
* await emailService.send(args);
|
|
133
|
+
* resolve(Result.Ok({ sent: true }));
|
|
134
|
+
* } catch (error) {
|
|
135
|
+
* // Wrap technical errors in ActivityError to enable retries
|
|
136
|
+
* resolve(Result.Error(
|
|
137
|
+
* new ActivityError(
|
|
138
|
+
* 'EMAIL_SEND_FAILED',
|
|
139
|
+
* 'Failed to send email',
|
|
140
|
+
* error // Original error as cause for debugging
|
|
141
|
+
* )
|
|
142
|
+
* ));
|
|
143
|
+
* }
|
|
144
|
+
* });
|
|
145
|
+
* },
|
|
146
|
+
* },
|
|
147
|
+
* });
|
|
148
|
+
*
|
|
149
|
+
* // Use with Temporal Worker
|
|
150
|
+
* import { Worker } from '@temporalio/worker';
|
|
151
|
+
*
|
|
152
|
+
* const worker = await Worker.create({
|
|
153
|
+
* workflowsPath: require.resolve('./workflows'),
|
|
154
|
+
* activities: activities,
|
|
155
|
+
* taskQueue: contract.taskQueue,
|
|
156
|
+
* });
|
|
157
|
+
* ```
|
|
158
|
+
*/
|
|
159
|
+
declare function declareActivitiesHandler<TContract extends ContractDefinition>(options: DeclareActivitiesHandlerOptions<TContract>): ActivitiesHandler<TContract>;
|
|
160
|
+
//#endregion
|
|
161
|
+
export { getWorkflowActivityNames as a, getWorkflowActivities as i, ActivityError as n, getWorkflowNames as o, declareActivitiesHandler as r, isWorkflowActivity as s, ActivitiesHandler as t };
|
package/dist/activity.cjs
CHANGED
|
@@ -1,5 +1,83 @@
|
|
|
1
1
|
const require_errors = require('./errors-DjSZg-93.cjs');
|
|
2
2
|
|
|
3
|
+
//#region src/activity-utils.ts
|
|
4
|
+
/**
|
|
5
|
+
* Extract activity definitions for a specific workflow from a contract
|
|
6
|
+
*
|
|
7
|
+
* This includes both:
|
|
8
|
+
* - Workflow-specific activities defined under workflow.activities
|
|
9
|
+
* - Global activities defined under contract.activities
|
|
10
|
+
*
|
|
11
|
+
* @param contract - The contract definition
|
|
12
|
+
* @param workflowName - The name of the workflow
|
|
13
|
+
* @returns Activity definitions for the workflow (workflow-specific + global activities merged)
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const orderWorkflowActivities = getWorkflowActivities(myContract, 'processOrder');
|
|
18
|
+
* // Returns: { processPayment: ActivityDef, reserveInventory: ActivityDef, sendEmail: ActivityDef }
|
|
19
|
+
* // where sendEmail is a global activity
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
function getWorkflowActivities(contract, workflowName) {
|
|
23
|
+
const workflowActivities = contract.workflows[workflowName]?.activities || {};
|
|
24
|
+
return {
|
|
25
|
+
...contract.activities || {},
|
|
26
|
+
...workflowActivities
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Extract all activity names for a specific workflow from a contract
|
|
31
|
+
*
|
|
32
|
+
* @param contract - The contract definition
|
|
33
|
+
* @param workflowName - The name of the workflow
|
|
34
|
+
* @returns Array of activity names (strings) available for the workflow
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* const activityNames = getWorkflowActivityNames(myContract, 'processOrder');
|
|
39
|
+
* // Returns: ['processPayment', 'reserveInventory', 'sendEmail']
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
function getWorkflowActivityNames(contract, workflowName) {
|
|
43
|
+
const activities = getWorkflowActivities(contract, workflowName);
|
|
44
|
+
return Object.keys(activities);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Check if an activity belongs to a specific workflow
|
|
48
|
+
*
|
|
49
|
+
* @param contract - The contract definition
|
|
50
|
+
* @param workflowName - The name of the workflow
|
|
51
|
+
* @param activityName - The name of the activity to check
|
|
52
|
+
* @returns True if the activity is available for the workflow, false otherwise
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* if (isWorkflowActivity(myContract, 'processOrder', 'processPayment')) {
|
|
57
|
+
* // Activity is available for this workflow
|
|
58
|
+
* }
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
function isWorkflowActivity(contract, workflowName, activityName) {
|
|
62
|
+
return activityName in getWorkflowActivities(contract, workflowName);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Get all workflow names from a contract
|
|
66
|
+
*
|
|
67
|
+
* @param contract - The contract definition
|
|
68
|
+
* @returns Array of workflow names defined in the contract
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```ts
|
|
72
|
+
* const workflows = getWorkflowNames(myContract);
|
|
73
|
+
* // Returns: ['processOrder', 'processRefund']
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
function getWorkflowNames(contract) {
|
|
77
|
+
return Object.keys(contract.workflows);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
//#endregion
|
|
3
81
|
//#region src/activity.ts
|
|
4
82
|
/**
|
|
5
83
|
* Activity error class that should be used to wrap all technical exceptions
|
|
@@ -28,7 +106,7 @@ var ActivityError = class ActivityError extends Error {
|
|
|
28
106
|
* @example
|
|
29
107
|
* ```ts
|
|
30
108
|
* import { declareActivitiesHandler, ActivityError } from '@temporal-contract/worker/activity';
|
|
31
|
-
* import { Result, Future } from '@
|
|
109
|
+
* import { Result, Future } from '@swan-io/boxed';
|
|
32
110
|
* import myContract from './contract';
|
|
33
111
|
*
|
|
34
112
|
* export const activities = declareActivitiesHandler({
|
|
@@ -106,4 +184,8 @@ exports.ActivityDefinitionNotFoundError = require_errors.ActivityDefinitionNotFo
|
|
|
106
184
|
exports.ActivityError = ActivityError;
|
|
107
185
|
exports.ActivityInputValidationError = require_errors.ActivityInputValidationError;
|
|
108
186
|
exports.ActivityOutputValidationError = require_errors.ActivityOutputValidationError;
|
|
109
|
-
exports.declareActivitiesHandler = declareActivitiesHandler;
|
|
187
|
+
exports.declareActivitiesHandler = declareActivitiesHandler;
|
|
188
|
+
exports.getWorkflowActivities = getWorkflowActivities;
|
|
189
|
+
exports.getWorkflowActivityNames = getWorkflowActivityNames;
|
|
190
|
+
exports.getWorkflowNames = getWorkflowNames;
|
|
191
|
+
exports.isWorkflowActivity = isWorkflowActivity;
|
package/dist/activity.d.cts
CHANGED
|
@@ -1,98 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
//#region src/activity.d.ts
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Activity error class that should be used to wrap all technical exceptions
|
|
9
|
-
* Forces proper error handling and enables retry policies
|
|
10
|
-
*/
|
|
11
|
-
declare class ActivityError extends Error {
|
|
12
|
-
readonly code: string;
|
|
13
|
-
constructor(code: string, message: string, cause?: unknown);
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Activity implementation using Future/Result pattern
|
|
17
|
-
*
|
|
18
|
-
* Returns Future<Result<Output, ActivityError>> for explicit error handling instead of throwing exceptions.
|
|
19
|
-
* All errors must be wrapped in ActivityError to enable proper retry policies.
|
|
20
|
-
*/
|
|
21
|
-
type BoxedActivityImplementation<TActivity extends ActivityDefinition> = (args: WorkerInferInput<TActivity>) => Future<Result<WorkerInferOutput<TActivity>, ActivityError>>;
|
|
22
|
-
/**
|
|
23
|
-
* Map of all activity implementations for a contract (global + all workflow-specific)
|
|
24
|
-
*/
|
|
25
|
-
type ContractBoxedActivitiesImplementations<TContract extends ContractDefinition> = (TContract["activities"] extends Record<string, ActivityDefinition> ? BoxedActivitiesImplementations<TContract["activities"]> : {}) & { [TWorkflow in keyof TContract["workflows"]]: TContract["workflows"][TWorkflow]["activities"] extends Record<string, ActivityDefinition> ? BoxedActivitiesImplementations<TContract["workflows"][TWorkflow]["activities"]> : {} };
|
|
26
|
-
type BoxedActivitiesImplementations<TActivities extends Record<string, ActivityDefinition>> = { [K in keyof TActivities]: BoxedActivityImplementation<TActivities[K]> };
|
|
27
|
-
/**
|
|
28
|
-
* Options for creating activities handler
|
|
29
|
-
*/
|
|
30
|
-
interface DeclareActivitiesHandlerOptions<TContract extends ContractDefinition> {
|
|
31
|
-
contract: TContract;
|
|
32
|
-
activities: ContractBoxedActivitiesImplementations<TContract>;
|
|
33
|
-
}
|
|
34
|
-
type ActivityImplementation<TActivity extends ActivityDefinition> = (args: WorkerInferInput<TActivity>) => Promise<WorkerInferOutput<TActivity>>;
|
|
35
|
-
type ActivitiesImplementations<TActivities extends Record<string, ActivityDefinition>> = { [K in keyof TActivities]: ActivityImplementation<TActivities[K]> };
|
|
36
|
-
type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
|
|
37
|
-
/**
|
|
38
|
-
* Activities handler ready for Temporal Worker
|
|
39
|
-
*
|
|
40
|
-
* Flat structure: all activities (global + all workflow-specific) are at the root level
|
|
41
|
-
*/
|
|
42
|
-
type ActivitiesHandler<TContract extends ContractDefinition> = (TContract["activities"] extends Record<string, ActivityDefinition> ? ActivitiesImplementations<TContract["activities"]> : {}) & UnionToIntersection<{ [TWorkflow in keyof TContract["workflows"]]: TContract["workflows"][TWorkflow]["activities"] extends Record<string, ActivityDefinition> ? ActivitiesImplementations<TContract["workflows"][TWorkflow]["activities"]> : {} }[keyof TContract["workflows"]]>;
|
|
43
|
-
/**
|
|
44
|
-
* Create a typed activities handler with automatic validation and Result pattern
|
|
45
|
-
*
|
|
46
|
-
* This wraps all activity implementations with:
|
|
47
|
-
* - Validation at network boundaries
|
|
48
|
-
* - Result<T, ActivityError> pattern for explicit error handling
|
|
49
|
-
* - Automatic conversion from Result to Promise (throwing on Error)
|
|
50
|
-
*
|
|
51
|
-
* TypeScript ensures ALL activities (global + workflow-specific) are implemented.
|
|
52
|
-
*
|
|
53
|
-
* Use this to create the activities object for the Temporal Worker.
|
|
54
|
-
*
|
|
55
|
-
* @example
|
|
56
|
-
* ```ts
|
|
57
|
-
* import { declareActivitiesHandler, ActivityError } from '@temporal-contract/worker/activity';
|
|
58
|
-
* import { Result, Future } from '@temporal-contract/boxed';
|
|
59
|
-
* import myContract from './contract';
|
|
60
|
-
*
|
|
61
|
-
* export const activities = declareActivitiesHandler({
|
|
62
|
-
* contract: myContract,
|
|
63
|
-
* activities: {
|
|
64
|
-
* // Activity returns Result instead of throwing
|
|
65
|
-
* // All technical exceptions must be wrapped in ActivityError for retry policies
|
|
66
|
-
* sendEmail: (args) => {
|
|
67
|
-
* return Future.make(async resolve => {
|
|
68
|
-
* try {
|
|
69
|
-
* await emailService.send(args);
|
|
70
|
-
* resolve(Result.Ok({ sent: true }));
|
|
71
|
-
* } catch (error) {
|
|
72
|
-
* // Wrap technical errors in ActivityError to enable retries
|
|
73
|
-
* resolve(Result.Error(
|
|
74
|
-
* new ActivityError(
|
|
75
|
-
* 'EMAIL_SEND_FAILED',
|
|
76
|
-
* 'Failed to send email',
|
|
77
|
-
* error // Original error as cause for debugging
|
|
78
|
-
* )
|
|
79
|
-
* ));
|
|
80
|
-
* }
|
|
81
|
-
* });
|
|
82
|
-
* },
|
|
83
|
-
* },
|
|
84
|
-
* });
|
|
85
|
-
*
|
|
86
|
-
* // Use with Temporal Worker
|
|
87
|
-
* import { Worker } from '@temporalio/worker';
|
|
88
|
-
*
|
|
89
|
-
* const worker = await Worker.create({
|
|
90
|
-
* workflowsPath: require.resolve('./workflows'),
|
|
91
|
-
* activities: activities,
|
|
92
|
-
* taskQueue: contract.taskQueue,
|
|
93
|
-
* });
|
|
94
|
-
* ```
|
|
95
|
-
*/
|
|
96
|
-
declare function declareActivitiesHandler<TContract extends ContractDefinition>(options: DeclareActivitiesHandlerOptions<TContract>): ActivitiesHandler<TContract>;
|
|
97
|
-
//#endregion
|
|
98
|
-
export { ActivitiesHandler, ActivityDefinitionNotFoundError, ActivityError, ActivityInputValidationError, ActivityOutputValidationError, declareActivitiesHandler };
|
|
1
|
+
import { n as ActivityInputValidationError, r as ActivityOutputValidationError, t as ActivityDefinitionNotFoundError } from "./errors-CXpHOFmk.cjs";
|
|
2
|
+
import { a as getWorkflowActivityNames, i as getWorkflowActivities, n as ActivityError, o as getWorkflowNames, r as declareActivitiesHandler, s as isWorkflowActivity, t as ActivitiesHandler } from "./activity-5pVNjW7l.cjs";
|
|
3
|
+
export { ActivitiesHandler, ActivityDefinitionNotFoundError, ActivityError, ActivityInputValidationError, ActivityOutputValidationError, declareActivitiesHandler, getWorkflowActivities, getWorkflowActivityNames, getWorkflowNames, isWorkflowActivity };
|
package/dist/activity.d.mts
CHANGED
|
@@ -1,98 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
//#region src/activity.d.ts
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Activity error class that should be used to wrap all technical exceptions
|
|
9
|
-
* Forces proper error handling and enables retry policies
|
|
10
|
-
*/
|
|
11
|
-
declare class ActivityError extends Error {
|
|
12
|
-
readonly code: string;
|
|
13
|
-
constructor(code: string, message: string, cause?: unknown);
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Activity implementation using Future/Result pattern
|
|
17
|
-
*
|
|
18
|
-
* Returns Future<Result<Output, ActivityError>> for explicit error handling instead of throwing exceptions.
|
|
19
|
-
* All errors must be wrapped in ActivityError to enable proper retry policies.
|
|
20
|
-
*/
|
|
21
|
-
type BoxedActivityImplementation<TActivity extends ActivityDefinition> = (args: WorkerInferInput<TActivity>) => Future<Result<WorkerInferOutput<TActivity>, ActivityError>>;
|
|
22
|
-
/**
|
|
23
|
-
* Map of all activity implementations for a contract (global + all workflow-specific)
|
|
24
|
-
*/
|
|
25
|
-
type ContractBoxedActivitiesImplementations<TContract extends ContractDefinition> = (TContract["activities"] extends Record<string, ActivityDefinition> ? BoxedActivitiesImplementations<TContract["activities"]> : {}) & { [TWorkflow in keyof TContract["workflows"]]: TContract["workflows"][TWorkflow]["activities"] extends Record<string, ActivityDefinition> ? BoxedActivitiesImplementations<TContract["workflows"][TWorkflow]["activities"]> : {} };
|
|
26
|
-
type BoxedActivitiesImplementations<TActivities extends Record<string, ActivityDefinition>> = { [K in keyof TActivities]: BoxedActivityImplementation<TActivities[K]> };
|
|
27
|
-
/**
|
|
28
|
-
* Options for creating activities handler
|
|
29
|
-
*/
|
|
30
|
-
interface DeclareActivitiesHandlerOptions<TContract extends ContractDefinition> {
|
|
31
|
-
contract: TContract;
|
|
32
|
-
activities: ContractBoxedActivitiesImplementations<TContract>;
|
|
33
|
-
}
|
|
34
|
-
type ActivityImplementation<TActivity extends ActivityDefinition> = (args: WorkerInferInput<TActivity>) => Promise<WorkerInferOutput<TActivity>>;
|
|
35
|
-
type ActivitiesImplementations<TActivities extends Record<string, ActivityDefinition>> = { [K in keyof TActivities]: ActivityImplementation<TActivities[K]> };
|
|
36
|
-
type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
|
|
37
|
-
/**
|
|
38
|
-
* Activities handler ready for Temporal Worker
|
|
39
|
-
*
|
|
40
|
-
* Flat structure: all activities (global + all workflow-specific) are at the root level
|
|
41
|
-
*/
|
|
42
|
-
type ActivitiesHandler<TContract extends ContractDefinition> = (TContract["activities"] extends Record<string, ActivityDefinition> ? ActivitiesImplementations<TContract["activities"]> : {}) & UnionToIntersection<{ [TWorkflow in keyof TContract["workflows"]]: TContract["workflows"][TWorkflow]["activities"] extends Record<string, ActivityDefinition> ? ActivitiesImplementations<TContract["workflows"][TWorkflow]["activities"]> : {} }[keyof TContract["workflows"]]>;
|
|
43
|
-
/**
|
|
44
|
-
* Create a typed activities handler with automatic validation and Result pattern
|
|
45
|
-
*
|
|
46
|
-
* This wraps all activity implementations with:
|
|
47
|
-
* - Validation at network boundaries
|
|
48
|
-
* - Result<T, ActivityError> pattern for explicit error handling
|
|
49
|
-
* - Automatic conversion from Result to Promise (throwing on Error)
|
|
50
|
-
*
|
|
51
|
-
* TypeScript ensures ALL activities (global + workflow-specific) are implemented.
|
|
52
|
-
*
|
|
53
|
-
* Use this to create the activities object for the Temporal Worker.
|
|
54
|
-
*
|
|
55
|
-
* @example
|
|
56
|
-
* ```ts
|
|
57
|
-
* import { declareActivitiesHandler, ActivityError } from '@temporal-contract/worker/activity';
|
|
58
|
-
* import { Result, Future } from '@temporal-contract/boxed';
|
|
59
|
-
* import myContract from './contract';
|
|
60
|
-
*
|
|
61
|
-
* export const activities = declareActivitiesHandler({
|
|
62
|
-
* contract: myContract,
|
|
63
|
-
* activities: {
|
|
64
|
-
* // Activity returns Result instead of throwing
|
|
65
|
-
* // All technical exceptions must be wrapped in ActivityError for retry policies
|
|
66
|
-
* sendEmail: (args) => {
|
|
67
|
-
* return Future.make(async resolve => {
|
|
68
|
-
* try {
|
|
69
|
-
* await emailService.send(args);
|
|
70
|
-
* resolve(Result.Ok({ sent: true }));
|
|
71
|
-
* } catch (error) {
|
|
72
|
-
* // Wrap technical errors in ActivityError to enable retries
|
|
73
|
-
* resolve(Result.Error(
|
|
74
|
-
* new ActivityError(
|
|
75
|
-
* 'EMAIL_SEND_FAILED',
|
|
76
|
-
* 'Failed to send email',
|
|
77
|
-
* error // Original error as cause for debugging
|
|
78
|
-
* )
|
|
79
|
-
* ));
|
|
80
|
-
* }
|
|
81
|
-
* });
|
|
82
|
-
* },
|
|
83
|
-
* },
|
|
84
|
-
* });
|
|
85
|
-
*
|
|
86
|
-
* // Use with Temporal Worker
|
|
87
|
-
* import { Worker } from '@temporalio/worker';
|
|
88
|
-
*
|
|
89
|
-
* const worker = await Worker.create({
|
|
90
|
-
* workflowsPath: require.resolve('./workflows'),
|
|
91
|
-
* activities: activities,
|
|
92
|
-
* taskQueue: contract.taskQueue,
|
|
93
|
-
* });
|
|
94
|
-
* ```
|
|
95
|
-
*/
|
|
96
|
-
declare function declareActivitiesHandler<TContract extends ContractDefinition>(options: DeclareActivitiesHandlerOptions<TContract>): ActivitiesHandler<TContract>;
|
|
97
|
-
//#endregion
|
|
98
|
-
export { ActivitiesHandler, ActivityDefinitionNotFoundError, ActivityError, ActivityInputValidationError, ActivityOutputValidationError, declareActivitiesHandler };
|
|
1
|
+
import { n as ActivityInputValidationError, r as ActivityOutputValidationError, t as ActivityDefinitionNotFoundError } from "./errors-Vr-sKdW7.mjs";
|
|
2
|
+
import { a as getWorkflowActivityNames, i as getWorkflowActivities, n as ActivityError, o as getWorkflowNames, r as declareActivitiesHandler, s as isWorkflowActivity, t as ActivitiesHandler } from "./activity-BUEBZ7SL.mjs";
|
|
3
|
+
export { ActivitiesHandler, ActivityDefinitionNotFoundError, ActivityError, ActivityInputValidationError, ActivityOutputValidationError, declareActivitiesHandler, getWorkflowActivities, getWorkflowActivityNames, getWorkflowNames, isWorkflowActivity };
|
package/dist/activity.mjs
CHANGED
|
@@ -1,5 +1,83 @@
|
|
|
1
1
|
import { n as ActivityInputValidationError, r as ActivityOutputValidationError, t as ActivityDefinitionNotFoundError } from "./errors-BqVTpfcf.mjs";
|
|
2
2
|
|
|
3
|
+
//#region src/activity-utils.ts
|
|
4
|
+
/**
|
|
5
|
+
* Extract activity definitions for a specific workflow from a contract
|
|
6
|
+
*
|
|
7
|
+
* This includes both:
|
|
8
|
+
* - Workflow-specific activities defined under workflow.activities
|
|
9
|
+
* - Global activities defined under contract.activities
|
|
10
|
+
*
|
|
11
|
+
* @param contract - The contract definition
|
|
12
|
+
* @param workflowName - The name of the workflow
|
|
13
|
+
* @returns Activity definitions for the workflow (workflow-specific + global activities merged)
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const orderWorkflowActivities = getWorkflowActivities(myContract, 'processOrder');
|
|
18
|
+
* // Returns: { processPayment: ActivityDef, reserveInventory: ActivityDef, sendEmail: ActivityDef }
|
|
19
|
+
* // where sendEmail is a global activity
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
function getWorkflowActivities(contract, workflowName) {
|
|
23
|
+
const workflowActivities = contract.workflows[workflowName]?.activities || {};
|
|
24
|
+
return {
|
|
25
|
+
...contract.activities || {},
|
|
26
|
+
...workflowActivities
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Extract all activity names for a specific workflow from a contract
|
|
31
|
+
*
|
|
32
|
+
* @param contract - The contract definition
|
|
33
|
+
* @param workflowName - The name of the workflow
|
|
34
|
+
* @returns Array of activity names (strings) available for the workflow
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* const activityNames = getWorkflowActivityNames(myContract, 'processOrder');
|
|
39
|
+
* // Returns: ['processPayment', 'reserveInventory', 'sendEmail']
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
function getWorkflowActivityNames(contract, workflowName) {
|
|
43
|
+
const activities = getWorkflowActivities(contract, workflowName);
|
|
44
|
+
return Object.keys(activities);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Check if an activity belongs to a specific workflow
|
|
48
|
+
*
|
|
49
|
+
* @param contract - The contract definition
|
|
50
|
+
* @param workflowName - The name of the workflow
|
|
51
|
+
* @param activityName - The name of the activity to check
|
|
52
|
+
* @returns True if the activity is available for the workflow, false otherwise
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* if (isWorkflowActivity(myContract, 'processOrder', 'processPayment')) {
|
|
57
|
+
* // Activity is available for this workflow
|
|
58
|
+
* }
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
function isWorkflowActivity(contract, workflowName, activityName) {
|
|
62
|
+
return activityName in getWorkflowActivities(contract, workflowName);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Get all workflow names from a contract
|
|
66
|
+
*
|
|
67
|
+
* @param contract - The contract definition
|
|
68
|
+
* @returns Array of workflow names defined in the contract
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```ts
|
|
72
|
+
* const workflows = getWorkflowNames(myContract);
|
|
73
|
+
* // Returns: ['processOrder', 'processRefund']
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
function getWorkflowNames(contract) {
|
|
77
|
+
return Object.keys(contract.workflows);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
//#endregion
|
|
3
81
|
//#region src/activity.ts
|
|
4
82
|
/**
|
|
5
83
|
* Activity error class that should be used to wrap all technical exceptions
|
|
@@ -28,7 +106,7 @@ var ActivityError = class ActivityError extends Error {
|
|
|
28
106
|
* @example
|
|
29
107
|
* ```ts
|
|
30
108
|
* import { declareActivitiesHandler, ActivityError } from '@temporal-contract/worker/activity';
|
|
31
|
-
* import { Result, Future } from '@
|
|
109
|
+
* import { Result, Future } from '@swan-io/boxed';
|
|
32
110
|
* import myContract from './contract';
|
|
33
111
|
*
|
|
34
112
|
* export const activities = declareActivitiesHandler({
|
|
@@ -102,4 +180,4 @@ function declareActivitiesHandler(options) {
|
|
|
102
180
|
}
|
|
103
181
|
|
|
104
182
|
//#endregion
|
|
105
|
-
export { ActivityDefinitionNotFoundError, ActivityError, ActivityInputValidationError, ActivityOutputValidationError, declareActivitiesHandler };
|
|
183
|
+
export { ActivityDefinitionNotFoundError, ActivityError, ActivityInputValidationError, ActivityOutputValidationError, declareActivitiesHandler, getWorkflowActivities, getWorkflowActivityNames, getWorkflowNames, isWorkflowActivity };
|
|
@@ -2,7 +2,6 @@ import { AnySchema } from "@temporal-contract/contract";
|
|
|
2
2
|
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
3
3
|
|
|
4
4
|
//#region src/types.d.ts
|
|
5
|
-
|
|
6
5
|
/**
|
|
7
6
|
* Infer input type from a definition (worker perspective)
|
|
8
7
|
* Worker receives the output type (after input schema parsing/transformation)
|
|
@@ -2,7 +2,6 @@ import { AnySchema } from "@temporal-contract/contract";
|
|
|
2
2
|
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
3
3
|
|
|
4
4
|
//#region src/types.d.ts
|
|
5
|
-
|
|
6
5
|
/**
|
|
7
6
|
* Infer input type from a definition (worker perspective)
|
|
8
7
|
* Worker receives the output type (after input schema parsing/transformation)
|
package/dist/worker.d.cts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { ActivitiesHandler } from "./activity.cjs";
|
|
1
|
+
import { t as ActivitiesHandler } from "./activity-5pVNjW7l.cjs";
|
|
2
2
|
import { ContractDefinition } from "@temporal-contract/contract";
|
|
3
3
|
import { Worker, WorkerOptions } from "@temporalio/worker";
|
|
4
4
|
|
|
5
5
|
//#region src/worker.d.ts
|
|
6
|
-
|
|
7
6
|
/**
|
|
8
7
|
* Options for creating a Temporal worker
|
|
9
8
|
*/
|
package/dist/worker.d.mts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { ActivitiesHandler } from "./activity.mjs";
|
|
1
|
+
import { t as ActivitiesHandler } from "./activity-BUEBZ7SL.mjs";
|
|
2
2
|
import { Worker, WorkerOptions } from "@temporalio/worker";
|
|
3
3
|
import { ContractDefinition } from "@temporal-contract/contract";
|
|
4
4
|
|
|
5
5
|
//#region src/worker.d.ts
|
|
6
|
-
|
|
7
6
|
/**
|
|
8
7
|
* Options for creating a Temporal worker
|
|
9
8
|
*/
|
package/dist/workflow.d.cts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import { a as ChildWorkflowNotFoundError, c as SignalInputValidationError, d as WorkflowInputValidationError, f as WorkflowOutputValidationError, g as WorkerInferOutput, h as WorkerInferInput, i as ChildWorkflowError, l as UpdateInputValidationError, m as ClientInferOutput, n as ActivityInputValidationError, o as QueryInputValidationError, p as ClientInferInput, r as ActivityOutputValidationError, s as QueryOutputValidationError, u as UpdateOutputValidationError } from "./errors-
|
|
1
|
+
import { a as ChildWorkflowNotFoundError, c as SignalInputValidationError, d as WorkflowInputValidationError, f as WorkflowOutputValidationError, g as WorkerInferOutput, h as WorkerInferInput, i as ChildWorkflowError, l as UpdateInputValidationError, m as ClientInferOutput, n as ActivityInputValidationError, o as QueryInputValidationError, p as ClientInferInput, r as ActivityOutputValidationError, s as QueryOutputValidationError, u as UpdateOutputValidationError } from "./errors-CXpHOFmk.cjs";
|
|
2
2
|
import { ActivityDefinition, ContractDefinition, QueryDefinition, SignalDefinition, UpdateDefinition, WorkflowDefinition } from "@temporal-contract/contract";
|
|
3
3
|
import { Future, Result } from "@temporal-contract/boxed";
|
|
4
4
|
import { ActivityOptions, ChildWorkflowOptions, WorkflowInfo } from "@temporalio/workflow";
|
|
5
5
|
|
|
6
6
|
//#region src/workflow.d.ts
|
|
7
|
-
|
|
8
7
|
/**
|
|
9
8
|
* Create a typed workflow implementation with automatic validation
|
|
10
9
|
*
|
|
@@ -158,7 +157,7 @@ interface WorkflowContext<TContract extends ContractDefinition, TWorkflowName ex
|
|
|
158
157
|
* }
|
|
159
158
|
* ```
|
|
160
159
|
*/
|
|
161
|
-
defineSignal: <K
|
|
160
|
+
defineSignal: <K extends keyof TContract["workflows"][TWorkflowName]["signals"]>(signalName: K, handler: SignalHandlerImplementation<TContract["workflows"][TWorkflowName]["signals"][K] extends SignalDefinition ? TContract["workflows"][TWorkflowName]["signals"][K] : never>) => void;
|
|
162
161
|
/**
|
|
163
162
|
* Define a query handler within the workflow implementation
|
|
164
163
|
* Allows the query handler to access workflow state
|
|
@@ -176,7 +175,7 @@ interface WorkflowContext<TContract extends ContractDefinition, TWorkflowName ex
|
|
|
176
175
|
* }
|
|
177
176
|
* ```
|
|
178
177
|
*/
|
|
179
|
-
defineQuery: <K
|
|
178
|
+
defineQuery: <K extends keyof TContract["workflows"][TWorkflowName]["queries"]>(queryName: K, handler: QueryHandlerImplementation<TContract["workflows"][TWorkflowName]["queries"][K] extends QueryDefinition ? TContract["workflows"][TWorkflowName]["queries"][K] : never>) => void;
|
|
180
179
|
/**
|
|
181
180
|
* Define an update handler within the workflow implementation
|
|
182
181
|
* Allows the update handler to access and modify workflow state
|
|
@@ -195,7 +194,7 @@ interface WorkflowContext<TContract extends ContractDefinition, TWorkflowName ex
|
|
|
195
194
|
* }
|
|
196
195
|
* ```
|
|
197
196
|
*/
|
|
198
|
-
defineUpdate: <K
|
|
197
|
+
defineUpdate: <K extends keyof TContract["workflows"][TWorkflowName]["updates"]>(updateName: K, handler: UpdateHandlerImplementation<TContract["workflows"][TWorkflowName]["updates"][K] extends UpdateDefinition ? TContract["workflows"][TWorkflowName]["updates"][K] : never>) => void;
|
|
199
198
|
/**
|
|
200
199
|
* Start a child workflow and return a typed handle with Future/Result pattern
|
|
201
200
|
*
|
package/dist/workflow.d.mts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import { a as ChildWorkflowNotFoundError, c as SignalInputValidationError, d as WorkflowInputValidationError, f as WorkflowOutputValidationError, g as WorkerInferOutput, h as WorkerInferInput, i as ChildWorkflowError, l as UpdateInputValidationError, m as ClientInferOutput, n as ActivityInputValidationError, o as QueryInputValidationError, p as ClientInferInput, r as ActivityOutputValidationError, s as QueryOutputValidationError, u as UpdateOutputValidationError } from "./errors-
|
|
1
|
+
import { a as ChildWorkflowNotFoundError, c as SignalInputValidationError, d as WorkflowInputValidationError, f as WorkflowOutputValidationError, g as WorkerInferOutput, h as WorkerInferInput, i as ChildWorkflowError, l as UpdateInputValidationError, m as ClientInferOutput, n as ActivityInputValidationError, o as QueryInputValidationError, p as ClientInferInput, r as ActivityOutputValidationError, s as QueryOutputValidationError, u as UpdateOutputValidationError } from "./errors-Vr-sKdW7.mjs";
|
|
2
2
|
import { Future, Result } from "@temporal-contract/boxed";
|
|
3
3
|
import { ActivityOptions, ChildWorkflowOptions, WorkflowInfo } from "@temporalio/workflow";
|
|
4
4
|
import { ActivityDefinition, ContractDefinition, QueryDefinition, SignalDefinition, UpdateDefinition, WorkflowDefinition } from "@temporal-contract/contract";
|
|
5
5
|
|
|
6
6
|
//#region src/workflow.d.ts
|
|
7
|
-
|
|
8
7
|
/**
|
|
9
8
|
* Create a typed workflow implementation with automatic validation
|
|
10
9
|
*
|
|
@@ -158,7 +157,7 @@ interface WorkflowContext<TContract extends ContractDefinition, TWorkflowName ex
|
|
|
158
157
|
* }
|
|
159
158
|
* ```
|
|
160
159
|
*/
|
|
161
|
-
defineSignal: <K
|
|
160
|
+
defineSignal: <K extends keyof TContract["workflows"][TWorkflowName]["signals"]>(signalName: K, handler: SignalHandlerImplementation<TContract["workflows"][TWorkflowName]["signals"][K] extends SignalDefinition ? TContract["workflows"][TWorkflowName]["signals"][K] : never>) => void;
|
|
162
161
|
/**
|
|
163
162
|
* Define a query handler within the workflow implementation
|
|
164
163
|
* Allows the query handler to access workflow state
|
|
@@ -176,7 +175,7 @@ interface WorkflowContext<TContract extends ContractDefinition, TWorkflowName ex
|
|
|
176
175
|
* }
|
|
177
176
|
* ```
|
|
178
177
|
*/
|
|
179
|
-
defineQuery: <K
|
|
178
|
+
defineQuery: <K extends keyof TContract["workflows"][TWorkflowName]["queries"]>(queryName: K, handler: QueryHandlerImplementation<TContract["workflows"][TWorkflowName]["queries"][K] extends QueryDefinition ? TContract["workflows"][TWorkflowName]["queries"][K] : never>) => void;
|
|
180
179
|
/**
|
|
181
180
|
* Define an update handler within the workflow implementation
|
|
182
181
|
* Allows the update handler to access and modify workflow state
|
|
@@ -195,7 +194,7 @@ interface WorkflowContext<TContract extends ContractDefinition, TWorkflowName ex
|
|
|
195
194
|
* }
|
|
196
195
|
* ```
|
|
197
196
|
*/
|
|
198
|
-
defineUpdate: <K
|
|
197
|
+
defineUpdate: <K extends keyof TContract["workflows"][TWorkflowName]["updates"]>(updateName: K, handler: UpdateHandlerImplementation<TContract["workflows"][TWorkflowName]["updates"][K] extends UpdateDefinition ? TContract["workflows"][TWorkflowName]["updates"][K] : never>) => void;
|
|
199
198
|
/**
|
|
200
199
|
* Start a child workflow and return a typed handle with Future/Result pattern
|
|
201
200
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@temporal-contract/worker",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "Worker utilities with Result/Future pattern for implementing temporal-contract workflows and activities",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"contract",
|
|
@@ -63,22 +63,24 @@
|
|
|
63
63
|
],
|
|
64
64
|
"dependencies": {
|
|
65
65
|
"@standard-schema/spec": "1.1.0",
|
|
66
|
-
"@
|
|
67
|
-
"@temporal-contract/
|
|
66
|
+
"@swan-io/boxed": "3.2.1",
|
|
67
|
+
"@temporal-contract/boxed": "0.1.0",
|
|
68
|
+
"@temporal-contract/contract": "0.1.0"
|
|
68
69
|
},
|
|
69
70
|
"devDependencies": {
|
|
70
|
-
"@temporalio/client": "1.14.
|
|
71
|
-
"@temporalio/worker": "1.14.
|
|
72
|
-
"@temporalio/workflow": "1.14.
|
|
73
|
-
"@types/node": "25.0.
|
|
74
|
-
"@vitest/coverage-v8": "4.0.
|
|
75
|
-
"tsdown": "0.
|
|
71
|
+
"@temporalio/client": "1.14.1",
|
|
72
|
+
"@temporalio/worker": "1.14.1",
|
|
73
|
+
"@temporalio/workflow": "1.14.1",
|
|
74
|
+
"@types/node": "25.0.9",
|
|
75
|
+
"@vitest/coverage-v8": "4.0.17",
|
|
76
|
+
"tsdown": "0.20.0-beta.4",
|
|
76
77
|
"typescript": "5.9.3",
|
|
77
|
-
"vitest": "4.0.
|
|
78
|
-
"zod": "4.
|
|
79
|
-
"@temporal-contract/client": "0.0
|
|
80
|
-
"@temporal-contract/testing": "0.0
|
|
81
|
-
"@temporal-contract/tsconfig": "0.0
|
|
78
|
+
"vitest": "4.0.17",
|
|
79
|
+
"zod": "4.3.5",
|
|
80
|
+
"@temporal-contract/client": "0.1.0",
|
|
81
|
+
"@temporal-contract/testing": "0.1.0",
|
|
82
|
+
"@temporal-contract/tsconfig": "0.1.0",
|
|
83
|
+
"@temporal-contract/typedoc": "0.1.0"
|
|
82
84
|
},
|
|
83
85
|
"peerDependencies": {
|
|
84
86
|
"@temporalio/worker": "^1",
|
|
@@ -86,6 +88,7 @@
|
|
|
86
88
|
},
|
|
87
89
|
"scripts": {
|
|
88
90
|
"build": "tsdown src/activity.ts src/worker.ts src/workflow.ts --format cjs,esm --dts --clean",
|
|
91
|
+
"build:docs": "typedoc",
|
|
89
92
|
"dev": "tsdown src/activity.ts src/worker.ts src/workflow.ts --format cjs,esm --dts --watch",
|
|
90
93
|
"test": "vitest run --project unit",
|
|
91
94
|
"test:integration": "vitest run --project integration",
|