@temporal-contract/worker 0.0.5 → 0.0.7
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 +23 -0
- package/dist/activity-Csptpwgf.d.cts +162 -0
- package/dist/activity-UOnp_bSX.d.mts +162 -0
- package/dist/activity.cjs +190 -7
- package/dist/activity.d.cts +3 -2
- package/dist/activity.d.mts +3 -2
- package/dist/activity.mjs +182 -2
- package/dist/errors-BqVTpfcf.mjs +155 -0
- package/dist/errors-BqYWpdvd.d.cts +137 -0
- package/dist/errors-C1RFkCuD.d.mts +137 -0
- package/dist/errors-DjSZg-93.cjs +227 -0
- package/dist/worker.cjs +65 -0
- package/dist/worker.d.cts +68 -0
- package/dist/worker.d.mts +68 -0
- package/dist/worker.mjs +64 -0
- package/dist/workflow.cjs +255 -12
- package/dist/workflow.d.cts +299 -2
- package/dist/workflow.d.mts +299 -2
- package/dist/workflow.mjs +244 -2
- package/package.json +26 -14
- package/dist/handler-BAzNuAZz.cjs +0 -597
- package/dist/handler-Cp9h_XCy.d.cts +0 -560
- package/dist/handler-DFFpSaGN.mjs +0 -502
- package/dist/handler-uAeIi9f0.d.mts +0 -560
package/README.md
CHANGED
|
@@ -34,6 +34,29 @@ export const processOrder = declareWorkflow({
|
|
|
34
34
|
return { success: true };
|
|
35
35
|
}
|
|
36
36
|
});
|
|
37
|
+
|
|
38
|
+
// worker.ts
|
|
39
|
+
import { NativeConnection } from '@temporalio/worker';
|
|
40
|
+
import { createWorker } from '@temporal-contract/worker/worker';
|
|
41
|
+
import { activities } from './activities';
|
|
42
|
+
import myContract from './contract';
|
|
43
|
+
|
|
44
|
+
async function run() {
|
|
45
|
+
const connection = await NativeConnection.connect({
|
|
46
|
+
address: 'localhost:7233',
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const worker = await createWorker({
|
|
50
|
+
contract: myContract,
|
|
51
|
+
connection,
|
|
52
|
+
workflowsPath: require.resolve('./workflows'),
|
|
53
|
+
activities,
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
await worker.run();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
run().catch(console.error);
|
|
37
60
|
```
|
|
38
61
|
|
|
39
62
|
### Child Workflows
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { g as WorkerInferOutput, h as WorkerInferInput } from "./errors-BqYWpdvd.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
|
+
/**
|
|
8
|
+
* Extract activity definitions for a specific workflow from a contract
|
|
9
|
+
*
|
|
10
|
+
* This includes both:
|
|
11
|
+
* - Workflow-specific activities defined under workflow.activities
|
|
12
|
+
* - Global activities defined under contract.activities
|
|
13
|
+
*
|
|
14
|
+
* @param contract - The contract definition
|
|
15
|
+
* @param workflowName - The name of the workflow
|
|
16
|
+
* @returns Activity definitions for the workflow (workflow-specific + global activities merged)
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* const orderWorkflowActivities = getWorkflowActivities(myContract, 'processOrder');
|
|
21
|
+
* // Returns: { processPayment: ActivityDef, reserveInventory: ActivityDef, sendEmail: ActivityDef }
|
|
22
|
+
* // where sendEmail is a global activity
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
declare function getWorkflowActivities<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"]>(contract: TContract, workflowName: TWorkflowName): Record<string, ActivityDefinition>;
|
|
26
|
+
/**
|
|
27
|
+
* Extract all activity names for a specific workflow from a contract
|
|
28
|
+
*
|
|
29
|
+
* @param contract - The contract definition
|
|
30
|
+
* @param workflowName - The name of the workflow
|
|
31
|
+
* @returns Array of activity names (strings) available for the workflow
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* const activityNames = getWorkflowActivityNames(myContract, 'processOrder');
|
|
36
|
+
* // Returns: ['processPayment', 'reserveInventory', 'sendEmail']
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
declare function getWorkflowActivityNames<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"]>(contract: TContract, workflowName: TWorkflowName): string[];
|
|
40
|
+
/**
|
|
41
|
+
* Check if an activity belongs to a specific workflow
|
|
42
|
+
*
|
|
43
|
+
* @param contract - The contract definition
|
|
44
|
+
* @param workflowName - The name of the workflow
|
|
45
|
+
* @param activityName - The name of the activity to check
|
|
46
|
+
* @returns True if the activity is available for the workflow, false otherwise
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* if (isWorkflowActivity(myContract, 'processOrder', 'processPayment')) {
|
|
51
|
+
* // Activity is available for this workflow
|
|
52
|
+
* }
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
declare function isWorkflowActivity<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"]>(contract: TContract, workflowName: TWorkflowName, activityName: string): boolean;
|
|
56
|
+
/**
|
|
57
|
+
* Get all workflow names from a contract
|
|
58
|
+
*
|
|
59
|
+
* @param contract - The contract definition
|
|
60
|
+
* @returns Array of workflow names defined in the contract
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* const workflows = getWorkflowNames(myContract);
|
|
65
|
+
* // Returns: ['processOrder', 'processRefund']
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
declare function getWorkflowNames<TContract extends ContractDefinition>(contract: TContract): Array<keyof TContract["workflows"]>;
|
|
69
|
+
//#endregion
|
|
70
|
+
//#region src/activity.d.ts
|
|
71
|
+
/**
|
|
72
|
+
* Activity error class that should be used to wrap all technical exceptions
|
|
73
|
+
* Forces proper error handling and enables retry policies
|
|
74
|
+
*/
|
|
75
|
+
declare class ActivityError extends Error {
|
|
76
|
+
readonly code: string;
|
|
77
|
+
constructor(code: string, message: string, cause?: unknown);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Activity implementation using Future/Result pattern
|
|
81
|
+
*
|
|
82
|
+
* Returns Future<Result<Output, ActivityError>> for explicit error handling instead of throwing exceptions.
|
|
83
|
+
* All errors must be wrapped in ActivityError to enable proper retry policies.
|
|
84
|
+
*/
|
|
85
|
+
type BoxedActivityImplementation<TActivity extends ActivityDefinition> = (args: WorkerInferInput<TActivity>) => Future<Result<WorkerInferOutput<TActivity>, ActivityError>>;
|
|
86
|
+
/**
|
|
87
|
+
* Map of all activity implementations for a contract (global + all workflow-specific)
|
|
88
|
+
*/
|
|
89
|
+
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"]> : {} };
|
|
90
|
+
type BoxedActivitiesImplementations<TActivities extends Record<string, ActivityDefinition>> = { [K in keyof TActivities]: BoxedActivityImplementation<TActivities[K]> };
|
|
91
|
+
/**
|
|
92
|
+
* Options for creating activities handler
|
|
93
|
+
*/
|
|
94
|
+
interface DeclareActivitiesHandlerOptions<TContract extends ContractDefinition> {
|
|
95
|
+
contract: TContract;
|
|
96
|
+
activities: ContractBoxedActivitiesImplementations<TContract>;
|
|
97
|
+
}
|
|
98
|
+
type ActivityImplementation<TActivity extends ActivityDefinition> = (args: WorkerInferInput<TActivity>) => Promise<WorkerInferOutput<TActivity>>;
|
|
99
|
+
type ActivitiesImplementations<TActivities extends Record<string, ActivityDefinition>> = { [K in keyof TActivities]: ActivityImplementation<TActivities[K]> };
|
|
100
|
+
type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
|
|
101
|
+
/**
|
|
102
|
+
* Activities handler ready for Temporal Worker
|
|
103
|
+
*
|
|
104
|
+
* Flat structure: all activities (global + all workflow-specific) are at the root level
|
|
105
|
+
*/
|
|
106
|
+
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"]]>;
|
|
107
|
+
/**
|
|
108
|
+
* Create a typed activities handler with automatic validation and Result pattern
|
|
109
|
+
*
|
|
110
|
+
* This wraps all activity implementations with:
|
|
111
|
+
* - Validation at network boundaries
|
|
112
|
+
* - Result<T, ActivityError> pattern for explicit error handling
|
|
113
|
+
* - Automatic conversion from Result to Promise (throwing on Error)
|
|
114
|
+
*
|
|
115
|
+
* TypeScript ensures ALL activities (global + workflow-specific) are implemented.
|
|
116
|
+
*
|
|
117
|
+
* Use this to create the activities object for the Temporal Worker.
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```ts
|
|
121
|
+
* import { declareActivitiesHandler, ActivityError } from '@temporal-contract/worker/activity';
|
|
122
|
+
* import { Result, Future } from '@swan-io/boxed';
|
|
123
|
+
* import myContract from './contract';
|
|
124
|
+
*
|
|
125
|
+
* export const activities = declareActivitiesHandler({
|
|
126
|
+
* contract: myContract,
|
|
127
|
+
* activities: {
|
|
128
|
+
* // Activity returns Result instead of throwing
|
|
129
|
+
* // All technical exceptions must be wrapped in ActivityError for retry policies
|
|
130
|
+
* sendEmail: (args) => {
|
|
131
|
+
* return Future.make(async resolve => {
|
|
132
|
+
* try {
|
|
133
|
+
* await emailService.send(args);
|
|
134
|
+
* resolve(Result.Ok({ sent: true }));
|
|
135
|
+
* } catch (error) {
|
|
136
|
+
* // Wrap technical errors in ActivityError to enable retries
|
|
137
|
+
* resolve(Result.Error(
|
|
138
|
+
* new ActivityError(
|
|
139
|
+
* 'EMAIL_SEND_FAILED',
|
|
140
|
+
* 'Failed to send email',
|
|
141
|
+
* error // Original error as cause for debugging
|
|
142
|
+
* )
|
|
143
|
+
* ));
|
|
144
|
+
* }
|
|
145
|
+
* });
|
|
146
|
+
* },
|
|
147
|
+
* },
|
|
148
|
+
* });
|
|
149
|
+
*
|
|
150
|
+
* // Use with Temporal Worker
|
|
151
|
+
* import { Worker } from '@temporalio/worker';
|
|
152
|
+
*
|
|
153
|
+
* const worker = await Worker.create({
|
|
154
|
+
* workflowsPath: require.resolve('./workflows'),
|
|
155
|
+
* activities: activities,
|
|
156
|
+
* taskQueue: contract.taskQueue,
|
|
157
|
+
* });
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
declare function declareActivitiesHandler<TContract extends ContractDefinition>(options: DeclareActivitiesHandlerOptions<TContract>): ActivitiesHandler<TContract>;
|
|
161
|
+
//#endregion
|
|
162
|
+
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,162 @@
|
|
|
1
|
+
import { g as WorkerInferOutput, h as WorkerInferInput } from "./errors-C1RFkCuD.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
|
+
/**
|
|
8
|
+
* Extract activity definitions for a specific workflow from a contract
|
|
9
|
+
*
|
|
10
|
+
* This includes both:
|
|
11
|
+
* - Workflow-specific activities defined under workflow.activities
|
|
12
|
+
* - Global activities defined under contract.activities
|
|
13
|
+
*
|
|
14
|
+
* @param contract - The contract definition
|
|
15
|
+
* @param workflowName - The name of the workflow
|
|
16
|
+
* @returns Activity definitions for the workflow (workflow-specific + global activities merged)
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* const orderWorkflowActivities = getWorkflowActivities(myContract, 'processOrder');
|
|
21
|
+
* // Returns: { processPayment: ActivityDef, reserveInventory: ActivityDef, sendEmail: ActivityDef }
|
|
22
|
+
* // where sendEmail is a global activity
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
declare function getWorkflowActivities<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"]>(contract: TContract, workflowName: TWorkflowName): Record<string, ActivityDefinition>;
|
|
26
|
+
/**
|
|
27
|
+
* Extract all activity names for a specific workflow from a contract
|
|
28
|
+
*
|
|
29
|
+
* @param contract - The contract definition
|
|
30
|
+
* @param workflowName - The name of the workflow
|
|
31
|
+
* @returns Array of activity names (strings) available for the workflow
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* const activityNames = getWorkflowActivityNames(myContract, 'processOrder');
|
|
36
|
+
* // Returns: ['processPayment', 'reserveInventory', 'sendEmail']
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
declare function getWorkflowActivityNames<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"]>(contract: TContract, workflowName: TWorkflowName): string[];
|
|
40
|
+
/**
|
|
41
|
+
* Check if an activity belongs to a specific workflow
|
|
42
|
+
*
|
|
43
|
+
* @param contract - The contract definition
|
|
44
|
+
* @param workflowName - The name of the workflow
|
|
45
|
+
* @param activityName - The name of the activity to check
|
|
46
|
+
* @returns True if the activity is available for the workflow, false otherwise
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* if (isWorkflowActivity(myContract, 'processOrder', 'processPayment')) {
|
|
51
|
+
* // Activity is available for this workflow
|
|
52
|
+
* }
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
declare function isWorkflowActivity<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"]>(contract: TContract, workflowName: TWorkflowName, activityName: string): boolean;
|
|
56
|
+
/**
|
|
57
|
+
* Get all workflow names from a contract
|
|
58
|
+
*
|
|
59
|
+
* @param contract - The contract definition
|
|
60
|
+
* @returns Array of workflow names defined in the contract
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* const workflows = getWorkflowNames(myContract);
|
|
65
|
+
* // Returns: ['processOrder', 'processRefund']
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
declare function getWorkflowNames<TContract extends ContractDefinition>(contract: TContract): Array<keyof TContract["workflows"]>;
|
|
69
|
+
//#endregion
|
|
70
|
+
//#region src/activity.d.ts
|
|
71
|
+
/**
|
|
72
|
+
* Activity error class that should be used to wrap all technical exceptions
|
|
73
|
+
* Forces proper error handling and enables retry policies
|
|
74
|
+
*/
|
|
75
|
+
declare class ActivityError extends Error {
|
|
76
|
+
readonly code: string;
|
|
77
|
+
constructor(code: string, message: string, cause?: unknown);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Activity implementation using Future/Result pattern
|
|
81
|
+
*
|
|
82
|
+
* Returns Future<Result<Output, ActivityError>> for explicit error handling instead of throwing exceptions.
|
|
83
|
+
* All errors must be wrapped in ActivityError to enable proper retry policies.
|
|
84
|
+
*/
|
|
85
|
+
type BoxedActivityImplementation<TActivity extends ActivityDefinition> = (args: WorkerInferInput<TActivity>) => Future<Result<WorkerInferOutput<TActivity>, ActivityError>>;
|
|
86
|
+
/**
|
|
87
|
+
* Map of all activity implementations for a contract (global + all workflow-specific)
|
|
88
|
+
*/
|
|
89
|
+
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"]> : {} };
|
|
90
|
+
type BoxedActivitiesImplementations<TActivities extends Record<string, ActivityDefinition>> = { [K in keyof TActivities]: BoxedActivityImplementation<TActivities[K]> };
|
|
91
|
+
/**
|
|
92
|
+
* Options for creating activities handler
|
|
93
|
+
*/
|
|
94
|
+
interface DeclareActivitiesHandlerOptions<TContract extends ContractDefinition> {
|
|
95
|
+
contract: TContract;
|
|
96
|
+
activities: ContractBoxedActivitiesImplementations<TContract>;
|
|
97
|
+
}
|
|
98
|
+
type ActivityImplementation<TActivity extends ActivityDefinition> = (args: WorkerInferInput<TActivity>) => Promise<WorkerInferOutput<TActivity>>;
|
|
99
|
+
type ActivitiesImplementations<TActivities extends Record<string, ActivityDefinition>> = { [K in keyof TActivities]: ActivityImplementation<TActivities[K]> };
|
|
100
|
+
type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
|
|
101
|
+
/**
|
|
102
|
+
* Activities handler ready for Temporal Worker
|
|
103
|
+
*
|
|
104
|
+
* Flat structure: all activities (global + all workflow-specific) are at the root level
|
|
105
|
+
*/
|
|
106
|
+
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"]]>;
|
|
107
|
+
/**
|
|
108
|
+
* Create a typed activities handler with automatic validation and Result pattern
|
|
109
|
+
*
|
|
110
|
+
* This wraps all activity implementations with:
|
|
111
|
+
* - Validation at network boundaries
|
|
112
|
+
* - Result<T, ActivityError> pattern for explicit error handling
|
|
113
|
+
* - Automatic conversion from Result to Promise (throwing on Error)
|
|
114
|
+
*
|
|
115
|
+
* TypeScript ensures ALL activities (global + workflow-specific) are implemented.
|
|
116
|
+
*
|
|
117
|
+
* Use this to create the activities object for the Temporal Worker.
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```ts
|
|
121
|
+
* import { declareActivitiesHandler, ActivityError } from '@temporal-contract/worker/activity';
|
|
122
|
+
* import { Result, Future } from '@swan-io/boxed';
|
|
123
|
+
* import myContract from './contract';
|
|
124
|
+
*
|
|
125
|
+
* export const activities = declareActivitiesHandler({
|
|
126
|
+
* contract: myContract,
|
|
127
|
+
* activities: {
|
|
128
|
+
* // Activity returns Result instead of throwing
|
|
129
|
+
* // All technical exceptions must be wrapped in ActivityError for retry policies
|
|
130
|
+
* sendEmail: (args) => {
|
|
131
|
+
* return Future.make(async resolve => {
|
|
132
|
+
* try {
|
|
133
|
+
* await emailService.send(args);
|
|
134
|
+
* resolve(Result.Ok({ sent: true }));
|
|
135
|
+
* } catch (error) {
|
|
136
|
+
* // Wrap technical errors in ActivityError to enable retries
|
|
137
|
+
* resolve(Result.Error(
|
|
138
|
+
* new ActivityError(
|
|
139
|
+
* 'EMAIL_SEND_FAILED',
|
|
140
|
+
* 'Failed to send email',
|
|
141
|
+
* error // Original error as cause for debugging
|
|
142
|
+
* )
|
|
143
|
+
* ));
|
|
144
|
+
* }
|
|
145
|
+
* });
|
|
146
|
+
* },
|
|
147
|
+
* },
|
|
148
|
+
* });
|
|
149
|
+
*
|
|
150
|
+
* // Use with Temporal Worker
|
|
151
|
+
* import { Worker } from '@temporalio/worker';
|
|
152
|
+
*
|
|
153
|
+
* const worker = await Worker.create({
|
|
154
|
+
* workflowsPath: require.resolve('./workflows'),
|
|
155
|
+
* activities: activities,
|
|
156
|
+
* taskQueue: contract.taskQueue,
|
|
157
|
+
* });
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
declare function declareActivitiesHandler<TContract extends ContractDefinition>(options: DeclareActivitiesHandlerOptions<TContract>): ActivitiesHandler<TContract>;
|
|
161
|
+
//#endregion
|
|
162
|
+
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,8 +1,191 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_errors = require('./errors-DjSZg-93.cjs');
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
|
81
|
+
//#region src/activity.ts
|
|
82
|
+
/**
|
|
83
|
+
* Activity error class that should be used to wrap all technical exceptions
|
|
84
|
+
* Forces proper error handling and enables retry policies
|
|
85
|
+
*/
|
|
86
|
+
var ActivityError = class ActivityError extends Error {
|
|
87
|
+
constructor(code, message, cause) {
|
|
88
|
+
super(message, { cause });
|
|
89
|
+
this.code = code;
|
|
90
|
+
this.name = "ActivityError";
|
|
91
|
+
if (Error.captureStackTrace) Error.captureStackTrace(this, ActivityError);
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* Create a typed activities handler with automatic validation and Result pattern
|
|
96
|
+
*
|
|
97
|
+
* This wraps all activity implementations with:
|
|
98
|
+
* - Validation at network boundaries
|
|
99
|
+
* - Result<T, ActivityError> pattern for explicit error handling
|
|
100
|
+
* - Automatic conversion from Result to Promise (throwing on Error)
|
|
101
|
+
*
|
|
102
|
+
* TypeScript ensures ALL activities (global + workflow-specific) are implemented.
|
|
103
|
+
*
|
|
104
|
+
* Use this to create the activities object for the Temporal Worker.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```ts
|
|
108
|
+
* import { declareActivitiesHandler, ActivityError } from '@temporal-contract/worker/activity';
|
|
109
|
+
* import { Result, Future } from '@swan-io/boxed';
|
|
110
|
+
* import myContract from './contract';
|
|
111
|
+
*
|
|
112
|
+
* export const activities = declareActivitiesHandler({
|
|
113
|
+
* contract: myContract,
|
|
114
|
+
* activities: {
|
|
115
|
+
* // Activity returns Result instead of throwing
|
|
116
|
+
* // All technical exceptions must be wrapped in ActivityError for retry policies
|
|
117
|
+
* sendEmail: (args) => {
|
|
118
|
+
* return Future.make(async resolve => {
|
|
119
|
+
* try {
|
|
120
|
+
* await emailService.send(args);
|
|
121
|
+
* resolve(Result.Ok({ sent: true }));
|
|
122
|
+
* } catch (error) {
|
|
123
|
+
* // Wrap technical errors in ActivityError to enable retries
|
|
124
|
+
* resolve(Result.Error(
|
|
125
|
+
* new ActivityError(
|
|
126
|
+
* 'EMAIL_SEND_FAILED',
|
|
127
|
+
* 'Failed to send email',
|
|
128
|
+
* error // Original error as cause for debugging
|
|
129
|
+
* )
|
|
130
|
+
* ));
|
|
131
|
+
* }
|
|
132
|
+
* });
|
|
133
|
+
* },
|
|
134
|
+
* },
|
|
135
|
+
* });
|
|
136
|
+
*
|
|
137
|
+
* // Use with Temporal Worker
|
|
138
|
+
* import { Worker } from '@temporalio/worker';
|
|
139
|
+
*
|
|
140
|
+
* const worker = await Worker.create({
|
|
141
|
+
* workflowsPath: require.resolve('./workflows'),
|
|
142
|
+
* activities: activities,
|
|
143
|
+
* taskQueue: contract.taskQueue,
|
|
144
|
+
* });
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
147
|
+
function declareActivitiesHandler(options) {
|
|
148
|
+
const { contract, activities } = options;
|
|
149
|
+
const wrappedActivities = {};
|
|
150
|
+
function makeWrapped(activityName, activityDef, activityImpl) {
|
|
151
|
+
return async (...args) => {
|
|
152
|
+
const input = args.length === 1 ? args[0] : args;
|
|
153
|
+
const inputResult = await activityDef.input["~standard"].validate(input);
|
|
154
|
+
if (inputResult.issues) throw new require_errors.ActivityInputValidationError(activityName, inputResult.issues);
|
|
155
|
+
const result = await activityImpl(inputResult.value);
|
|
156
|
+
if (result.isOk()) {
|
|
157
|
+
const outputResult = await activityDef.output["~standard"].validate(result.value);
|
|
158
|
+
if (outputResult.issues) throw new require_errors.ActivityOutputValidationError(activityName, outputResult.issues);
|
|
159
|
+
return outputResult.value;
|
|
160
|
+
} else throw result.error;
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
if (contract.activities) for (const [activityName, impl] of Object.entries(activities)) {
|
|
164
|
+
if (contract.workflows && activityName in contract.workflows) continue;
|
|
165
|
+
const activityDef = contract.activities[activityName];
|
|
166
|
+
if (!activityDef) throw new require_errors.ActivityDefinitionNotFoundError(activityName, Object.keys(contract.activities));
|
|
167
|
+
wrappedActivities[activityName] = makeWrapped(activityName, activityDef, impl);
|
|
168
|
+
}
|
|
169
|
+
if (contract.workflows) for (const [workflowName, workflowDef] of Object.entries(contract.workflows)) {
|
|
170
|
+
const wfActivitiesImpl = activities[workflowName];
|
|
171
|
+
if (!wfActivitiesImpl) continue;
|
|
172
|
+
const wfDefs = workflowDef.activities ?? {};
|
|
173
|
+
for (const [activityName, impl] of Object.entries(wfActivitiesImpl)) {
|
|
174
|
+
const activityDef = wfDefs[activityName];
|
|
175
|
+
if (!activityDef) throw new require_errors.ActivityDefinitionNotFoundError(`${workflowName}.${activityName}`, Object.keys(wfDefs));
|
|
176
|
+
wrappedActivities[activityName] = makeWrapped(`${workflowName}.${activityName}`, activityDef, impl);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return wrappedActivities;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
//#endregion
|
|
183
|
+
exports.ActivityDefinitionNotFoundError = require_errors.ActivityDefinitionNotFoundError;
|
|
184
|
+
exports.ActivityError = ActivityError;
|
|
185
|
+
exports.ActivityInputValidationError = require_errors.ActivityInputValidationError;
|
|
186
|
+
exports.ActivityOutputValidationError = require_errors.ActivityOutputValidationError;
|
|
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,2 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { n as ActivityInputValidationError, r as ActivityOutputValidationError, t as ActivityDefinitionNotFoundError } from "./errors-BqYWpdvd.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-Csptpwgf.cjs";
|
|
3
|
+
export { ActivitiesHandler, ActivityDefinitionNotFoundError, ActivityError, ActivityInputValidationError, ActivityOutputValidationError, declareActivitiesHandler, getWorkflowActivities, getWorkflowActivityNames, getWorkflowNames, isWorkflowActivity };
|
package/dist/activity.d.mts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { n as ActivityInputValidationError, r as ActivityOutputValidationError, t as ActivityDefinitionNotFoundError } from "./errors-C1RFkCuD.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-UOnp_bSX.mjs";
|
|
3
|
+
export { ActivitiesHandler, ActivityDefinitionNotFoundError, ActivityError, ActivityInputValidationError, ActivityOutputValidationError, declareActivitiesHandler, getWorkflowActivities, getWorkflowActivityNames, getWorkflowNames, isWorkflowActivity };
|