@temporal-contract/worker 0.2.0 → 2.0.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 +19 -23
- package/dist/activity.cjs +49 -130
- package/dist/activity.d.cts +46 -42
- package/dist/activity.d.cts.map +1 -1
- package/dist/activity.d.mts +46 -42
- package/dist/activity.d.mts.map +1 -1
- package/dist/activity.mjs +37 -118
- package/dist/activity.mjs.map +1 -1
- package/dist/{errors-4jH78z8m.d.cts → errors-BeIXtRJe.d.cts} +14 -2
- package/dist/{errors-4jH78z8m.d.cts.map → errors-BeIXtRJe.d.cts.map} +1 -1
- package/dist/{errors-CmTXZ3JW.d.mts → errors-BjNG_jUi.d.mts} +14 -2
- package/dist/{errors-CmTXZ3JW.d.mts.map → errors-BjNG_jUi.d.mts.map} +1 -1
- package/dist/internal--45IXCxX.mjs +354 -0
- package/dist/internal--45IXCxX.mjs.map +1 -0
- package/dist/internal-C8MB-kez.cjs +453 -0
- package/dist/worker.cjs +2 -4
- package/dist/worker.mjs +1 -2
- package/dist/worker.mjs.map +1 -1
- package/dist/workflow.cjs +249 -136
- package/dist/workflow.d.cts +170 -46
- package/dist/workflow.d.cts.map +1 -1
- package/dist/workflow.d.mts +170 -46
- package/dist/workflow.d.mts.map +1 -1
- package/dist/workflow.mjs +234 -121
- package/dist/workflow.mjs.map +1 -1
- package/package.json +28 -29
- package/dist/activity-utils-B3vP03_P.d.mts +0 -68
- package/dist/activity-utils-B3vP03_P.d.mts.map +0 -1
- package/dist/activity-utils-RsXOceIH.d.cts +0 -68
- package/dist/activity-utils-RsXOceIH.d.cts.map +0 -1
- package/dist/errors-Di6Ja4Rt.mjs +0 -156
- package/dist/errors-Di6Ja4Rt.mjs.map +0 -1
- package/dist/errors-DjSZg-93.cjs +0 -227
package/README.md
CHANGED
|
@@ -14,24 +14,20 @@ pnpm add @temporal-contract/worker @temporal-contract/contract @temporalio/workf
|
|
|
14
14
|
|
|
15
15
|
```typescript
|
|
16
16
|
// activities.ts
|
|
17
|
-
import { declareActivitiesHandler,
|
|
18
|
-
import {
|
|
17
|
+
import { declareActivitiesHandler, ApplicationFailure } from "@temporal-contract/worker/activity";
|
|
18
|
+
import { ResultAsync } from "neverthrow";
|
|
19
19
|
|
|
20
20
|
export const activities = declareActivitiesHandler({
|
|
21
21
|
contract: myContract,
|
|
22
22
|
activities: {
|
|
23
|
-
sendEmail: ({ to, body }) =>
|
|
24
|
-
|
|
25
|
-
.
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
),
|
|
32
|
-
)
|
|
33
|
-
.mapOk(() => ({ sent: true }));
|
|
34
|
-
},
|
|
23
|
+
sendEmail: ({ to, body }) =>
|
|
24
|
+
ResultAsync.fromPromise(emailService.send({ to, body }), (error) =>
|
|
25
|
+
ApplicationFailure.create({
|
|
26
|
+
type: "EMAIL_FAILED",
|
|
27
|
+
message: error instanceof Error ? error.message : "Failed to send email",
|
|
28
|
+
cause: error,
|
|
29
|
+
}),
|
|
30
|
+
).map(() => ({ sent: true })),
|
|
35
31
|
},
|
|
36
32
|
});
|
|
37
33
|
|
|
@@ -69,7 +65,7 @@ run().catch(console.error);
|
|
|
69
65
|
|
|
70
66
|
### Child Workflows
|
|
71
67
|
|
|
72
|
-
Execute child workflows with type-safe
|
|
68
|
+
Execute child workflows with type-safe `ResultAsync`. Supports both same-contract and cross-contract child workflows:
|
|
73
69
|
|
|
74
70
|
```typescript
|
|
75
71
|
// workflows.ts
|
|
@@ -86,10 +82,10 @@ export const parentWorkflow = declareWorkflow({
|
|
|
86
82
|
args: { amount: input.totalAmount },
|
|
87
83
|
});
|
|
88
84
|
|
|
89
|
-
childResult.match(
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
85
|
+
childResult.match(
|
|
86
|
+
(output) => console.log("Payment processed:", output),
|
|
87
|
+
(error) => console.error("Payment failed:", error),
|
|
88
|
+
);
|
|
93
89
|
|
|
94
90
|
// Execute child workflow from another contract (another worker)
|
|
95
91
|
const notificationResult = await context.executeChildWorkflow(
|
|
@@ -107,14 +103,14 @@ export const parentWorkflow = declareWorkflow({
|
|
|
107
103
|
args: { to: "user@example.com", body: "Order received" },
|
|
108
104
|
});
|
|
109
105
|
|
|
110
|
-
handleResult.match(
|
|
111
|
-
|
|
106
|
+
handleResult.match(
|
|
107
|
+
async (handle) => {
|
|
112
108
|
// Can wait for result later
|
|
113
109
|
const result = await handle.result();
|
|
114
110
|
// ...
|
|
115
111
|
},
|
|
116
|
-
|
|
117
|
-
|
|
112
|
+
(error) => console.error("Failed to start:", error),
|
|
113
|
+
);
|
|
118
114
|
|
|
119
115
|
return { success: true };
|
|
120
116
|
},
|
package/dist/activity.cjs
CHANGED
|
@@ -1,103 +1,13 @@
|
|
|
1
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value:
|
|
2
|
-
const
|
|
3
|
-
|
|
4
|
-
//#region src/activity-utils.ts
|
|
5
|
-
/**
|
|
6
|
-
* Extract activity definitions for a specific workflow from a contract
|
|
7
|
-
*
|
|
8
|
-
* This includes both:
|
|
9
|
-
* - Workflow-specific activities defined under workflow.activities
|
|
10
|
-
* - Global activities defined under contract.activities
|
|
11
|
-
*
|
|
12
|
-
* @param contract - The contract definition
|
|
13
|
-
* @param workflowName - The name of the workflow
|
|
14
|
-
* @returns Activity definitions for the workflow (workflow-specific + global activities merged)
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```ts
|
|
18
|
-
* const orderWorkflowActivities = getWorkflowActivities(myContract, 'processOrder');
|
|
19
|
-
* // Returns: { processPayment: ActivityDef, reserveInventory: ActivityDef, sendEmail: ActivityDef }
|
|
20
|
-
* // where sendEmail is a global activity
|
|
21
|
-
* ```
|
|
22
|
-
*/
|
|
23
|
-
function getWorkflowActivities(contract, workflowName) {
|
|
24
|
-
const workflowActivities = contract.workflows[workflowName]?.activities || {};
|
|
25
|
-
return {
|
|
26
|
-
...contract.activities || {},
|
|
27
|
-
...workflowActivities
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Extract all activity names for a specific workflow from a contract
|
|
32
|
-
*
|
|
33
|
-
* @param contract - The contract definition
|
|
34
|
-
* @param workflowName - The name of the workflow
|
|
35
|
-
* @returns Array of activity names (strings) available for the workflow
|
|
36
|
-
*
|
|
37
|
-
* @example
|
|
38
|
-
* ```ts
|
|
39
|
-
* const activityNames = getWorkflowActivityNames(myContract, 'processOrder');
|
|
40
|
-
* // Returns: ['processPayment', 'reserveInventory', 'sendEmail']
|
|
41
|
-
* ```
|
|
42
|
-
*/
|
|
43
|
-
function getWorkflowActivityNames(contract, workflowName) {
|
|
44
|
-
const activities = getWorkflowActivities(contract, workflowName);
|
|
45
|
-
return Object.keys(activities);
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Check if an activity belongs to a specific workflow
|
|
49
|
-
*
|
|
50
|
-
* @param contract - The contract definition
|
|
51
|
-
* @param workflowName - The name of the workflow
|
|
52
|
-
* @param activityName - The name of the activity to check
|
|
53
|
-
* @returns True if the activity is available for the workflow, false otherwise
|
|
54
|
-
*
|
|
55
|
-
* @example
|
|
56
|
-
* ```ts
|
|
57
|
-
* if (isWorkflowActivity(myContract, 'processOrder', 'processPayment')) {
|
|
58
|
-
* // Activity is available for this workflow
|
|
59
|
-
* }
|
|
60
|
-
* ```
|
|
61
|
-
*/
|
|
62
|
-
function isWorkflowActivity(contract, workflowName, activityName) {
|
|
63
|
-
return activityName in getWorkflowActivities(contract, workflowName);
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Get all workflow names from a contract
|
|
67
|
-
*
|
|
68
|
-
* @param contract - The contract definition
|
|
69
|
-
* @returns Array of workflow names defined in the contract
|
|
70
|
-
*
|
|
71
|
-
* @example
|
|
72
|
-
* ```ts
|
|
73
|
-
* const workflows = getWorkflowNames(myContract);
|
|
74
|
-
* // Returns: ['processOrder', 'processRefund']
|
|
75
|
-
* ```
|
|
76
|
-
*/
|
|
77
|
-
function getWorkflowNames(contract) {
|
|
78
|
-
return Object.keys(contract.workflows);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
//#endregion
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_internal = require("./internal-C8MB-kez.cjs");
|
|
3
|
+
let _temporalio_common = require("@temporalio/common");
|
|
82
4
|
//#region src/activity.ts
|
|
83
5
|
/**
|
|
84
|
-
*
|
|
85
|
-
* Forces proper error handling and enables retry policies
|
|
86
|
-
*/
|
|
87
|
-
var ActivityError = class ActivityError extends Error {
|
|
88
|
-
constructor(code, message, cause) {
|
|
89
|
-
super(message, { cause });
|
|
90
|
-
this.code = code;
|
|
91
|
-
this.name = "ActivityError";
|
|
92
|
-
if (Error.captureStackTrace) Error.captureStackTrace(this, ActivityError);
|
|
93
|
-
}
|
|
94
|
-
};
|
|
95
|
-
/**
|
|
96
|
-
* Create a typed activities handler with automatic validation and Result pattern
|
|
6
|
+
* Create a typed activities handler with automatic validation and Result pattern.
|
|
97
7
|
*
|
|
98
8
|
* This wraps all activity implementations with:
|
|
99
9
|
* - Validation at network boundaries
|
|
100
|
-
* -
|
|
10
|
+
* - `ResultAsync<T, ApplicationFailure>` pattern for explicit error handling
|
|
101
11
|
* - Automatic conversion from Result to Promise (throwing on Error)
|
|
102
12
|
*
|
|
103
13
|
* TypeScript ensures ALL activities (global + workflow-specific) are implemented.
|
|
@@ -106,32 +16,28 @@ var ActivityError = class ActivityError extends Error {
|
|
|
106
16
|
*
|
|
107
17
|
* @example
|
|
108
18
|
* ```ts
|
|
109
|
-
* import { declareActivitiesHandler,
|
|
110
|
-
* import {
|
|
19
|
+
* import { declareActivitiesHandler, ApplicationFailure } from '@temporal-contract/worker/activity';
|
|
20
|
+
* import { ResultAsync, errAsync, okAsync } from 'neverthrow';
|
|
111
21
|
* import myContract from './contract';
|
|
112
22
|
*
|
|
113
23
|
* export const activities = declareActivitiesHandler({
|
|
114
24
|
* contract: myContract,
|
|
115
25
|
* activities: {
|
|
116
|
-
* // Activity returns
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
*
|
|
131
|
-
* ));
|
|
132
|
-
* }
|
|
133
|
-
* });
|
|
134
|
-
* },
|
|
26
|
+
* // Activity returns ResultAsync instead of throwing.
|
|
27
|
+
* sendEmail: (args) =>
|
|
28
|
+
* ResultAsync.fromPromise(
|
|
29
|
+
* emailService.send(args),
|
|
30
|
+
* (error) =>
|
|
31
|
+
* // Wrap technical errors in ApplicationFailure. `nonRetryable`
|
|
32
|
+
* // is per-instance: set it to true on permanent failures so
|
|
33
|
+
* // Temporal stops retrying immediately.
|
|
34
|
+
* ApplicationFailure.create({
|
|
35
|
+
* type: 'EMAIL_SEND_FAILED',
|
|
36
|
+
* message: 'Failed to send email',
|
|
37
|
+
* nonRetryable: false,
|
|
38
|
+
* cause: error instanceof Error ? error : undefined,
|
|
39
|
+
* }),
|
|
40
|
+
* ).map(() => ({ sent: true })),
|
|
135
41
|
* },
|
|
136
42
|
* });
|
|
137
43
|
*
|
|
@@ -144,19 +50,32 @@ var ActivityError = class ActivityError extends Error {
|
|
|
144
50
|
* taskQueue: contract.taskQueue,
|
|
145
51
|
* });
|
|
146
52
|
* ```
|
|
53
|
+
*
|
|
54
|
+
* @remarks
|
|
55
|
+
* The wrapper accepts implementations in the
|
|
56
|
+
* `ResultAsync<T, ApplicationFailure>` shape and produces ordinary
|
|
57
|
+
* Promise-returning Temporal handlers (`err(...)` → thrown
|
|
58
|
+
* `ApplicationFailure`; `ok(...)` → output validated against the
|
|
59
|
+
* contract and resolved). It does **not** hide Temporal's
|
|
60
|
+
* `@temporalio/activity` runtime: inside the body you can still call
|
|
61
|
+
* `Context.current()` from `@temporalio/activity` to access heartbeats
|
|
62
|
+
* (`heartbeat(details)`, `heartbeatDetails`), activity info (attempt
|
|
63
|
+
* number, workflow IDs), and the async-completion task token. See the
|
|
64
|
+
* "Working with the Activity Context" section of the worker
|
|
65
|
+
* implementation guide for end-to-end examples.
|
|
147
66
|
*/
|
|
148
67
|
function declareActivitiesHandler(options) {
|
|
149
68
|
const { contract, activities } = options;
|
|
150
69
|
const wrappedActivities = {};
|
|
151
70
|
function makeWrapped(activityName, activityDef, activityImpl) {
|
|
152
71
|
return async (...args) => {
|
|
153
|
-
const input =
|
|
72
|
+
const input = require_internal.extractHandlerInput(args);
|
|
154
73
|
const inputResult = await activityDef.input["~standard"].validate(input);
|
|
155
|
-
if (inputResult.issues) throw new
|
|
74
|
+
if (inputResult.issues) throw new require_internal.ActivityInputValidationError(activityName, inputResult.issues);
|
|
156
75
|
const result = await activityImpl(inputResult.value);
|
|
157
76
|
if (result.isOk()) {
|
|
158
77
|
const outputResult = await activityDef.output["~standard"].validate(result.value);
|
|
159
|
-
if (outputResult.issues) throw new
|
|
78
|
+
if (outputResult.issues) throw new require_internal.ActivityOutputValidationError(activityName, outputResult.issues);
|
|
160
79
|
return outputResult.value;
|
|
161
80
|
} else throw result.error;
|
|
162
81
|
};
|
|
@@ -164,7 +83,7 @@ function declareActivitiesHandler(options) {
|
|
|
164
83
|
if (contract.activities) for (const [activityName, impl] of Object.entries(activities)) {
|
|
165
84
|
if (contract.workflows && activityName in contract.workflows) continue;
|
|
166
85
|
const activityDef = contract.activities[activityName];
|
|
167
|
-
if (!activityDef) throw new
|
|
86
|
+
if (!activityDef) throw new require_internal.ActivityDefinitionNotFoundError(activityName, Object.keys(contract.activities));
|
|
168
87
|
wrappedActivities[activityName] = makeWrapped(activityName, activityDef, impl);
|
|
169
88
|
}
|
|
170
89
|
if (contract.workflows) for (const [workflowName, workflowDef] of Object.entries(contract.workflows)) {
|
|
@@ -173,20 +92,20 @@ function declareActivitiesHandler(options) {
|
|
|
173
92
|
const wfDefs = workflowDef.activities ?? {};
|
|
174
93
|
for (const [activityName, impl] of Object.entries(wfActivitiesImpl)) {
|
|
175
94
|
const activityDef = wfDefs[activityName];
|
|
176
|
-
if (!activityDef) throw new
|
|
95
|
+
if (!activityDef) throw new require_internal.ActivityDefinitionNotFoundError(`${workflowName}.${activityName}`, Object.keys(wfDefs));
|
|
177
96
|
wrappedActivities[activityName] = makeWrapped(`${workflowName}.${activityName}`, activityDef, impl);
|
|
178
97
|
}
|
|
179
98
|
}
|
|
180
99
|
return wrappedActivities;
|
|
181
100
|
}
|
|
182
|
-
|
|
183
101
|
//#endregion
|
|
184
|
-
exports.ActivityDefinitionNotFoundError =
|
|
185
|
-
exports.
|
|
186
|
-
exports.
|
|
187
|
-
exports
|
|
102
|
+
exports.ActivityDefinitionNotFoundError = require_internal.ActivityDefinitionNotFoundError;
|
|
103
|
+
exports.ActivityInputValidationError = require_internal.ActivityInputValidationError;
|
|
104
|
+
exports.ActivityOutputValidationError = require_internal.ActivityOutputValidationError;
|
|
105
|
+
Object.defineProperty(exports, "ApplicationFailure", {
|
|
106
|
+
enumerable: true,
|
|
107
|
+
get: function() {
|
|
108
|
+
return _temporalio_common.ApplicationFailure;
|
|
109
|
+
}
|
|
110
|
+
});
|
|
188
111
|
exports.declareActivitiesHandler = declareActivitiesHandler;
|
|
189
|
-
exports.getWorkflowActivities = getWorkflowActivities;
|
|
190
|
-
exports.getWorkflowActivityNames = getWorkflowActivityNames;
|
|
191
|
-
exports.getWorkflowNames = getWorkflowNames;
|
|
192
|
-
exports.isWorkflowActivity = isWorkflowActivity;
|
package/dist/activity.d.cts
CHANGED
|
@@ -1,35 +1,30 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { i as isWorkflowActivity, n as getWorkflowActivityNames, r as getWorkflowNames, t as getWorkflowActivities } from "./activity-utils-RsXOceIH.cjs";
|
|
1
|
+
import { _ as WorkerInferOutput, g as WorkerInferInput, n as ActivityInputValidationError, r as ActivityOutputValidationError, t as ActivityDefinitionNotFoundError } from "./errors-BeIXtRJe.cjs";
|
|
3
2
|
import { ActivityDefinition, ContractDefinition } from "@temporal-contract/contract";
|
|
4
|
-
import {
|
|
3
|
+
import { ResultAsync } from "neverthrow";
|
|
4
|
+
import { ApplicationFailure, ApplicationFailure as ApplicationFailure$1 } from "@temporalio/common";
|
|
5
5
|
|
|
6
6
|
//#region src/activity.d.ts
|
|
7
7
|
/**
|
|
8
|
-
* Activity
|
|
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
|
|
8
|
+
* Activity implementation using neverthrow's `ResultAsync`.
|
|
17
9
|
*
|
|
18
|
-
* Returns
|
|
19
|
-
*
|
|
10
|
+
* Returns `ResultAsync<Output, ApplicationFailure>` for explicit error
|
|
11
|
+
* handling instead of throwing. The wrapper rethrows `err()` payloads at
|
|
12
|
+
* the activity boundary; Temporal recognizes `ApplicationFailure` natively
|
|
13
|
+
* and applies the configured retry policy (with `nonRetryable: true`
|
|
14
|
+
* opting an instance out per-call).
|
|
20
15
|
*/
|
|
21
|
-
type
|
|
16
|
+
type ResultActivityImplementation<TActivity extends ActivityDefinition> = (args: WorkerInferInput<TActivity>) => ResultAsync<WorkerInferOutput<TActivity>, ApplicationFailure$1>;
|
|
22
17
|
/**
|
|
23
18
|
* Map of all activity implementations for a contract (global + all workflow-specific)
|
|
24
19
|
*/
|
|
25
|
-
type
|
|
26
|
-
type
|
|
20
|
+
type ContractResultActivitiesImplementations<TContract extends ContractDefinition> = (TContract["activities"] extends Record<string, ActivityDefinition> ? ResultActivitiesImplementations<TContract["activities"]> : {}) & { [TWorkflow in keyof TContract["workflows"]]: TContract["workflows"][TWorkflow]["activities"] extends Record<string, ActivityDefinition> ? ResultActivitiesImplementations<TContract["workflows"][TWorkflow]["activities"]> : {} };
|
|
21
|
+
type ResultActivitiesImplementations<TActivities extends Record<string, ActivityDefinition>> = { [K in keyof TActivities]: ResultActivityImplementation<TActivities[K]> };
|
|
27
22
|
/**
|
|
28
23
|
* Options for creating activities handler
|
|
29
24
|
*/
|
|
30
25
|
type DeclareActivitiesHandlerOptions<TContract extends ContractDefinition> = {
|
|
31
26
|
contract: TContract;
|
|
32
|
-
activities:
|
|
27
|
+
activities: ContractResultActivitiesImplementations<TContract>;
|
|
33
28
|
};
|
|
34
29
|
type ActivityImplementation<TActivity extends ActivityDefinition> = (args: WorkerInferInput<TActivity>) => Promise<WorkerInferOutput<TActivity>>;
|
|
35
30
|
type ActivitiesImplementations<TActivities extends Record<string, ActivityDefinition>> = { [K in keyof TActivities]: ActivityImplementation<TActivities[K]> };
|
|
@@ -41,11 +36,11 @@ type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) exten
|
|
|
41
36
|
*/
|
|
42
37
|
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
38
|
/**
|
|
44
|
-
* Create a typed activities handler with automatic validation and Result pattern
|
|
39
|
+
* Create a typed activities handler with automatic validation and Result pattern.
|
|
45
40
|
*
|
|
46
41
|
* This wraps all activity implementations with:
|
|
47
42
|
* - Validation at network boundaries
|
|
48
|
-
* -
|
|
43
|
+
* - `ResultAsync<T, ApplicationFailure>` pattern for explicit error handling
|
|
49
44
|
* - Automatic conversion from Result to Promise (throwing on Error)
|
|
50
45
|
*
|
|
51
46
|
* TypeScript ensures ALL activities (global + workflow-specific) are implemented.
|
|
@@ -54,32 +49,28 @@ type ActivitiesHandler<TContract extends ContractDefinition> = (TContract["activ
|
|
|
54
49
|
*
|
|
55
50
|
* @example
|
|
56
51
|
* ```ts
|
|
57
|
-
* import { declareActivitiesHandler,
|
|
58
|
-
* import {
|
|
52
|
+
* import { declareActivitiesHandler, ApplicationFailure } from '@temporal-contract/worker/activity';
|
|
53
|
+
* import { ResultAsync, errAsync, okAsync } from 'neverthrow';
|
|
59
54
|
* import myContract from './contract';
|
|
60
55
|
*
|
|
61
56
|
* export const activities = declareActivitiesHandler({
|
|
62
57
|
* contract: myContract,
|
|
63
58
|
* activities: {
|
|
64
|
-
* // Activity returns
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
* ));
|
|
80
|
-
* }
|
|
81
|
-
* });
|
|
82
|
-
* },
|
|
59
|
+
* // Activity returns ResultAsync instead of throwing.
|
|
60
|
+
* sendEmail: (args) =>
|
|
61
|
+
* ResultAsync.fromPromise(
|
|
62
|
+
* emailService.send(args),
|
|
63
|
+
* (error) =>
|
|
64
|
+
* // Wrap technical errors in ApplicationFailure. `nonRetryable`
|
|
65
|
+
* // is per-instance: set it to true on permanent failures so
|
|
66
|
+
* // Temporal stops retrying immediately.
|
|
67
|
+
* ApplicationFailure.create({
|
|
68
|
+
* type: 'EMAIL_SEND_FAILED',
|
|
69
|
+
* message: 'Failed to send email',
|
|
70
|
+
* nonRetryable: false,
|
|
71
|
+
* cause: error instanceof Error ? error : undefined,
|
|
72
|
+
* }),
|
|
73
|
+
* ).map(() => ({ sent: true })),
|
|
83
74
|
* },
|
|
84
75
|
* });
|
|
85
76
|
*
|
|
@@ -92,8 +83,21 @@ type ActivitiesHandler<TContract extends ContractDefinition> = (TContract["activ
|
|
|
92
83
|
* taskQueue: contract.taskQueue,
|
|
93
84
|
* });
|
|
94
85
|
* ```
|
|
86
|
+
*
|
|
87
|
+
* @remarks
|
|
88
|
+
* The wrapper accepts implementations in the
|
|
89
|
+
* `ResultAsync<T, ApplicationFailure>` shape and produces ordinary
|
|
90
|
+
* Promise-returning Temporal handlers (`err(...)` → thrown
|
|
91
|
+
* `ApplicationFailure`; `ok(...)` → output validated against the
|
|
92
|
+
* contract and resolved). It does **not** hide Temporal's
|
|
93
|
+
* `@temporalio/activity` runtime: inside the body you can still call
|
|
94
|
+
* `Context.current()` from `@temporalio/activity` to access heartbeats
|
|
95
|
+
* (`heartbeat(details)`, `heartbeatDetails`), activity info (attempt
|
|
96
|
+
* number, workflow IDs), and the async-completion task token. See the
|
|
97
|
+
* "Working with the Activity Context" section of the worker
|
|
98
|
+
* implementation guide for end-to-end examples.
|
|
95
99
|
*/
|
|
96
100
|
declare function declareActivitiesHandler<TContract extends ContractDefinition>(options: DeclareActivitiesHandlerOptions<TContract>): ActivitiesHandler<TContract>;
|
|
97
101
|
//#endregion
|
|
98
|
-
export { ActivitiesHandler, ActivityDefinitionNotFoundError,
|
|
102
|
+
export { ActivitiesHandler, ActivityDefinitionNotFoundError, ActivityInputValidationError, ActivityOutputValidationError, ApplicationFailure, declareActivitiesHandler };
|
|
99
103
|
//# sourceMappingURL=activity.d.cts.map
|
package/dist/activity.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"activity.d.cts","names":[],"sources":["../src/activity.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"activity.d.cts","names":[],"sources":["../src/activity.ts"],"mappings":";;;;;;AA+BwD;;;;;;;;;AAAA,KAWnD,4BAAA,mBAA+C,kBAAA,KAClD,IAAA,EAAM,gBAAA,CAAiB,SAAA,MACpB,WAAA,CAAY,iBAAA,CAAkB,SAAA,GAAY,oBAAA;;;;KAK1C,uCAAA,mBAA0D,kBAAA,KAE5D,SAAA,uBAAgC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as ActivityInputValidationError, r as ActivityOutputValidationError, t as ActivityDefinitionNotFoundError } from "./errors-BjNG_jUi.mjs";
|
|
2
|
+
import { ApplicationFailure, ApplicationFailure as ApplicationFailure$1 } from "@temporalio/common";
|
|
3
|
+
import { ResultAsync } from "neverthrow";
|
|
3
4
|
import { ActivityDefinition, ContractDefinition } from "@temporal-contract/contract";
|
|
4
|
-
import { Future, Result } from "@swan-io/boxed";
|
|
5
5
|
|
|
6
6
|
//#region src/activity.d.ts
|
|
7
7
|
/**
|
|
8
|
-
* Activity
|
|
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
|
|
8
|
+
* Activity implementation using neverthrow's `ResultAsync`.
|
|
17
9
|
*
|
|
18
|
-
* Returns
|
|
19
|
-
*
|
|
10
|
+
* Returns `ResultAsync<Output, ApplicationFailure>` for explicit error
|
|
11
|
+
* handling instead of throwing. The wrapper rethrows `err()` payloads at
|
|
12
|
+
* the activity boundary; Temporal recognizes `ApplicationFailure` natively
|
|
13
|
+
* and applies the configured retry policy (with `nonRetryable: true`
|
|
14
|
+
* opting an instance out per-call).
|
|
20
15
|
*/
|
|
21
|
-
type
|
|
16
|
+
type ResultActivityImplementation<TActivity extends ActivityDefinition> = (args: WorkerInferInput<TActivity>) => ResultAsync<WorkerInferOutput<TActivity>, ApplicationFailure$1>;
|
|
22
17
|
/**
|
|
23
18
|
* Map of all activity implementations for a contract (global + all workflow-specific)
|
|
24
19
|
*/
|
|
25
|
-
type
|
|
26
|
-
type
|
|
20
|
+
type ContractResultActivitiesImplementations<TContract extends ContractDefinition> = (TContract["activities"] extends Record<string, ActivityDefinition> ? ResultActivitiesImplementations<TContract["activities"]> : {}) & { [TWorkflow in keyof TContract["workflows"]]: TContract["workflows"][TWorkflow]["activities"] extends Record<string, ActivityDefinition> ? ResultActivitiesImplementations<TContract["workflows"][TWorkflow]["activities"]> : {} };
|
|
21
|
+
type ResultActivitiesImplementations<TActivities extends Record<string, ActivityDefinition>> = { [K in keyof TActivities]: ResultActivityImplementation<TActivities[K]> };
|
|
27
22
|
/**
|
|
28
23
|
* Options for creating activities handler
|
|
29
24
|
*/
|
|
30
25
|
type DeclareActivitiesHandlerOptions<TContract extends ContractDefinition> = {
|
|
31
26
|
contract: TContract;
|
|
32
|
-
activities:
|
|
27
|
+
activities: ContractResultActivitiesImplementations<TContract>;
|
|
33
28
|
};
|
|
34
29
|
type ActivityImplementation<TActivity extends ActivityDefinition> = (args: WorkerInferInput<TActivity>) => Promise<WorkerInferOutput<TActivity>>;
|
|
35
30
|
type ActivitiesImplementations<TActivities extends Record<string, ActivityDefinition>> = { [K in keyof TActivities]: ActivityImplementation<TActivities[K]> };
|
|
@@ -41,11 +36,11 @@ type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) exten
|
|
|
41
36
|
*/
|
|
42
37
|
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
38
|
/**
|
|
44
|
-
* Create a typed activities handler with automatic validation and Result pattern
|
|
39
|
+
* Create a typed activities handler with automatic validation and Result pattern.
|
|
45
40
|
*
|
|
46
41
|
* This wraps all activity implementations with:
|
|
47
42
|
* - Validation at network boundaries
|
|
48
|
-
* -
|
|
43
|
+
* - `ResultAsync<T, ApplicationFailure>` pattern for explicit error handling
|
|
49
44
|
* - Automatic conversion from Result to Promise (throwing on Error)
|
|
50
45
|
*
|
|
51
46
|
* TypeScript ensures ALL activities (global + workflow-specific) are implemented.
|
|
@@ -54,32 +49,28 @@ type ActivitiesHandler<TContract extends ContractDefinition> = (TContract["activ
|
|
|
54
49
|
*
|
|
55
50
|
* @example
|
|
56
51
|
* ```ts
|
|
57
|
-
* import { declareActivitiesHandler,
|
|
58
|
-
* import {
|
|
52
|
+
* import { declareActivitiesHandler, ApplicationFailure } from '@temporal-contract/worker/activity';
|
|
53
|
+
* import { ResultAsync, errAsync, okAsync } from 'neverthrow';
|
|
59
54
|
* import myContract from './contract';
|
|
60
55
|
*
|
|
61
56
|
* export const activities = declareActivitiesHandler({
|
|
62
57
|
* contract: myContract,
|
|
63
58
|
* activities: {
|
|
64
|
-
* // Activity returns
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
* ));
|
|
80
|
-
* }
|
|
81
|
-
* });
|
|
82
|
-
* },
|
|
59
|
+
* // Activity returns ResultAsync instead of throwing.
|
|
60
|
+
* sendEmail: (args) =>
|
|
61
|
+
* ResultAsync.fromPromise(
|
|
62
|
+
* emailService.send(args),
|
|
63
|
+
* (error) =>
|
|
64
|
+
* // Wrap technical errors in ApplicationFailure. `nonRetryable`
|
|
65
|
+
* // is per-instance: set it to true on permanent failures so
|
|
66
|
+
* // Temporal stops retrying immediately.
|
|
67
|
+
* ApplicationFailure.create({
|
|
68
|
+
* type: 'EMAIL_SEND_FAILED',
|
|
69
|
+
* message: 'Failed to send email',
|
|
70
|
+
* nonRetryable: false,
|
|
71
|
+
* cause: error instanceof Error ? error : undefined,
|
|
72
|
+
* }),
|
|
73
|
+
* ).map(() => ({ sent: true })),
|
|
83
74
|
* },
|
|
84
75
|
* });
|
|
85
76
|
*
|
|
@@ -92,8 +83,21 @@ type ActivitiesHandler<TContract extends ContractDefinition> = (TContract["activ
|
|
|
92
83
|
* taskQueue: contract.taskQueue,
|
|
93
84
|
* });
|
|
94
85
|
* ```
|
|
86
|
+
*
|
|
87
|
+
* @remarks
|
|
88
|
+
* The wrapper accepts implementations in the
|
|
89
|
+
* `ResultAsync<T, ApplicationFailure>` shape and produces ordinary
|
|
90
|
+
* Promise-returning Temporal handlers (`err(...)` → thrown
|
|
91
|
+
* `ApplicationFailure`; `ok(...)` → output validated against the
|
|
92
|
+
* contract and resolved). It does **not** hide Temporal's
|
|
93
|
+
* `@temporalio/activity` runtime: inside the body you can still call
|
|
94
|
+
* `Context.current()` from `@temporalio/activity` to access heartbeats
|
|
95
|
+
* (`heartbeat(details)`, `heartbeatDetails`), activity info (attempt
|
|
96
|
+
* number, workflow IDs), and the async-completion task token. See the
|
|
97
|
+
* "Working with the Activity Context" section of the worker
|
|
98
|
+
* implementation guide for end-to-end examples.
|
|
95
99
|
*/
|
|
96
100
|
declare function declareActivitiesHandler<TContract extends ContractDefinition>(options: DeclareActivitiesHandlerOptions<TContract>): ActivitiesHandler<TContract>;
|
|
97
101
|
//#endregion
|
|
98
|
-
export { ActivitiesHandler, ActivityDefinitionNotFoundError,
|
|
102
|
+
export { ActivitiesHandler, ActivityDefinitionNotFoundError, ActivityInputValidationError, ActivityOutputValidationError, ApplicationFailure, declareActivitiesHandler };
|
|
99
103
|
//# sourceMappingURL=activity.d.mts.map
|
package/dist/activity.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"activity.d.mts","names":[],"sources":["../src/activity.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"activity.d.mts","names":[],"sources":["../src/activity.ts"],"mappings":";;;;;;AA+BwD;;;;;;;;;AAAA,KAWnD,4BAAA,mBAA+C,kBAAA,KAClD,IAAA,EAAM,gBAAA,CAAiB,SAAA,MACpB,WAAA,CAAY,iBAAA,CAAkB,SAAA,GAAY,oBAAA;;;;KAK1C,uCAAA,mBAA0D,kB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