@temporal-contract/worker 0.0.5 → 0.0.6
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.cjs +108 -7
- package/dist/activity.d.cts +98 -2
- package/dist/activity.d.mts +98 -2
- package/dist/activity.mjs +104 -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 +25 -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
|
package/dist/activity.cjs
CHANGED
|
@@ -1,8 +1,109 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_errors = require('./errors-DjSZg-93.cjs');
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+
//#region src/activity.ts
|
|
4
|
+
/**
|
|
5
|
+
* Activity error class that should be used to wrap all technical exceptions
|
|
6
|
+
* Forces proper error handling and enables retry policies
|
|
7
|
+
*/
|
|
8
|
+
var ActivityError = class ActivityError extends Error {
|
|
9
|
+
constructor(code, message, cause) {
|
|
10
|
+
super(message, { cause });
|
|
11
|
+
this.code = code;
|
|
12
|
+
this.name = "ActivityError";
|
|
13
|
+
if (Error.captureStackTrace) Error.captureStackTrace(this, ActivityError);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Create a typed activities handler with automatic validation and Result pattern
|
|
18
|
+
*
|
|
19
|
+
* This wraps all activity implementations with:
|
|
20
|
+
* - Validation at network boundaries
|
|
21
|
+
* - Result<T, ActivityError> pattern for explicit error handling
|
|
22
|
+
* - Automatic conversion from Result to Promise (throwing on Error)
|
|
23
|
+
*
|
|
24
|
+
* TypeScript ensures ALL activities (global + workflow-specific) are implemented.
|
|
25
|
+
*
|
|
26
|
+
* Use this to create the activities object for the Temporal Worker.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* import { declareActivitiesHandler, ActivityError } from '@temporal-contract/worker/activity';
|
|
31
|
+
* import { Result, Future } from '@temporal-contract/boxed';
|
|
32
|
+
* import myContract from './contract';
|
|
33
|
+
*
|
|
34
|
+
* export const activities = declareActivitiesHandler({
|
|
35
|
+
* contract: myContract,
|
|
36
|
+
* activities: {
|
|
37
|
+
* // Activity returns Result instead of throwing
|
|
38
|
+
* // All technical exceptions must be wrapped in ActivityError for retry policies
|
|
39
|
+
* sendEmail: (args) => {
|
|
40
|
+
* return Future.make(async resolve => {
|
|
41
|
+
* try {
|
|
42
|
+
* await emailService.send(args);
|
|
43
|
+
* resolve(Result.Ok({ sent: true }));
|
|
44
|
+
* } catch (error) {
|
|
45
|
+
* // Wrap technical errors in ActivityError to enable retries
|
|
46
|
+
* resolve(Result.Error(
|
|
47
|
+
* new ActivityError(
|
|
48
|
+
* 'EMAIL_SEND_FAILED',
|
|
49
|
+
* 'Failed to send email',
|
|
50
|
+
* error // Original error as cause for debugging
|
|
51
|
+
* )
|
|
52
|
+
* ));
|
|
53
|
+
* }
|
|
54
|
+
* });
|
|
55
|
+
* },
|
|
56
|
+
* },
|
|
57
|
+
* });
|
|
58
|
+
*
|
|
59
|
+
* // Use with Temporal Worker
|
|
60
|
+
* import { Worker } from '@temporalio/worker';
|
|
61
|
+
*
|
|
62
|
+
* const worker = await Worker.create({
|
|
63
|
+
* workflowsPath: require.resolve('./workflows'),
|
|
64
|
+
* activities: activities,
|
|
65
|
+
* taskQueue: contract.taskQueue,
|
|
66
|
+
* });
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
function declareActivitiesHandler(options) {
|
|
70
|
+
const { contract, activities } = options;
|
|
71
|
+
const wrappedActivities = {};
|
|
72
|
+
function makeWrapped(activityName, activityDef, activityImpl) {
|
|
73
|
+
return async (...args) => {
|
|
74
|
+
const input = args.length === 1 ? args[0] : args;
|
|
75
|
+
const inputResult = await activityDef.input["~standard"].validate(input);
|
|
76
|
+
if (inputResult.issues) throw new require_errors.ActivityInputValidationError(activityName, inputResult.issues);
|
|
77
|
+
const result = await activityImpl(inputResult.value);
|
|
78
|
+
if (result.isOk()) {
|
|
79
|
+
const outputResult = await activityDef.output["~standard"].validate(result.value);
|
|
80
|
+
if (outputResult.issues) throw new require_errors.ActivityOutputValidationError(activityName, outputResult.issues);
|
|
81
|
+
return outputResult.value;
|
|
82
|
+
} else throw result.error;
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
if (contract.activities) for (const [activityName, impl] of Object.entries(activities)) {
|
|
86
|
+
if (contract.workflows && activityName in contract.workflows) continue;
|
|
87
|
+
const activityDef = contract.activities[activityName];
|
|
88
|
+
if (!activityDef) throw new require_errors.ActivityDefinitionNotFoundError(activityName, Object.keys(contract.activities));
|
|
89
|
+
wrappedActivities[activityName] = makeWrapped(activityName, activityDef, impl);
|
|
90
|
+
}
|
|
91
|
+
if (contract.workflows) for (const [workflowName, workflowDef] of Object.entries(contract.workflows)) {
|
|
92
|
+
const wfActivitiesImpl = activities[workflowName];
|
|
93
|
+
if (!wfActivitiesImpl) continue;
|
|
94
|
+
const wfDefs = workflowDef.activities ?? {};
|
|
95
|
+
for (const [activityName, impl] of Object.entries(wfActivitiesImpl)) {
|
|
96
|
+
const activityDef = wfDefs[activityName];
|
|
97
|
+
if (!activityDef) throw new require_errors.ActivityDefinitionNotFoundError(`${workflowName}.${activityName}`, Object.keys(wfDefs));
|
|
98
|
+
wrappedActivities[activityName] = makeWrapped(`${workflowName}.${activityName}`, activityDef, impl);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return wrappedActivities;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
//#endregion
|
|
105
|
+
exports.ActivityDefinitionNotFoundError = require_errors.ActivityDefinitionNotFoundError;
|
|
106
|
+
exports.ActivityError = ActivityError;
|
|
107
|
+
exports.ActivityInputValidationError = require_errors.ActivityInputValidationError;
|
|
108
|
+
exports.ActivityOutputValidationError = require_errors.ActivityOutputValidationError;
|
|
109
|
+
exports.declareActivitiesHandler = declareActivitiesHandler;
|
package/dist/activity.d.cts
CHANGED
|
@@ -1,2 +1,98 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { g as WorkerInferOutput, h as WorkerInferInput, n as ActivityInputValidationError, r as ActivityOutputValidationError, t as ActivityDefinitionNotFoundError } from "./errors-BqYWpdvd.cjs";
|
|
2
|
+
import { ActivityDefinition, ContractDefinition } from "@temporal-contract/contract";
|
|
3
|
+
import { Future, Result } from "@temporal-contract/boxed";
|
|
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 };
|
package/dist/activity.d.mts
CHANGED
|
@@ -1,2 +1,98 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { g as WorkerInferOutput, h as WorkerInferInput, n as ActivityInputValidationError, r as ActivityOutputValidationError, t as ActivityDefinitionNotFoundError } from "./errors-C1RFkCuD.mjs";
|
|
2
|
+
import { Future, Result } from "@temporal-contract/boxed";
|
|
3
|
+
import { ActivityDefinition, ContractDefinition } from "@temporal-contract/contract";
|
|
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 };
|
package/dist/activity.mjs
CHANGED
|
@@ -1,3 +1,105 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { n as ActivityInputValidationError, r as ActivityOutputValidationError, t as ActivityDefinitionNotFoundError } from "./errors-BqVTpfcf.mjs";
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
//#region src/activity.ts
|
|
4
|
+
/**
|
|
5
|
+
* Activity error class that should be used to wrap all technical exceptions
|
|
6
|
+
* Forces proper error handling and enables retry policies
|
|
7
|
+
*/
|
|
8
|
+
var ActivityError = class ActivityError extends Error {
|
|
9
|
+
constructor(code, message, cause) {
|
|
10
|
+
super(message, { cause });
|
|
11
|
+
this.code = code;
|
|
12
|
+
this.name = "ActivityError";
|
|
13
|
+
if (Error.captureStackTrace) Error.captureStackTrace(this, ActivityError);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Create a typed activities handler with automatic validation and Result pattern
|
|
18
|
+
*
|
|
19
|
+
* This wraps all activity implementations with:
|
|
20
|
+
* - Validation at network boundaries
|
|
21
|
+
* - Result<T, ActivityError> pattern for explicit error handling
|
|
22
|
+
* - Automatic conversion from Result to Promise (throwing on Error)
|
|
23
|
+
*
|
|
24
|
+
* TypeScript ensures ALL activities (global + workflow-specific) are implemented.
|
|
25
|
+
*
|
|
26
|
+
* Use this to create the activities object for the Temporal Worker.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* import { declareActivitiesHandler, ActivityError } from '@temporal-contract/worker/activity';
|
|
31
|
+
* import { Result, Future } from '@temporal-contract/boxed';
|
|
32
|
+
* import myContract from './contract';
|
|
33
|
+
*
|
|
34
|
+
* export const activities = declareActivitiesHandler({
|
|
35
|
+
* contract: myContract,
|
|
36
|
+
* activities: {
|
|
37
|
+
* // Activity returns Result instead of throwing
|
|
38
|
+
* // All technical exceptions must be wrapped in ActivityError for retry policies
|
|
39
|
+
* sendEmail: (args) => {
|
|
40
|
+
* return Future.make(async resolve => {
|
|
41
|
+
* try {
|
|
42
|
+
* await emailService.send(args);
|
|
43
|
+
* resolve(Result.Ok({ sent: true }));
|
|
44
|
+
* } catch (error) {
|
|
45
|
+
* // Wrap technical errors in ActivityError to enable retries
|
|
46
|
+
* resolve(Result.Error(
|
|
47
|
+
* new ActivityError(
|
|
48
|
+
* 'EMAIL_SEND_FAILED',
|
|
49
|
+
* 'Failed to send email',
|
|
50
|
+
* error // Original error as cause for debugging
|
|
51
|
+
* )
|
|
52
|
+
* ));
|
|
53
|
+
* }
|
|
54
|
+
* });
|
|
55
|
+
* },
|
|
56
|
+
* },
|
|
57
|
+
* });
|
|
58
|
+
*
|
|
59
|
+
* // Use with Temporal Worker
|
|
60
|
+
* import { Worker } from '@temporalio/worker';
|
|
61
|
+
*
|
|
62
|
+
* const worker = await Worker.create({
|
|
63
|
+
* workflowsPath: require.resolve('./workflows'),
|
|
64
|
+
* activities: activities,
|
|
65
|
+
* taskQueue: contract.taskQueue,
|
|
66
|
+
* });
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
function declareActivitiesHandler(options) {
|
|
70
|
+
const { contract, activities } = options;
|
|
71
|
+
const wrappedActivities = {};
|
|
72
|
+
function makeWrapped(activityName, activityDef, activityImpl) {
|
|
73
|
+
return async (...args) => {
|
|
74
|
+
const input = args.length === 1 ? args[0] : args;
|
|
75
|
+
const inputResult = await activityDef.input["~standard"].validate(input);
|
|
76
|
+
if (inputResult.issues) throw new ActivityInputValidationError(activityName, inputResult.issues);
|
|
77
|
+
const result = await activityImpl(inputResult.value);
|
|
78
|
+
if (result.isOk()) {
|
|
79
|
+
const outputResult = await activityDef.output["~standard"].validate(result.value);
|
|
80
|
+
if (outputResult.issues) throw new ActivityOutputValidationError(activityName, outputResult.issues);
|
|
81
|
+
return outputResult.value;
|
|
82
|
+
} else throw result.error;
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
if (contract.activities) for (const [activityName, impl] of Object.entries(activities)) {
|
|
86
|
+
if (contract.workflows && activityName in contract.workflows) continue;
|
|
87
|
+
const activityDef = contract.activities[activityName];
|
|
88
|
+
if (!activityDef) throw new ActivityDefinitionNotFoundError(activityName, Object.keys(contract.activities));
|
|
89
|
+
wrappedActivities[activityName] = makeWrapped(activityName, activityDef, impl);
|
|
90
|
+
}
|
|
91
|
+
if (contract.workflows) for (const [workflowName, workflowDef] of Object.entries(contract.workflows)) {
|
|
92
|
+
const wfActivitiesImpl = activities[workflowName];
|
|
93
|
+
if (!wfActivitiesImpl) continue;
|
|
94
|
+
const wfDefs = workflowDef.activities ?? {};
|
|
95
|
+
for (const [activityName, impl] of Object.entries(wfActivitiesImpl)) {
|
|
96
|
+
const activityDef = wfDefs[activityName];
|
|
97
|
+
if (!activityDef) throw new ActivityDefinitionNotFoundError(`${workflowName}.${activityName}`, Object.keys(wfDefs));
|
|
98
|
+
wrappedActivities[activityName] = makeWrapped(`${workflowName}.${activityName}`, activityDef, impl);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return wrappedActivities;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
//#endregion
|
|
105
|
+
export { ActivityDefinitionNotFoundError, ActivityError, ActivityInputValidationError, ActivityOutputValidationError, declareActivitiesHandler };
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
//#region src/errors.ts
|
|
2
|
+
/**
|
|
3
|
+
* Base error class for worker errors
|
|
4
|
+
*/
|
|
5
|
+
var WorkerError = class extends Error {
|
|
6
|
+
constructor(message, cause) {
|
|
7
|
+
super(message, { cause });
|
|
8
|
+
this.name = "WorkerError";
|
|
9
|
+
if (Error.captureStackTrace) Error.captureStackTrace(this, this.constructor);
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Error thrown when an activity definition is not found in the contract
|
|
14
|
+
*/
|
|
15
|
+
var ActivityDefinitionNotFoundError = class extends WorkerError {
|
|
16
|
+
constructor(activityName, availableDefinitions = []) {
|
|
17
|
+
const available = availableDefinitions.length > 0 ? availableDefinitions.join(", ") : "none";
|
|
18
|
+
super(`Activity definition not found for: "${activityName}". Available activities: ${available}`);
|
|
19
|
+
this.activityName = activityName;
|
|
20
|
+
this.availableDefinitions = availableDefinitions;
|
|
21
|
+
this.name = "ActivityDefinitionNotFoundError";
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Error thrown when activity input validation fails
|
|
26
|
+
*/
|
|
27
|
+
var ActivityInputValidationError = class extends WorkerError {
|
|
28
|
+
constructor(activityName, issues) {
|
|
29
|
+
const message = issues.map((issue) => issue.message).join("; ");
|
|
30
|
+
super(`Activity "${activityName}" input validation failed: ${message}`);
|
|
31
|
+
this.activityName = activityName;
|
|
32
|
+
this.issues = issues;
|
|
33
|
+
this.name = "ActivityInputValidationError";
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Error thrown when activity output validation fails
|
|
38
|
+
*/
|
|
39
|
+
var ActivityOutputValidationError = class extends WorkerError {
|
|
40
|
+
constructor(activityName, issues) {
|
|
41
|
+
const message = issues.map((issue) => issue.message).join("; ");
|
|
42
|
+
super(`Activity "${activityName}" output validation failed: ${message}`);
|
|
43
|
+
this.activityName = activityName;
|
|
44
|
+
this.issues = issues;
|
|
45
|
+
this.name = "ActivityOutputValidationError";
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Error thrown when workflow input validation fails
|
|
50
|
+
*/
|
|
51
|
+
var WorkflowInputValidationError = class extends WorkerError {
|
|
52
|
+
constructor(workflowName, issues) {
|
|
53
|
+
const message = issues.map((issue) => issue.message).join("; ");
|
|
54
|
+
super(`Workflow "${workflowName}" input validation failed: ${message}`);
|
|
55
|
+
this.workflowName = workflowName;
|
|
56
|
+
this.issues = issues;
|
|
57
|
+
this.name = "WorkflowInputValidationError";
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Error thrown when workflow output validation fails
|
|
62
|
+
*/
|
|
63
|
+
var WorkflowOutputValidationError = class extends WorkerError {
|
|
64
|
+
constructor(workflowName, issues) {
|
|
65
|
+
const message = issues.map((issue) => issue.message).join("; ");
|
|
66
|
+
super(`Workflow "${workflowName}" output validation failed: ${message}`);
|
|
67
|
+
this.workflowName = workflowName;
|
|
68
|
+
this.issues = issues;
|
|
69
|
+
this.name = "WorkflowOutputValidationError";
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Error thrown when signal input validation fails
|
|
74
|
+
*/
|
|
75
|
+
var SignalInputValidationError = class extends WorkerError {
|
|
76
|
+
constructor(signalName, issues) {
|
|
77
|
+
const message = issues.map((issue) => issue.message).join("; ");
|
|
78
|
+
super(`Signal "${signalName}" input validation failed: ${message}`);
|
|
79
|
+
this.signalName = signalName;
|
|
80
|
+
this.issues = issues;
|
|
81
|
+
this.name = "SignalInputValidationError";
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Error thrown when query input validation fails
|
|
86
|
+
*/
|
|
87
|
+
var QueryInputValidationError = class extends WorkerError {
|
|
88
|
+
constructor(queryName, issues) {
|
|
89
|
+
const message = issues.map((issue) => issue.message).join("; ");
|
|
90
|
+
super(`Query "${queryName}" input validation failed: ${message}`);
|
|
91
|
+
this.queryName = queryName;
|
|
92
|
+
this.issues = issues;
|
|
93
|
+
this.name = "QueryInputValidationError";
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* Error thrown when query output validation fails
|
|
98
|
+
*/
|
|
99
|
+
var QueryOutputValidationError = class extends WorkerError {
|
|
100
|
+
constructor(queryName, issues) {
|
|
101
|
+
const message = issues.map((issue) => issue.message).join("; ");
|
|
102
|
+
super(`Query "${queryName}" output validation failed: ${message}`);
|
|
103
|
+
this.queryName = queryName;
|
|
104
|
+
this.issues = issues;
|
|
105
|
+
this.name = "QueryOutputValidationError";
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* Error thrown when update input validation fails
|
|
110
|
+
*/
|
|
111
|
+
var UpdateInputValidationError = class extends WorkerError {
|
|
112
|
+
constructor(updateName, issues) {
|
|
113
|
+
const message = issues.map((issue) => issue.message).join("; ");
|
|
114
|
+
super(`Update "${updateName}" input validation failed: ${message}`);
|
|
115
|
+
this.updateName = updateName;
|
|
116
|
+
this.issues = issues;
|
|
117
|
+
this.name = "UpdateInputValidationError";
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
/**
|
|
121
|
+
* Error thrown when update output validation fails
|
|
122
|
+
*/
|
|
123
|
+
var UpdateOutputValidationError = class extends WorkerError {
|
|
124
|
+
constructor(updateName, issues) {
|
|
125
|
+
const message = issues.map((issue) => issue.message).join("; ");
|
|
126
|
+
super(`Update "${updateName}" output validation failed: ${message}`);
|
|
127
|
+
this.updateName = updateName;
|
|
128
|
+
this.issues = issues;
|
|
129
|
+
this.name = "UpdateOutputValidationError";
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
/**
|
|
133
|
+
* Error thrown when a child workflow is not found in the contract
|
|
134
|
+
*/
|
|
135
|
+
var ChildWorkflowNotFoundError = class extends WorkerError {
|
|
136
|
+
constructor(workflowName, availableWorkflows = []) {
|
|
137
|
+
const available = availableWorkflows.length > 0 ? availableWorkflows.join(", ") : "none";
|
|
138
|
+
super(`Child workflow not found: "${workflowName}". Available workflows: ${available}`);
|
|
139
|
+
this.workflowName = workflowName;
|
|
140
|
+
this.availableWorkflows = availableWorkflows;
|
|
141
|
+
this.name = "ChildWorkflowNotFoundError";
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
/**
|
|
145
|
+
* Generic error for child workflow operations
|
|
146
|
+
*/
|
|
147
|
+
var ChildWorkflowError = class extends WorkerError {
|
|
148
|
+
constructor(message, cause) {
|
|
149
|
+
super(message, cause);
|
|
150
|
+
this.name = "ChildWorkflowError";
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
//#endregion
|
|
155
|
+
export { ChildWorkflowNotFoundError as a, SignalInputValidationError as c, WorkflowInputValidationError as d, WorkflowOutputValidationError as f, ChildWorkflowError as i, UpdateInputValidationError as l, ActivityInputValidationError as n, QueryInputValidationError as o, ActivityOutputValidationError as r, QueryOutputValidationError as s, ActivityDefinitionNotFoundError as t, UpdateOutputValidationError as u };
|