@temporal-contract/worker 1.0.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -21
- package/dist/activity.cjs +18 -24
- package/dist/activity.d.cts +29 -35
- package/dist/activity.d.cts.map +1 -1
- package/dist/activity.d.mts +30 -36
- package/dist/activity.d.mts.map +1 -1
- package/dist/activity.mjs +18 -24
- package/dist/activity.mjs.map +1 -1
- package/dist/{errors-CG1y7SHO.d.cts → errors-DTq5OTwH.d.cts} +69 -10
- package/dist/{errors-CG1y7SHO.d.cts.map → errors-DTq5OTwH.d.cts.map} +1 -1
- package/dist/{errors-DZhaNhwr.d.mts → errors-DbPMxULo.d.mts} +69 -10
- package/dist/{errors-DZhaNhwr.d.mts.map → errors-DbPMxULo.d.mts.map} +1 -1
- package/dist/{internal-BoNcEtYh.mjs → internal-BzG1KhEK.mjs} +132 -54
- package/dist/internal-BzG1KhEK.mjs.map +1 -0
- package/dist/{internal-Tj4m4f_K.cjs → internal-Cwch3OHR.cjs} +156 -60
- package/dist/worker.d.mts +1 -1
- package/dist/workflow.cjs +185 -133
- package/dist/workflow.d.cts +94 -81
- package/dist/workflow.d.cts.map +1 -1
- package/dist/workflow.d.mts +94 -80
- package/dist/workflow.d.mts.map +1 -1
- package/dist/workflow.mjs +183 -134
- package/dist/workflow.mjs.map +1 -1
- package/package.json +14 -14
- package/dist/internal-BoNcEtYh.mjs.map +0 -1
package/README.md
CHANGED
|
@@ -15,22 +15,19 @@ pnpm add @temporal-contract/worker @temporal-contract/contract @temporalio/workf
|
|
|
15
15
|
```typescript
|
|
16
16
|
// activities.ts
|
|
17
17
|
import { declareActivitiesHandler, ApplicationFailure } from "@temporal-contract/worker/activity";
|
|
18
|
-
import {
|
|
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
|
-
.mapOk(() => ({ sent: true }));
|
|
33
|
-
},
|
|
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 })),
|
|
34
31
|
},
|
|
35
32
|
});
|
|
36
33
|
|
|
@@ -68,7 +65,7 @@ run().catch(console.error);
|
|
|
68
65
|
|
|
69
66
|
### Child Workflows
|
|
70
67
|
|
|
71
|
-
Execute child workflows with type-safe
|
|
68
|
+
Execute child workflows with type-safe `ResultAsync`. Supports both same-contract and cross-contract child workflows:
|
|
72
69
|
|
|
73
70
|
```typescript
|
|
74
71
|
// workflows.ts
|
|
@@ -85,10 +82,10 @@ export const parentWorkflow = declareWorkflow({
|
|
|
85
82
|
args: { amount: input.totalAmount },
|
|
86
83
|
});
|
|
87
84
|
|
|
88
|
-
childResult.match(
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
85
|
+
childResult.match(
|
|
86
|
+
(output) => console.log("Payment processed:", output),
|
|
87
|
+
(error) => console.error("Payment failed:", error),
|
|
88
|
+
);
|
|
92
89
|
|
|
93
90
|
// Execute child workflow from another contract (another worker)
|
|
94
91
|
const notificationResult = await context.executeChildWorkflow(
|
|
@@ -106,14 +103,14 @@ export const parentWorkflow = declareWorkflow({
|
|
|
106
103
|
args: { to: "user@example.com", body: "Order received" },
|
|
107
104
|
});
|
|
108
105
|
|
|
109
|
-
handleResult.match(
|
|
110
|
-
|
|
106
|
+
handleResult.match(
|
|
107
|
+
async (handle) => {
|
|
111
108
|
// Can wait for result later
|
|
112
109
|
const result = await handle.result();
|
|
113
110
|
// ...
|
|
114
111
|
},
|
|
115
|
-
|
|
116
|
-
|
|
112
|
+
(error) => console.error("Failed to start:", error),
|
|
113
|
+
);
|
|
117
114
|
|
|
118
115
|
return { success: true };
|
|
119
116
|
},
|
package/dist/activity.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_internal = require("./internal-
|
|
2
|
+
const require_internal = require("./internal-Cwch3OHR.cjs");
|
|
3
3
|
let _temporalio_common = require("@temporalio/common");
|
|
4
4
|
//#region src/activity.ts
|
|
5
5
|
/**
|
|
@@ -7,7 +7,7 @@ let _temporalio_common = require("@temporalio/common");
|
|
|
7
7
|
*
|
|
8
8
|
* This wraps all activity implementations with:
|
|
9
9
|
* - Validation at network boundaries
|
|
10
|
-
* - `
|
|
10
|
+
* - `ResultAsync<T, ApplicationFailure>` pattern for explicit error handling
|
|
11
11
|
* - Automatic conversion from Result to Promise (throwing on Error)
|
|
12
12
|
*
|
|
13
13
|
* TypeScript ensures ALL activities (global + workflow-specific) are implemented.
|
|
@@ -17,33 +17,27 @@ let _temporalio_common = require("@temporalio/common");
|
|
|
17
17
|
* @example
|
|
18
18
|
* ```ts
|
|
19
19
|
* import { declareActivitiesHandler, ApplicationFailure } from '@temporal-contract/worker/activity';
|
|
20
|
-
* import {
|
|
20
|
+
* import { ResultAsync, errAsync, okAsync } from 'neverthrow';
|
|
21
21
|
* import myContract from './contract';
|
|
22
22
|
*
|
|
23
23
|
* export const activities = declareActivitiesHandler({
|
|
24
24
|
* contract: myContract,
|
|
25
25
|
* activities: {
|
|
26
|
-
* // Activity returns
|
|
27
|
-
* sendEmail: (args) =>
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
* resolve(Result.Ok({ sent: true }));
|
|
32
|
-
* } catch (error) {
|
|
26
|
+
* // Activity returns ResultAsync instead of throwing.
|
|
27
|
+
* sendEmail: (args) =>
|
|
28
|
+
* ResultAsync.fromPromise(
|
|
29
|
+
* emailService.send(args),
|
|
30
|
+
* (error) =>
|
|
33
31
|
* // Wrap technical errors in ApplicationFailure. `nonRetryable`
|
|
34
32
|
* // is per-instance: set it to true on permanent failures so
|
|
35
33
|
* // Temporal stops retrying immediately.
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
* ));
|
|
44
|
-
* }
|
|
45
|
-
* });
|
|
46
|
-
* },
|
|
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 })),
|
|
47
41
|
* },
|
|
48
42
|
* });
|
|
49
43
|
*
|
|
@@ -59,9 +53,9 @@ let _temporalio_common = require("@temporalio/common");
|
|
|
59
53
|
*
|
|
60
54
|
* @remarks
|
|
61
55
|
* The wrapper accepts implementations in the
|
|
62
|
-
* `
|
|
63
|
-
* Promise-returning Temporal handlers (
|
|
64
|
-
* `ApplicationFailure`;
|
|
56
|
+
* `ResultAsync<T, ApplicationFailure>` shape and produces ordinary
|
|
57
|
+
* Promise-returning Temporal handlers (`err(...)` → thrown
|
|
58
|
+
* `ApplicationFailure`; `ok(...)` → output validated against the
|
|
65
59
|
* contract and resolved). It does **not** hide Temporal's
|
|
66
60
|
* `@temporalio/activity` runtime: inside the body you can still call
|
|
67
61
|
* `Context.current()` from `@temporalio/activity` to access heartbeats
|
package/dist/activity.d.cts
CHANGED
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { n as ActivityInputValidationError, r as ActivityOutputValidationError, t as ActivityDefinitionNotFoundError, v as WorkerInferInput, y as WorkerInferOutput } from "./errors-DTq5OTwH.cjs";
|
|
2
2
|
import { ActivityDefinition, ContractDefinition } from "@temporal-contract/contract";
|
|
3
|
-
import {
|
|
3
|
+
import { ResultAsync } from "neverthrow";
|
|
4
4
|
import { ApplicationFailure, ApplicationFailure as ApplicationFailure$1 } from "@temporalio/common";
|
|
5
5
|
|
|
6
6
|
//#region src/activity.d.ts
|
|
7
7
|
/**
|
|
8
|
-
* Activity implementation using
|
|
8
|
+
* Activity implementation using neverthrow's `ResultAsync`.
|
|
9
9
|
*
|
|
10
|
-
* Returns `
|
|
11
|
-
* handling instead of throwing. The wrapper rethrows `
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
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).
|
|
15
15
|
*/
|
|
16
|
-
type
|
|
16
|
+
type ResultActivityImplementation<TActivity extends ActivityDefinition> = (args: WorkerInferInput<TActivity>) => ResultAsync<WorkerInferOutput<TActivity>, ApplicationFailure$1>;
|
|
17
17
|
/**
|
|
18
18
|
* Map of all activity implementations for a contract (global + all workflow-specific)
|
|
19
19
|
*/
|
|
20
|
-
type
|
|
21
|
-
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]> };
|
|
22
22
|
/**
|
|
23
23
|
* Options for creating activities handler
|
|
24
24
|
*/
|
|
25
25
|
type DeclareActivitiesHandlerOptions<TContract extends ContractDefinition> = {
|
|
26
26
|
contract: TContract;
|
|
27
|
-
activities:
|
|
27
|
+
activities: ContractResultActivitiesImplementations<TContract>;
|
|
28
28
|
};
|
|
29
29
|
type ActivityImplementation<TActivity extends ActivityDefinition> = (args: WorkerInferInput<TActivity>) => Promise<WorkerInferOutput<TActivity>>;
|
|
30
30
|
type ActivitiesImplementations<TActivities extends Record<string, ActivityDefinition>> = { [K in keyof TActivities]: ActivityImplementation<TActivities[K]> };
|
|
@@ -40,7 +40,7 @@ type ActivitiesHandler<TContract extends ContractDefinition> = (TContract["activ
|
|
|
40
40
|
*
|
|
41
41
|
* This wraps all activity implementations with:
|
|
42
42
|
* - Validation at network boundaries
|
|
43
|
-
* - `
|
|
43
|
+
* - `ResultAsync<T, ApplicationFailure>` pattern for explicit error handling
|
|
44
44
|
* - Automatic conversion from Result to Promise (throwing on Error)
|
|
45
45
|
*
|
|
46
46
|
* TypeScript ensures ALL activities (global + workflow-specific) are implemented.
|
|
@@ -50,33 +50,27 @@ type ActivitiesHandler<TContract extends ContractDefinition> = (TContract["activ
|
|
|
50
50
|
* @example
|
|
51
51
|
* ```ts
|
|
52
52
|
* import { declareActivitiesHandler, ApplicationFailure } from '@temporal-contract/worker/activity';
|
|
53
|
-
* import {
|
|
53
|
+
* import { ResultAsync, errAsync, okAsync } from 'neverthrow';
|
|
54
54
|
* import myContract from './contract';
|
|
55
55
|
*
|
|
56
56
|
* export const activities = declareActivitiesHandler({
|
|
57
57
|
* contract: myContract,
|
|
58
58
|
* activities: {
|
|
59
|
-
* // Activity returns
|
|
60
|
-
* sendEmail: (args) =>
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
* resolve(Result.Ok({ sent: true }));
|
|
65
|
-
* } catch (error) {
|
|
59
|
+
* // Activity returns ResultAsync instead of throwing.
|
|
60
|
+
* sendEmail: (args) =>
|
|
61
|
+
* ResultAsync.fromPromise(
|
|
62
|
+
* emailService.send(args),
|
|
63
|
+
* (error) =>
|
|
66
64
|
* // Wrap technical errors in ApplicationFailure. `nonRetryable`
|
|
67
65
|
* // is per-instance: set it to true on permanent failures so
|
|
68
66
|
* // Temporal stops retrying immediately.
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
* ));
|
|
77
|
-
* }
|
|
78
|
-
* });
|
|
79
|
-
* },
|
|
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 })),
|
|
80
74
|
* },
|
|
81
75
|
* });
|
|
82
76
|
*
|
|
@@ -92,9 +86,9 @@ type ActivitiesHandler<TContract extends ContractDefinition> = (TContract["activ
|
|
|
92
86
|
*
|
|
93
87
|
* @remarks
|
|
94
88
|
* The wrapper accepts implementations in the
|
|
95
|
-
* `
|
|
96
|
-
* Promise-returning Temporal handlers (
|
|
97
|
-
* `ApplicationFailure`;
|
|
89
|
+
* `ResultAsync<T, ApplicationFailure>` shape and produces ordinary
|
|
90
|
+
* Promise-returning Temporal handlers (`err(...)` → thrown
|
|
91
|
+
* `ApplicationFailure`; `ok(...)` → output validated against the
|
|
98
92
|
* contract and resolved). It does **not** hide Temporal's
|
|
99
93
|
* `@temporalio/activity` runtime: inside the body you can still call
|
|
100
94
|
* `Context.current()` from `@temporalio/activity` to access heartbeats
|
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,MAAA,SAAe,kBAAA,IAC5C,+BAAA,CAAgC,SAAA,8CAIZ,SAAA,gBAAyB,SAAA,cAAuB,SAAA,wBAAiC,MAAA,SAEnG,kBAAA,IAEE,+BAAA,CAAgC,SAAA,cAAuB,SAAA;AAAA,KAI5D,+BAAA,qBAAoD,MAAA,SAAe,kBAAA,mBAC1D,WAAA,GAAc,4BAAA,CAA6B,WAAA,CAAY,CAAA;;;;KAMhE,+BAAA,mBAAkD,kBAAA;EACrD,QAAA,EAAU,SAAA;EACV,UAAA,EAAY,uCAAA,CAAwC,SAAA;AAAA;AAAA,KAGjD,sBAAA,mBAAyC,kBAAA,KAC5C,IAAA,EAAM,gBAAA,CAAiB,SAAA,MACpB,OAAA,CAAQ,iBAAA,CAAkB,SAAA;AAAA,KAE1B,yBAAA,qBAA8C,MAAA,SAAe,kBAAA,mBACpD,WAAA,GAAc,sBAAA,CAAuB,WAAA,CAAY,CAAA;AAAA,KAG1D,mBAAA,OAA0B,CAAA,oBAAqB,CAAA,EAAG,CAAA,6BACrD,CAAA,sBAEE,CAAA;;;;;;KAQQ,iBAAA,mBAAoC,kBAAA,KAE7C,SAAA,uBAAgC,MAAA,SAAe,kBAAA,IAC5C,yBAAA,CAA0B,SAAA,wBAG5B,mBAAA,uBAEwB,SAAA,gBAAyB,SAAA,cAAuB,SAAA,wBAAiC,MAAA,SAEnG,kBAAA,IAEE,yBAAA,CAA0B,SAAA,cAAuB,SAAA,8BAE/C,SAAA;;;;;;;;;;;;;;;;;;;;;;AAjD4D;;;;;;;;;;;;;;;;;;;;;AAKJ;;;;;;;;;;;;;;;;;;AAQP;;iBAqG/C,wBAAA,mBAA2C,kBAAA,CAAA,CACzD,OAAA,EAAS,+BAAA,CAAgC,SAAA,IACxC,iBAAA,CAAkB,SAAA"}
|
package/dist/activity.d.mts
CHANGED
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { ApplicationFailure, ApplicationFailure as ApplicationFailure$1 } from "@temporalio/common";
|
|
1
|
+
import { n as ActivityInputValidationError, r as ActivityOutputValidationError, t as ActivityDefinitionNotFoundError, v as WorkerInferInput, y as WorkerInferOutput } from "./errors-DbPMxULo.mjs";
|
|
3
2
|
import { ActivityDefinition, ContractDefinition } from "@temporal-contract/contract";
|
|
4
|
-
import {
|
|
3
|
+
import { ApplicationFailure, ApplicationFailure as ApplicationFailure$1 } from "@temporalio/common";
|
|
4
|
+
import { ResultAsync } from "neverthrow";
|
|
5
5
|
|
|
6
6
|
//#region src/activity.d.ts
|
|
7
7
|
/**
|
|
8
|
-
* Activity implementation using
|
|
8
|
+
* Activity implementation using neverthrow's `ResultAsync`.
|
|
9
9
|
*
|
|
10
|
-
* Returns `
|
|
11
|
-
* handling instead of throwing. The wrapper rethrows `
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
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).
|
|
15
15
|
*/
|
|
16
|
-
type
|
|
16
|
+
type ResultActivityImplementation<TActivity extends ActivityDefinition> = (args: WorkerInferInput<TActivity>) => ResultAsync<WorkerInferOutput<TActivity>, ApplicationFailure$1>;
|
|
17
17
|
/**
|
|
18
18
|
* Map of all activity implementations for a contract (global + all workflow-specific)
|
|
19
19
|
*/
|
|
20
|
-
type
|
|
21
|
-
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]> };
|
|
22
22
|
/**
|
|
23
23
|
* Options for creating activities handler
|
|
24
24
|
*/
|
|
25
25
|
type DeclareActivitiesHandlerOptions<TContract extends ContractDefinition> = {
|
|
26
26
|
contract: TContract;
|
|
27
|
-
activities:
|
|
27
|
+
activities: ContractResultActivitiesImplementations<TContract>;
|
|
28
28
|
};
|
|
29
29
|
type ActivityImplementation<TActivity extends ActivityDefinition> = (args: WorkerInferInput<TActivity>) => Promise<WorkerInferOutput<TActivity>>;
|
|
30
30
|
type ActivitiesImplementations<TActivities extends Record<string, ActivityDefinition>> = { [K in keyof TActivities]: ActivityImplementation<TActivities[K]> };
|
|
@@ -40,7 +40,7 @@ type ActivitiesHandler<TContract extends ContractDefinition> = (TContract["activ
|
|
|
40
40
|
*
|
|
41
41
|
* This wraps all activity implementations with:
|
|
42
42
|
* - Validation at network boundaries
|
|
43
|
-
* - `
|
|
43
|
+
* - `ResultAsync<T, ApplicationFailure>` pattern for explicit error handling
|
|
44
44
|
* - Automatic conversion from Result to Promise (throwing on Error)
|
|
45
45
|
*
|
|
46
46
|
* TypeScript ensures ALL activities (global + workflow-specific) are implemented.
|
|
@@ -50,33 +50,27 @@ type ActivitiesHandler<TContract extends ContractDefinition> = (TContract["activ
|
|
|
50
50
|
* @example
|
|
51
51
|
* ```ts
|
|
52
52
|
* import { declareActivitiesHandler, ApplicationFailure } from '@temporal-contract/worker/activity';
|
|
53
|
-
* import {
|
|
53
|
+
* import { ResultAsync, errAsync, okAsync } from 'neverthrow';
|
|
54
54
|
* import myContract from './contract';
|
|
55
55
|
*
|
|
56
56
|
* export const activities = declareActivitiesHandler({
|
|
57
57
|
* contract: myContract,
|
|
58
58
|
* activities: {
|
|
59
|
-
* // Activity returns
|
|
60
|
-
* sendEmail: (args) =>
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
* resolve(Result.Ok({ sent: true }));
|
|
65
|
-
* } catch (error) {
|
|
59
|
+
* // Activity returns ResultAsync instead of throwing.
|
|
60
|
+
* sendEmail: (args) =>
|
|
61
|
+
* ResultAsync.fromPromise(
|
|
62
|
+
* emailService.send(args),
|
|
63
|
+
* (error) =>
|
|
66
64
|
* // Wrap technical errors in ApplicationFailure. `nonRetryable`
|
|
67
65
|
* // is per-instance: set it to true on permanent failures so
|
|
68
66
|
* // Temporal stops retrying immediately.
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
* ));
|
|
77
|
-
* }
|
|
78
|
-
* });
|
|
79
|
-
* },
|
|
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 })),
|
|
80
74
|
* },
|
|
81
75
|
* });
|
|
82
76
|
*
|
|
@@ -92,9 +86,9 @@ type ActivitiesHandler<TContract extends ContractDefinition> = (TContract["activ
|
|
|
92
86
|
*
|
|
93
87
|
* @remarks
|
|
94
88
|
* The wrapper accepts implementations in the
|
|
95
|
-
* `
|
|
96
|
-
* Promise-returning Temporal handlers (
|
|
97
|
-
* `ApplicationFailure`;
|
|
89
|
+
* `ResultAsync<T, ApplicationFailure>` shape and produces ordinary
|
|
90
|
+
* Promise-returning Temporal handlers (`err(...)` → thrown
|
|
91
|
+
* `ApplicationFailure`; `ok(...)` → output validated against the
|
|
98
92
|
* contract and resolved). It does **not** hide Temporal's
|
|
99
93
|
* `@temporalio/activity` runtime: inside the body you can still call
|
|
100
94
|
* `Context.current()` from `@temporalio/activity` to access heartbeats
|
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,kBAAA,KAE5D,SAAA,uBAAgC,MAAA,SAAe,kBAAA,IAC5C,+BAAA,CAAgC,SAAA,8CAIZ,SAAA,gBAAyB,SAAA,cAAuB,SAAA,wBAAiC,MAAA,SAEnG,kBAAA,IAEE,+BAAA,CAAgC,SAAA,cAAuB,SAAA;AAAA,KAI5D,+BAAA,qBAAoD,MAAA,SAAe,kBAAA,mBAC1D,WAAA,GAAc,4BAAA,CAA6B,WAAA,CAAY,CAAA;;;;KAMhE,+BAAA,mBAAkD,kBAAA;EACrD,QAAA,EAAU,SAAA;EACV,UAAA,EAAY,uCAAA,CAAwC,SAAA;AAAA;AAAA,KAGjD,sBAAA,mBAAyC,kBAAA,KAC5C,IAAA,EAAM,gBAAA,CAAiB,SAAA,MACpB,OAAA,CAAQ,iBAAA,CAAkB,SAAA;AAAA,KAE1B,yBAAA,qBAA8C,MAAA,SAAe,kBAAA,mBACpD,WAAA,GAAc,sBAAA,CAAuB,WAAA,CAAY,CAAA;AAAA,KAG1D,mBAAA,OAA0B,CAAA,oBAAqB,CAAA,EAAG,CAAA,6BACrD,CAAA,sBAEE,CAAA;;;;;;KAQQ,iBAAA,mBAAoC,kBAAA,KAE7C,SAAA,uBAAgC,MAAA,SAAe,kBAAA,IAC5C,yBAAA,CAA0B,SAAA,wBAG5B,mBAAA,uBAEwB,SAAA,gBAAyB,SAAA,cAAuB,SAAA,wBAAiC,MAAA,SAEnG,kBAAA,IAEE,yBAAA,CAA0B,SAAA,cAAuB,SAAA,8BAE/C,SAAA;;;;;;;;;;;;;;;;;;;;;;AAjD4D;;;;;;;;;;;;;;;;;;;;;AAKJ;;;;;;;;;;;;;;;;;;AAQP;;iBAqG/C,wBAAA,mBAA2C,kBAAA,CAAA,CACzD,OAAA,EAAS,+BAAA,CAAgC,SAAA,IACxC,iBAAA,CAAkB,SAAA"}
|
package/dist/activity.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { c as ActivityInputValidationError, i as extractHandlerInput, l as ActivityOutputValidationError, s as ActivityDefinitionNotFoundError } from "./internal-BzG1KhEK.mjs";
|
|
2
2
|
import { ApplicationFailure } from "@temporalio/common";
|
|
3
3
|
//#region src/activity.ts
|
|
4
4
|
/**
|
|
@@ -6,7 +6,7 @@ import { ApplicationFailure } from "@temporalio/common";
|
|
|
6
6
|
*
|
|
7
7
|
* This wraps all activity implementations with:
|
|
8
8
|
* - Validation at network boundaries
|
|
9
|
-
* - `
|
|
9
|
+
* - `ResultAsync<T, ApplicationFailure>` pattern for explicit error handling
|
|
10
10
|
* - Automatic conversion from Result to Promise (throwing on Error)
|
|
11
11
|
*
|
|
12
12
|
* TypeScript ensures ALL activities (global + workflow-specific) are implemented.
|
|
@@ -16,33 +16,27 @@ import { ApplicationFailure } from "@temporalio/common";
|
|
|
16
16
|
* @example
|
|
17
17
|
* ```ts
|
|
18
18
|
* import { declareActivitiesHandler, ApplicationFailure } from '@temporal-contract/worker/activity';
|
|
19
|
-
* import {
|
|
19
|
+
* import { ResultAsync, errAsync, okAsync } from 'neverthrow';
|
|
20
20
|
* import myContract from './contract';
|
|
21
21
|
*
|
|
22
22
|
* export const activities = declareActivitiesHandler({
|
|
23
23
|
* contract: myContract,
|
|
24
24
|
* activities: {
|
|
25
|
-
* // Activity returns
|
|
26
|
-
* sendEmail: (args) =>
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
* resolve(Result.Ok({ sent: true }));
|
|
31
|
-
* } catch (error) {
|
|
25
|
+
* // Activity returns ResultAsync instead of throwing.
|
|
26
|
+
* sendEmail: (args) =>
|
|
27
|
+
* ResultAsync.fromPromise(
|
|
28
|
+
* emailService.send(args),
|
|
29
|
+
* (error) =>
|
|
32
30
|
* // Wrap technical errors in ApplicationFailure. `nonRetryable`
|
|
33
31
|
* // is per-instance: set it to true on permanent failures so
|
|
34
32
|
* // Temporal stops retrying immediately.
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
* ));
|
|
43
|
-
* }
|
|
44
|
-
* });
|
|
45
|
-
* },
|
|
33
|
+
* ApplicationFailure.create({
|
|
34
|
+
* type: 'EMAIL_SEND_FAILED',
|
|
35
|
+
* message: 'Failed to send email',
|
|
36
|
+
* nonRetryable: false,
|
|
37
|
+
* cause: error instanceof Error ? error : undefined,
|
|
38
|
+
* }),
|
|
39
|
+
* ).map(() => ({ sent: true })),
|
|
46
40
|
* },
|
|
47
41
|
* });
|
|
48
42
|
*
|
|
@@ -58,9 +52,9 @@ import { ApplicationFailure } from "@temporalio/common";
|
|
|
58
52
|
*
|
|
59
53
|
* @remarks
|
|
60
54
|
* The wrapper accepts implementations in the
|
|
61
|
-
* `
|
|
62
|
-
* Promise-returning Temporal handlers (
|
|
63
|
-
* `ApplicationFailure`;
|
|
55
|
+
* `ResultAsync<T, ApplicationFailure>` shape and produces ordinary
|
|
56
|
+
* Promise-returning Temporal handlers (`err(...)` → thrown
|
|
57
|
+
* `ApplicationFailure`; `ok(...)` → output validated against the
|
|
64
58
|
* contract and resolved). It does **not** hide Temporal's
|
|
65
59
|
* `@temporalio/activity` runtime: inside the body you can still call
|
|
66
60
|
* `Context.current()` from `@temporalio/activity` to access heartbeats
|
package/dist/activity.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"activity.mjs","names":[],"sources":["../src/activity.ts"],"sourcesContent":["// Entry point for activity implementations.\n//\n// Activities run *outside* the workflow sandbox, so they can use any\n// implementation of the Result/Future pattern — we standardize on\n// `@swan-io/boxed` here for its richer API. Workflow code (see workflow.ts)\n// must use `@temporal-contract/boxed` instead, since it's compatible with\n// Temporal's deterministic replay machinery.\n//\n// Errors flow through Temporal's `ApplicationFailure` (re-exported from\n// `@temporalio/common`) — it's the SDK's first-class failure shape, so we\n// don't wrap it in a custom class. `ApplicationFailure` exposes\n// `nonRetryable`, `type`, `details`, and `category` natively, and survives\n// the activity → workflow serialization boundary unchanged.\nimport { ActivityDefinition, ContractDefinition } from \"@temporal-contract/contract\";\nimport { Future, Result } from \"@swan-io/boxed\";\nimport { ApplicationFailure } from \"@temporalio/common\";\nimport { WorkerInferInput, WorkerInferOutput } from \"./types.js\";\nimport {\n ActivityDefinitionNotFoundError,\n ActivityInputValidationError,\n ActivityOutputValidationError,\n} from \"./errors.js\";\nimport { extractHandlerInput } from \"./internal.js\";\n\nexport {\n ActivityDefinitionNotFoundError,\n ActivityInputValidationError,\n ActivityOutputValidationError,\n} from \"./errors.js\";\n\n// Re-export the canonical activity-failure class so consumers don't need\n// a separate `@temporalio/common` import to construct one.\nexport { ApplicationFailure } from \"@temporalio/common\";\n\n/**\n * Activity implementation using Future/Result pattern.\n *\n * Returns `Future<Result<Output, ApplicationFailure>>` for explicit error\n * handling instead of throwing. The wrapper rethrows `Result.Error`\n * payloads at the activity boundary; Temporal recognizes\n * `ApplicationFailure` natively and applies the configured retry policy\n * (with `nonRetryable: true` opting an instance out per-call).\n */\ntype BoxedActivityImplementation<TActivity extends ActivityDefinition> = (\n args: WorkerInferInput<TActivity>,\n) => Future<Result<WorkerInferOutput<TActivity>, ApplicationFailure>>;\n\n/**\n * Map of all activity implementations for a contract (global + all workflow-specific)\n */\ntype ContractBoxedActivitiesImplementations<TContract extends ContractDefinition> =\n // Global activities\n (TContract[\"activities\"] extends Record<string, ActivityDefinition>\n ? BoxedActivitiesImplementations<TContract[\"activities\"]>\n : {}) &\n // All workflow-specific activities merged\n {\n [TWorkflow in keyof TContract[\"workflows\"]]: TContract[\"workflows\"][TWorkflow][\"activities\"] extends Record<\n string,\n ActivityDefinition\n >\n ? BoxedActivitiesImplementations<TContract[\"workflows\"][TWorkflow][\"activities\"]>\n : {};\n };\n\ntype BoxedActivitiesImplementations<TActivities extends Record<string, ActivityDefinition>> = {\n [K in keyof TActivities]: BoxedActivityImplementation<TActivities[K]>;\n};\n\n/**\n * Options for creating activities handler\n */\ntype DeclareActivitiesHandlerOptions<TContract extends ContractDefinition> = {\n contract: TContract;\n activities: ContractBoxedActivitiesImplementations<TContract>;\n};\n\ntype ActivityImplementation<TActivity extends ActivityDefinition> = (\n args: WorkerInferInput<TActivity>,\n) => Promise<WorkerInferOutput<TActivity>>;\n\ntype ActivitiesImplementations<TActivities extends Record<string, ActivityDefinition>> = {\n [K in keyof TActivities]: ActivityImplementation<TActivities[K]>;\n};\n\ntype UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends (\n k: infer I,\n) => void\n ? I\n : never;\n\n/**\n * Activities handler ready for Temporal Worker\n *\n * Flat structure: all activities (global + all workflow-specific) are at the root level\n */\nexport type ActivitiesHandler<TContract extends ContractDefinition> =\n // Global activities\n (TContract[\"activities\"] extends Record<string, ActivityDefinition>\n ? ActivitiesImplementations<TContract[\"activities\"]>\n : {}) &\n // All workflow-specific activities merged at root level (flat)\n UnionToIntersection<\n {\n [TWorkflow in keyof TContract[\"workflows\"]]: TContract[\"workflows\"][TWorkflow][\"activities\"] extends Record<\n string,\n ActivityDefinition\n >\n ? ActivitiesImplementations<TContract[\"workflows\"][TWorkflow][\"activities\"]>\n : {};\n }[keyof TContract[\"workflows\"]]\n >;\n\n/**\n * Create a typed activities handler with automatic validation and Result pattern.\n *\n * This wraps all activity implementations with:\n * - Validation at network boundaries\n * - `Result<T, ApplicationFailure>` pattern for explicit error handling\n * - Automatic conversion from Result to Promise (throwing on Error)\n *\n * TypeScript ensures ALL activities (global + workflow-specific) are implemented.\n *\n * Use this to create the activities object for the Temporal Worker.\n *\n * @example\n * ```ts\n * import { declareActivitiesHandler, ApplicationFailure } from '@temporal-contract/worker/activity';\n * import { Result, Future } from '@swan-io/boxed';\n * import myContract from './contract';\n *\n * export const activities = declareActivitiesHandler({\n * contract: myContract,\n * activities: {\n * // Activity returns Result instead of throwing.\n * sendEmail: (args) => {\n * return Future.make(async (resolve) => {\n * try {\n * await emailService.send(args);\n * resolve(Result.Ok({ sent: true }));\n * } catch (error) {\n * // Wrap technical errors in ApplicationFailure. `nonRetryable`\n * // is per-instance: set it to true on permanent failures so\n * // Temporal stops retrying immediately.\n * resolve(Result.Error(\n * ApplicationFailure.create({\n * type: 'EMAIL_SEND_FAILED',\n * message: 'Failed to send email',\n * nonRetryable: false,\n * cause: error instanceof Error ? error : undefined,\n * }),\n * ));\n * }\n * });\n * },\n * },\n * });\n *\n * // Use with Temporal Worker\n * import { Worker } from '@temporalio/worker';\n *\n * const worker = await Worker.create({\n * workflowsPath: require.resolve('./workflows'),\n * activities: activities,\n * taskQueue: contract.taskQueue,\n * });\n * ```\n *\n * @remarks\n * The wrapper accepts implementations in the\n * `Future<Result<T, ApplicationFailure>>` shape and produces ordinary\n * Promise-returning Temporal handlers (Result.Error → thrown\n * `ApplicationFailure`; Result.Ok → output validated against the\n * contract and resolved). It does **not** hide Temporal's\n * `@temporalio/activity` runtime: inside the body you can still call\n * `Context.current()` from `@temporalio/activity` to access heartbeats\n * (`heartbeat(details)`, `heartbeatDetails`), activity info (attempt\n * number, workflow IDs), and the async-completion task token. See the\n * \"Working with the Activity Context\" section of the worker\n * implementation guide for end-to-end examples.\n */\nexport function declareActivitiesHandler<TContract extends ContractDefinition>(\n options: DeclareActivitiesHandlerOptions<TContract>,\n): ActivitiesHandler<TContract> {\n const { contract, activities } = options;\n\n // Prepare Temporal-compatible activities with validation and Result unwrapping\n const wrappedActivities = {} as ActivitiesHandler<TContract>;\n\n // Helper to create a wrapped implementation from a definition and impl\n function makeWrapped(\n activityName: string,\n activityDef: ActivityDefinition,\n activityImpl: (args: unknown) => Future<Result<unknown, ApplicationFailure>>,\n ) {\n return async (...args: unknown[]) => {\n const input = extractHandlerInput(args);\n\n // Validate input\n const inputResult = await activityDef.input[\"~standard\"].validate(input);\n if (inputResult.issues) {\n throw new ActivityInputValidationError(activityName, inputResult.issues);\n }\n\n // Execute boxed activity (returns Future<Result<T, ApplicationFailure>>)\n const futureResult = activityImpl(inputResult.value);\n\n // Await Future and unwrap Result\n const result = await futureResult;\n\n // Process result: validate output or throw error\n if (result.isOk()) {\n // Validate output on success\n const outputResult = await activityDef.output[\"~standard\"].validate(result.value);\n if (outputResult.issues) {\n throw new ActivityOutputValidationError(activityName, outputResult.issues);\n }\n return outputResult.value;\n } else {\n // Convert Result.Error to thrown ApplicationFailure for Temporal.\n // Temporal recognizes this class natively and applies the\n // configured retry policy (honoring `nonRetryable: true`).\n throw result.error;\n }\n };\n }\n\n // 1) Wrap global activities defined directly under contract.activities\n if (contract.activities) {\n for (const [activityName, impl] of Object.entries(activities)) {\n // Skip workflow namespaces if present at root\n if (contract.workflows && activityName in contract.workflows) {\n continue;\n }\n\n const activityDef = contract.activities[activityName];\n if (!activityDef) {\n throw new ActivityDefinitionNotFoundError(activityName, Object.keys(contract.activities));\n }\n\n // Assign wrapped global activity\n (wrappedActivities as Record<string, unknown>)[activityName] = makeWrapped(\n activityName,\n activityDef,\n impl as (args: unknown) => Future<Result<unknown, ApplicationFailure>>,\n );\n }\n }\n\n // 2) Wrap workflow-scoped activities at root level (flat)\n if (contract.workflows) {\n for (const [workflowName, workflowDef] of Object.entries(contract.workflows)) {\n const wfActivitiesImpl = (activities as Record<string, unknown>)[workflowName] as\n | Record<string, unknown>\n | undefined;\n if (!wfActivitiesImpl) {\n // If no implementations provided for this workflow, skip (TypeScript typing should enforce completeness for declared ones)\n continue;\n }\n\n const wfDefs = workflowDef.activities ?? {};\n\n for (const [activityName, impl] of Object.entries(wfActivitiesImpl)) {\n const activityDef = wfDefs[activityName];\n if (!activityDef) {\n throw new ActivityDefinitionNotFoundError(\n `${workflowName}.${activityName}`,\n Object.keys(wfDefs),\n );\n }\n\n // Assign workflow activity directly at root level (flat structure)\n (wrappedActivities as Record<string, unknown>)[activityName] = makeWrapped(\n `${workflowName}.${activityName}`,\n activityDef,\n impl as (args: unknown) => Future<Result<unknown, ApplicationFailure>>,\n );\n }\n }\n }\n\n return wrappedActivities;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqLA,SAAgB,yBACd,SAC8B;CAC9B,MAAM,EAAE,UAAU,eAAe;CAGjC,MAAM,oBAAoB,EAAE;CAG5B,SAAS,YACP,cACA,aACA,cACA;AACA,SAAO,OAAO,GAAG,SAAoB;GACnC,MAAM,QAAQ,oBAAoB,KAAK;GAGvC,MAAM,cAAc,MAAM,YAAY,MAAM,aAAa,SAAS,MAAM;AACxE,OAAI,YAAY,OACd,OAAM,IAAI,6BAA6B,cAAc,YAAY,OAAO;GAO1E,MAAM,SAAS,MAHM,aAAa,YAAY,MAGb;AAGjC,OAAI,OAAO,MAAM,EAAE;IAEjB,MAAM,eAAe,MAAM,YAAY,OAAO,aAAa,SAAS,OAAO,MAAM;AACjF,QAAI,aAAa,OACf,OAAM,IAAI,8BAA8B,cAAc,aAAa,OAAO;AAE5E,WAAO,aAAa;SAKpB,OAAM,OAAO;;;AAMnB,KAAI,SAAS,WACX,MAAK,MAAM,CAAC,cAAc,SAAS,OAAO,QAAQ,WAAW,EAAE;AAE7D,MAAI,SAAS,aAAa,gBAAgB,SAAS,UACjD;EAGF,MAAM,cAAc,SAAS,WAAW;AACxC,MAAI,CAAC,YACH,OAAM,IAAI,gCAAgC,cAAc,OAAO,KAAK,SAAS,WAAW,CAAC;AAI1F,oBAA8C,gBAAgB,YAC7D,cACA,aACA,KACD;;AAKL,KAAI,SAAS,UACX,MAAK,MAAM,CAAC,cAAc,gBAAgB,OAAO,QAAQ,SAAS,UAAU,EAAE;EAC5E,MAAM,mBAAoB,WAAuC;AAGjE,MAAI,CAAC,iBAEH;EAGF,MAAM,SAAS,YAAY,cAAc,EAAE;AAE3C,OAAK,MAAM,CAAC,cAAc,SAAS,OAAO,QAAQ,iBAAiB,EAAE;GACnE,MAAM,cAAc,OAAO;AAC3B,OAAI,CAAC,YACH,OAAM,IAAI,gCACR,GAAG,aAAa,GAAG,gBACnB,OAAO,KAAK,OAAO,CACpB;AAIF,qBAA8C,gBAAgB,YAC7D,GAAG,aAAa,GAAG,gBACnB,aACA,KACD;;;AAKP,QAAO"}
|
|
1
|
+
{"version":3,"file":"activity.mjs","names":[],"sources":["../src/activity.ts"],"sourcesContent":["// Entry point for activity implementations.\n//\n// Activities run *outside* the workflow sandbox, so they use neverthrow's\n// `ResultAsync` directly. Workflow code (see workflow.ts) uses the same\n// neverthrow API — neverthrow's evaluation is compatible with Temporal's\n// deterministic replay machinery.\n//\n// Errors flow through Temporal's `ApplicationFailure` (re-exported from\n// `@temporalio/common`) — it's the SDK's first-class failure shape, so we\n// don't wrap it in a custom class. `ApplicationFailure` exposes\n// `nonRetryable`, `type`, `details`, and `category` natively, and survives\n// the activity → workflow serialization boundary unchanged.\nimport { ActivityDefinition, ContractDefinition } from \"@temporal-contract/contract\";\nimport type { ResultAsync } from \"neverthrow\";\nimport { ApplicationFailure } from \"@temporalio/common\";\nimport { WorkerInferInput, WorkerInferOutput } from \"./types.js\";\nimport {\n ActivityDefinitionNotFoundError,\n ActivityInputValidationError,\n ActivityOutputValidationError,\n} from \"./errors.js\";\nimport { extractHandlerInput } from \"./internal.js\";\n\nexport {\n ActivityDefinitionNotFoundError,\n ActivityInputValidationError,\n ActivityOutputValidationError,\n} from \"./errors.js\";\n\n// Re-export the canonical activity-failure class so consumers don't need\n// a separate `@temporalio/common` import to construct one.\nexport { ApplicationFailure } from \"@temporalio/common\";\n\n/**\n * Activity implementation using neverthrow's `ResultAsync`.\n *\n * Returns `ResultAsync<Output, ApplicationFailure>` for explicit error\n * handling instead of throwing. The wrapper rethrows `err()` payloads at\n * the activity boundary; Temporal recognizes `ApplicationFailure` natively\n * and applies the configured retry policy (with `nonRetryable: true`\n * opting an instance out per-call).\n */\ntype ResultActivityImplementation<TActivity extends ActivityDefinition> = (\n args: WorkerInferInput<TActivity>,\n) => ResultAsync<WorkerInferOutput<TActivity>, ApplicationFailure>;\n\n/**\n * Map of all activity implementations for a contract (global + all workflow-specific)\n */\ntype ContractResultActivitiesImplementations<TContract extends ContractDefinition> =\n // Global activities\n (TContract[\"activities\"] extends Record<string, ActivityDefinition>\n ? ResultActivitiesImplementations<TContract[\"activities\"]>\n : {}) &\n // All workflow-specific activities merged\n {\n [TWorkflow in keyof TContract[\"workflows\"]]: TContract[\"workflows\"][TWorkflow][\"activities\"] extends Record<\n string,\n ActivityDefinition\n >\n ? ResultActivitiesImplementations<TContract[\"workflows\"][TWorkflow][\"activities\"]>\n : {};\n };\n\ntype ResultActivitiesImplementations<TActivities extends Record<string, ActivityDefinition>> = {\n [K in keyof TActivities]: ResultActivityImplementation<TActivities[K]>;\n};\n\n/**\n * Options for creating activities handler\n */\ntype DeclareActivitiesHandlerOptions<TContract extends ContractDefinition> = {\n contract: TContract;\n activities: ContractResultActivitiesImplementations<TContract>;\n};\n\ntype ActivityImplementation<TActivity extends ActivityDefinition> = (\n args: WorkerInferInput<TActivity>,\n) => Promise<WorkerInferOutput<TActivity>>;\n\ntype ActivitiesImplementations<TActivities extends Record<string, ActivityDefinition>> = {\n [K in keyof TActivities]: ActivityImplementation<TActivities[K]>;\n};\n\ntype UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends (\n k: infer I,\n) => void\n ? I\n : never;\n\n/**\n * Activities handler ready for Temporal Worker\n *\n * Flat structure: all activities (global + all workflow-specific) are at the root level\n */\nexport type ActivitiesHandler<TContract extends ContractDefinition> =\n // Global activities\n (TContract[\"activities\"] extends Record<string, ActivityDefinition>\n ? ActivitiesImplementations<TContract[\"activities\"]>\n : {}) &\n // All workflow-specific activities merged at root level (flat)\n UnionToIntersection<\n {\n [TWorkflow in keyof TContract[\"workflows\"]]: TContract[\"workflows\"][TWorkflow][\"activities\"] extends Record<\n string,\n ActivityDefinition\n >\n ? ActivitiesImplementations<TContract[\"workflows\"][TWorkflow][\"activities\"]>\n : {};\n }[keyof TContract[\"workflows\"]]\n >;\n\n/**\n * Create a typed activities handler with automatic validation and Result pattern.\n *\n * This wraps all activity implementations with:\n * - Validation at network boundaries\n * - `ResultAsync<T, ApplicationFailure>` pattern for explicit error handling\n * - Automatic conversion from Result to Promise (throwing on Error)\n *\n * TypeScript ensures ALL activities (global + workflow-specific) are implemented.\n *\n * Use this to create the activities object for the Temporal Worker.\n *\n * @example\n * ```ts\n * import { declareActivitiesHandler, ApplicationFailure } from '@temporal-contract/worker/activity';\n * import { ResultAsync, errAsync, okAsync } from 'neverthrow';\n * import myContract from './contract';\n *\n * export const activities = declareActivitiesHandler({\n * contract: myContract,\n * activities: {\n * // Activity returns ResultAsync instead of throwing.\n * sendEmail: (args) =>\n * ResultAsync.fromPromise(\n * emailService.send(args),\n * (error) =>\n * // Wrap technical errors in ApplicationFailure. `nonRetryable`\n * // is per-instance: set it to true on permanent failures so\n * // Temporal stops retrying immediately.\n * ApplicationFailure.create({\n * type: 'EMAIL_SEND_FAILED',\n * message: 'Failed to send email',\n * nonRetryable: false,\n * cause: error instanceof Error ? error : undefined,\n * }),\n * ).map(() => ({ sent: true })),\n * },\n * });\n *\n * // Use with Temporal Worker\n * import { Worker } from '@temporalio/worker';\n *\n * const worker = await Worker.create({\n * workflowsPath: require.resolve('./workflows'),\n * activities: activities,\n * taskQueue: contract.taskQueue,\n * });\n * ```\n *\n * @remarks\n * The wrapper accepts implementations in the\n * `ResultAsync<T, ApplicationFailure>` shape and produces ordinary\n * Promise-returning Temporal handlers (`err(...)` → thrown\n * `ApplicationFailure`; `ok(...)` → output validated against the\n * contract and resolved). It does **not** hide Temporal's\n * `@temporalio/activity` runtime: inside the body you can still call\n * `Context.current()` from `@temporalio/activity` to access heartbeats\n * (`heartbeat(details)`, `heartbeatDetails`), activity info (attempt\n * number, workflow IDs), and the async-completion task token. See the\n * \"Working with the Activity Context\" section of the worker\n * implementation guide for end-to-end examples.\n */\nexport function declareActivitiesHandler<TContract extends ContractDefinition>(\n options: DeclareActivitiesHandlerOptions<TContract>,\n): ActivitiesHandler<TContract> {\n const { contract, activities } = options;\n\n // Prepare Temporal-compatible activities with validation and Result unwrapping\n const wrappedActivities = {} as ActivitiesHandler<TContract>;\n\n // Helper to create a wrapped implementation from a definition and impl\n function makeWrapped(\n activityName: string,\n activityDef: ActivityDefinition,\n activityImpl: (args: unknown) => ResultAsync<unknown, ApplicationFailure>,\n ) {\n return async (...args: unknown[]) => {\n const input = extractHandlerInput(args);\n\n // Validate input\n const inputResult = await activityDef.input[\"~standard\"].validate(input);\n if (inputResult.issues) {\n throw new ActivityInputValidationError(activityName, inputResult.issues);\n }\n\n // Execute neverthrow activity (returns ResultAsync<T, ApplicationFailure>);\n // awaiting yields a Result<T, ApplicationFailure>.\n const result = await activityImpl(inputResult.value);\n\n // Process result: validate output or throw error\n if (result.isOk()) {\n // Validate output on success\n const outputResult = await activityDef.output[\"~standard\"].validate(result.value);\n if (outputResult.issues) {\n throw new ActivityOutputValidationError(activityName, outputResult.issues);\n }\n return outputResult.value;\n } else {\n // Convert err(...) payload to thrown ApplicationFailure for Temporal.\n // Temporal recognizes this class natively and applies the\n // configured retry policy (honoring `nonRetryable: true`).\n throw result.error;\n }\n };\n }\n\n // 1) Wrap global activities defined directly under contract.activities\n if (contract.activities) {\n for (const [activityName, impl] of Object.entries(activities)) {\n // Skip workflow namespaces if present at root\n if (contract.workflows && activityName in contract.workflows) {\n continue;\n }\n\n const activityDef = contract.activities[activityName];\n if (!activityDef) {\n throw new ActivityDefinitionNotFoundError(activityName, Object.keys(contract.activities));\n }\n\n // Assign wrapped global activity\n (wrappedActivities as Record<string, unknown>)[activityName] = makeWrapped(\n activityName,\n activityDef,\n impl as (args: unknown) => ResultAsync<unknown, ApplicationFailure>,\n );\n }\n }\n\n // 2) Wrap workflow-scoped activities at root level (flat)\n if (contract.workflows) {\n for (const [workflowName, workflowDef] of Object.entries(contract.workflows)) {\n const wfActivitiesImpl = (activities as Record<string, unknown>)[workflowName] as\n | Record<string, unknown>\n | undefined;\n if (!wfActivitiesImpl) {\n // If no implementations provided for this workflow, skip (TypeScript typing should enforce completeness for declared ones)\n continue;\n }\n\n const wfDefs = workflowDef.activities ?? {};\n\n for (const [activityName, impl] of Object.entries(wfActivitiesImpl)) {\n const activityDef = wfDefs[activityName];\n if (!activityDef) {\n throw new ActivityDefinitionNotFoundError(\n `${workflowName}.${activityName}`,\n Object.keys(wfDefs),\n );\n }\n\n // Assign workflow activity directly at root level (flat structure)\n (wrappedActivities as Record<string, unknown>)[activityName] = makeWrapped(\n `${workflowName}.${activityName}`,\n activityDef,\n impl as (args: unknown) => ResultAsync<unknown, ApplicationFailure>,\n );\n }\n }\n }\n\n return wrappedActivities;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8KA,SAAgB,yBACd,SAC8B;CAC9B,MAAM,EAAE,UAAU,eAAe;CAGjC,MAAM,oBAAoB,EAAE;CAG5B,SAAS,YACP,cACA,aACA,cACA;AACA,SAAO,OAAO,GAAG,SAAoB;GACnC,MAAM,QAAQ,oBAAoB,KAAK;GAGvC,MAAM,cAAc,MAAM,YAAY,MAAM,aAAa,SAAS,MAAM;AACxE,OAAI,YAAY,OACd,OAAM,IAAI,6BAA6B,cAAc,YAAY,OAAO;GAK1E,MAAM,SAAS,MAAM,aAAa,YAAY,MAAM;AAGpD,OAAI,OAAO,MAAM,EAAE;IAEjB,MAAM,eAAe,MAAM,YAAY,OAAO,aAAa,SAAS,OAAO,MAAM;AACjF,QAAI,aAAa,OACf,OAAM,IAAI,8BAA8B,cAAc,aAAa,OAAO;AAE5E,WAAO,aAAa;SAKpB,OAAM,OAAO;;;AAMnB,KAAI,SAAS,WACX,MAAK,MAAM,CAAC,cAAc,SAAS,OAAO,QAAQ,WAAW,EAAE;AAE7D,MAAI,SAAS,aAAa,gBAAgB,SAAS,UACjD;EAGF,MAAM,cAAc,SAAS,WAAW;AACxC,MAAI,CAAC,YACH,OAAM,IAAI,gCAAgC,cAAc,OAAO,KAAK,SAAS,WAAW,CAAC;AAI1F,oBAA8C,gBAAgB,YAC7D,cACA,aACA,KACD;;AAKL,KAAI,SAAS,UACX,MAAK,MAAM,CAAC,cAAc,gBAAgB,OAAO,QAAQ,SAAS,UAAU,EAAE;EAC5E,MAAM,mBAAoB,WAAuC;AAGjE,MAAI,CAAC,iBAEH;EAGF,MAAM,SAAS,YAAY,cAAc,EAAE;AAE3C,OAAK,MAAM,CAAC,cAAc,SAAS,OAAO,QAAQ,iBAAiB,EAAE;GACnE,MAAM,cAAc,OAAO;AAC3B,OAAI,CAAC,YACH,OAAM,IAAI,gCACR,GAAG,aAAa,GAAG,gBACnB,OAAO,KAAK,OAAO,CACpB;AAIF,qBAA8C,gBAAgB,YAC7D,GAAG,aAAa,GAAG,gBACnB,aACA,KACD;;;AAKP,QAAO"}
|