@temporal-contract/worker 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,162 @@
1
+ import { g as WorkerInferOutput, h as WorkerInferInput } from "./errors-BqYWpdvd.cjs";
2
+ import { ActivityDefinition, ContractDefinition } from "@temporal-contract/contract";
3
+ import { Future, Result } from "@swan-io/boxed";
4
+
5
+ //#region src/activity-utils.d.ts
6
+
7
+ /**
8
+ * Extract activity definitions for a specific workflow from a contract
9
+ *
10
+ * This includes both:
11
+ * - Workflow-specific activities defined under workflow.activities
12
+ * - Global activities defined under contract.activities
13
+ *
14
+ * @param contract - The contract definition
15
+ * @param workflowName - The name of the workflow
16
+ * @returns Activity definitions for the workflow (workflow-specific + global activities merged)
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * const orderWorkflowActivities = getWorkflowActivities(myContract, 'processOrder');
21
+ * // Returns: { processPayment: ActivityDef, reserveInventory: ActivityDef, sendEmail: ActivityDef }
22
+ * // where sendEmail is a global activity
23
+ * ```
24
+ */
25
+ declare function getWorkflowActivities<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"]>(contract: TContract, workflowName: TWorkflowName): Record<string, ActivityDefinition>;
26
+ /**
27
+ * Extract all activity names for a specific workflow from a contract
28
+ *
29
+ * @param contract - The contract definition
30
+ * @param workflowName - The name of the workflow
31
+ * @returns Array of activity names (strings) available for the workflow
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * const activityNames = getWorkflowActivityNames(myContract, 'processOrder');
36
+ * // Returns: ['processPayment', 'reserveInventory', 'sendEmail']
37
+ * ```
38
+ */
39
+ declare function getWorkflowActivityNames<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"]>(contract: TContract, workflowName: TWorkflowName): string[];
40
+ /**
41
+ * Check if an activity belongs to a specific workflow
42
+ *
43
+ * @param contract - The contract definition
44
+ * @param workflowName - The name of the workflow
45
+ * @param activityName - The name of the activity to check
46
+ * @returns True if the activity is available for the workflow, false otherwise
47
+ *
48
+ * @example
49
+ * ```ts
50
+ * if (isWorkflowActivity(myContract, 'processOrder', 'processPayment')) {
51
+ * // Activity is available for this workflow
52
+ * }
53
+ * ```
54
+ */
55
+ declare function isWorkflowActivity<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"]>(contract: TContract, workflowName: TWorkflowName, activityName: string): boolean;
56
+ /**
57
+ * Get all workflow names from a contract
58
+ *
59
+ * @param contract - The contract definition
60
+ * @returns Array of workflow names defined in the contract
61
+ *
62
+ * @example
63
+ * ```ts
64
+ * const workflows = getWorkflowNames(myContract);
65
+ * // Returns: ['processOrder', 'processRefund']
66
+ * ```
67
+ */
68
+ declare function getWorkflowNames<TContract extends ContractDefinition>(contract: TContract): Array<keyof TContract["workflows"]>;
69
+ //#endregion
70
+ //#region src/activity.d.ts
71
+ /**
72
+ * Activity error class that should be used to wrap all technical exceptions
73
+ * Forces proper error handling and enables retry policies
74
+ */
75
+ declare class ActivityError extends Error {
76
+ readonly code: string;
77
+ constructor(code: string, message: string, cause?: unknown);
78
+ }
79
+ /**
80
+ * Activity implementation using Future/Result pattern
81
+ *
82
+ * Returns Future<Result<Output, ActivityError>> for explicit error handling instead of throwing exceptions.
83
+ * All errors must be wrapped in ActivityError to enable proper retry policies.
84
+ */
85
+ type BoxedActivityImplementation<TActivity extends ActivityDefinition> = (args: WorkerInferInput<TActivity>) => Future<Result<WorkerInferOutput<TActivity>, ActivityError>>;
86
+ /**
87
+ * Map of all activity implementations for a contract (global + all workflow-specific)
88
+ */
89
+ type ContractBoxedActivitiesImplementations<TContract extends ContractDefinition> = (TContract["activities"] extends Record<string, ActivityDefinition> ? BoxedActivitiesImplementations<TContract["activities"]> : {}) & { [TWorkflow in keyof TContract["workflows"]]: TContract["workflows"][TWorkflow]["activities"] extends Record<string, ActivityDefinition> ? BoxedActivitiesImplementations<TContract["workflows"][TWorkflow]["activities"]> : {} };
90
+ type BoxedActivitiesImplementations<TActivities extends Record<string, ActivityDefinition>> = { [K in keyof TActivities]: BoxedActivityImplementation<TActivities[K]> };
91
+ /**
92
+ * Options for creating activities handler
93
+ */
94
+ interface DeclareActivitiesHandlerOptions<TContract extends ContractDefinition> {
95
+ contract: TContract;
96
+ activities: ContractBoxedActivitiesImplementations<TContract>;
97
+ }
98
+ type ActivityImplementation<TActivity extends ActivityDefinition> = (args: WorkerInferInput<TActivity>) => Promise<WorkerInferOutput<TActivity>>;
99
+ type ActivitiesImplementations<TActivities extends Record<string, ActivityDefinition>> = { [K in keyof TActivities]: ActivityImplementation<TActivities[K]> };
100
+ type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
101
+ /**
102
+ * Activities handler ready for Temporal Worker
103
+ *
104
+ * Flat structure: all activities (global + all workflow-specific) are at the root level
105
+ */
106
+ type ActivitiesHandler<TContract extends ContractDefinition> = (TContract["activities"] extends Record<string, ActivityDefinition> ? ActivitiesImplementations<TContract["activities"]> : {}) & UnionToIntersection<{ [TWorkflow in keyof TContract["workflows"]]: TContract["workflows"][TWorkflow]["activities"] extends Record<string, ActivityDefinition> ? ActivitiesImplementations<TContract["workflows"][TWorkflow]["activities"]> : {} }[keyof TContract["workflows"]]>;
107
+ /**
108
+ * Create a typed activities handler with automatic validation and Result pattern
109
+ *
110
+ * This wraps all activity implementations with:
111
+ * - Validation at network boundaries
112
+ * - Result<T, ActivityError> pattern for explicit error handling
113
+ * - Automatic conversion from Result to Promise (throwing on Error)
114
+ *
115
+ * TypeScript ensures ALL activities (global + workflow-specific) are implemented.
116
+ *
117
+ * Use this to create the activities object for the Temporal Worker.
118
+ *
119
+ * @example
120
+ * ```ts
121
+ * import { declareActivitiesHandler, ActivityError } from '@temporal-contract/worker/activity';
122
+ * import { Result, Future } from '@swan-io/boxed';
123
+ * import myContract from './contract';
124
+ *
125
+ * export const activities = declareActivitiesHandler({
126
+ * contract: myContract,
127
+ * activities: {
128
+ * // Activity returns Result instead of throwing
129
+ * // All technical exceptions must be wrapped in ActivityError for retry policies
130
+ * sendEmail: (args) => {
131
+ * return Future.make(async resolve => {
132
+ * try {
133
+ * await emailService.send(args);
134
+ * resolve(Result.Ok({ sent: true }));
135
+ * } catch (error) {
136
+ * // Wrap technical errors in ActivityError to enable retries
137
+ * resolve(Result.Error(
138
+ * new ActivityError(
139
+ * 'EMAIL_SEND_FAILED',
140
+ * 'Failed to send email',
141
+ * error // Original error as cause for debugging
142
+ * )
143
+ * ));
144
+ * }
145
+ * });
146
+ * },
147
+ * },
148
+ * });
149
+ *
150
+ * // Use with Temporal Worker
151
+ * import { Worker } from '@temporalio/worker';
152
+ *
153
+ * const worker = await Worker.create({
154
+ * workflowsPath: require.resolve('./workflows'),
155
+ * activities: activities,
156
+ * taskQueue: contract.taskQueue,
157
+ * });
158
+ * ```
159
+ */
160
+ declare function declareActivitiesHandler<TContract extends ContractDefinition>(options: DeclareActivitiesHandlerOptions<TContract>): ActivitiesHandler<TContract>;
161
+ //#endregion
162
+ export { getWorkflowActivityNames as a, getWorkflowActivities as i, ActivityError as n, getWorkflowNames as o, declareActivitiesHandler as r, isWorkflowActivity as s, ActivitiesHandler as t };
@@ -0,0 +1,162 @@
1
+ import { g as WorkerInferOutput, h as WorkerInferInput } from "./errors-C1RFkCuD.mjs";
2
+ import { ActivityDefinition, ContractDefinition } from "@temporal-contract/contract";
3
+ import { Future, Result } from "@swan-io/boxed";
4
+
5
+ //#region src/activity-utils.d.ts
6
+
7
+ /**
8
+ * Extract activity definitions for a specific workflow from a contract
9
+ *
10
+ * This includes both:
11
+ * - Workflow-specific activities defined under workflow.activities
12
+ * - Global activities defined under contract.activities
13
+ *
14
+ * @param contract - The contract definition
15
+ * @param workflowName - The name of the workflow
16
+ * @returns Activity definitions for the workflow (workflow-specific + global activities merged)
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * const orderWorkflowActivities = getWorkflowActivities(myContract, 'processOrder');
21
+ * // Returns: { processPayment: ActivityDef, reserveInventory: ActivityDef, sendEmail: ActivityDef }
22
+ * // where sendEmail is a global activity
23
+ * ```
24
+ */
25
+ declare function getWorkflowActivities<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"]>(contract: TContract, workflowName: TWorkflowName): Record<string, ActivityDefinition>;
26
+ /**
27
+ * Extract all activity names for a specific workflow from a contract
28
+ *
29
+ * @param contract - The contract definition
30
+ * @param workflowName - The name of the workflow
31
+ * @returns Array of activity names (strings) available for the workflow
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * const activityNames = getWorkflowActivityNames(myContract, 'processOrder');
36
+ * // Returns: ['processPayment', 'reserveInventory', 'sendEmail']
37
+ * ```
38
+ */
39
+ declare function getWorkflowActivityNames<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"]>(contract: TContract, workflowName: TWorkflowName): string[];
40
+ /**
41
+ * Check if an activity belongs to a specific workflow
42
+ *
43
+ * @param contract - The contract definition
44
+ * @param workflowName - The name of the workflow
45
+ * @param activityName - The name of the activity to check
46
+ * @returns True if the activity is available for the workflow, false otherwise
47
+ *
48
+ * @example
49
+ * ```ts
50
+ * if (isWorkflowActivity(myContract, 'processOrder', 'processPayment')) {
51
+ * // Activity is available for this workflow
52
+ * }
53
+ * ```
54
+ */
55
+ declare function isWorkflowActivity<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"]>(contract: TContract, workflowName: TWorkflowName, activityName: string): boolean;
56
+ /**
57
+ * Get all workflow names from a contract
58
+ *
59
+ * @param contract - The contract definition
60
+ * @returns Array of workflow names defined in the contract
61
+ *
62
+ * @example
63
+ * ```ts
64
+ * const workflows = getWorkflowNames(myContract);
65
+ * // Returns: ['processOrder', 'processRefund']
66
+ * ```
67
+ */
68
+ declare function getWorkflowNames<TContract extends ContractDefinition>(contract: TContract): Array<keyof TContract["workflows"]>;
69
+ //#endregion
70
+ //#region src/activity.d.ts
71
+ /**
72
+ * Activity error class that should be used to wrap all technical exceptions
73
+ * Forces proper error handling and enables retry policies
74
+ */
75
+ declare class ActivityError extends Error {
76
+ readonly code: string;
77
+ constructor(code: string, message: string, cause?: unknown);
78
+ }
79
+ /**
80
+ * Activity implementation using Future/Result pattern
81
+ *
82
+ * Returns Future<Result<Output, ActivityError>> for explicit error handling instead of throwing exceptions.
83
+ * All errors must be wrapped in ActivityError to enable proper retry policies.
84
+ */
85
+ type BoxedActivityImplementation<TActivity extends ActivityDefinition> = (args: WorkerInferInput<TActivity>) => Future<Result<WorkerInferOutput<TActivity>, ActivityError>>;
86
+ /**
87
+ * Map of all activity implementations for a contract (global + all workflow-specific)
88
+ */
89
+ type ContractBoxedActivitiesImplementations<TContract extends ContractDefinition> = (TContract["activities"] extends Record<string, ActivityDefinition> ? BoxedActivitiesImplementations<TContract["activities"]> : {}) & { [TWorkflow in keyof TContract["workflows"]]: TContract["workflows"][TWorkflow]["activities"] extends Record<string, ActivityDefinition> ? BoxedActivitiesImplementations<TContract["workflows"][TWorkflow]["activities"]> : {} };
90
+ type BoxedActivitiesImplementations<TActivities extends Record<string, ActivityDefinition>> = { [K in keyof TActivities]: BoxedActivityImplementation<TActivities[K]> };
91
+ /**
92
+ * Options for creating activities handler
93
+ */
94
+ interface DeclareActivitiesHandlerOptions<TContract extends ContractDefinition> {
95
+ contract: TContract;
96
+ activities: ContractBoxedActivitiesImplementations<TContract>;
97
+ }
98
+ type ActivityImplementation<TActivity extends ActivityDefinition> = (args: WorkerInferInput<TActivity>) => Promise<WorkerInferOutput<TActivity>>;
99
+ type ActivitiesImplementations<TActivities extends Record<string, ActivityDefinition>> = { [K in keyof TActivities]: ActivityImplementation<TActivities[K]> };
100
+ type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
101
+ /**
102
+ * Activities handler ready for Temporal Worker
103
+ *
104
+ * Flat structure: all activities (global + all workflow-specific) are at the root level
105
+ */
106
+ type ActivitiesHandler<TContract extends ContractDefinition> = (TContract["activities"] extends Record<string, ActivityDefinition> ? ActivitiesImplementations<TContract["activities"]> : {}) & UnionToIntersection<{ [TWorkflow in keyof TContract["workflows"]]: TContract["workflows"][TWorkflow]["activities"] extends Record<string, ActivityDefinition> ? ActivitiesImplementations<TContract["workflows"][TWorkflow]["activities"]> : {} }[keyof TContract["workflows"]]>;
107
+ /**
108
+ * Create a typed activities handler with automatic validation and Result pattern
109
+ *
110
+ * This wraps all activity implementations with:
111
+ * - Validation at network boundaries
112
+ * - Result<T, ActivityError> pattern for explicit error handling
113
+ * - Automatic conversion from Result to Promise (throwing on Error)
114
+ *
115
+ * TypeScript ensures ALL activities (global + workflow-specific) are implemented.
116
+ *
117
+ * Use this to create the activities object for the Temporal Worker.
118
+ *
119
+ * @example
120
+ * ```ts
121
+ * import { declareActivitiesHandler, ActivityError } from '@temporal-contract/worker/activity';
122
+ * import { Result, Future } from '@swan-io/boxed';
123
+ * import myContract from './contract';
124
+ *
125
+ * export const activities = declareActivitiesHandler({
126
+ * contract: myContract,
127
+ * activities: {
128
+ * // Activity returns Result instead of throwing
129
+ * // All technical exceptions must be wrapped in ActivityError for retry policies
130
+ * sendEmail: (args) => {
131
+ * return Future.make(async resolve => {
132
+ * try {
133
+ * await emailService.send(args);
134
+ * resolve(Result.Ok({ sent: true }));
135
+ * } catch (error) {
136
+ * // Wrap technical errors in ActivityError to enable retries
137
+ * resolve(Result.Error(
138
+ * new ActivityError(
139
+ * 'EMAIL_SEND_FAILED',
140
+ * 'Failed to send email',
141
+ * error // Original error as cause for debugging
142
+ * )
143
+ * ));
144
+ * }
145
+ * });
146
+ * },
147
+ * },
148
+ * });
149
+ *
150
+ * // Use with Temporal Worker
151
+ * import { Worker } from '@temporalio/worker';
152
+ *
153
+ * const worker = await Worker.create({
154
+ * workflowsPath: require.resolve('./workflows'),
155
+ * activities: activities,
156
+ * taskQueue: contract.taskQueue,
157
+ * });
158
+ * ```
159
+ */
160
+ declare function declareActivitiesHandler<TContract extends ContractDefinition>(options: DeclareActivitiesHandlerOptions<TContract>): ActivitiesHandler<TContract>;
161
+ //#endregion
162
+ export { getWorkflowActivityNames as a, getWorkflowActivities as i, ActivityError as n, getWorkflowNames as o, declareActivitiesHandler as r, isWorkflowActivity as s, ActivitiesHandler as t };
package/dist/activity.cjs CHANGED
@@ -1,5 +1,83 @@
1
1
  const require_errors = require('./errors-DjSZg-93.cjs');
2
2
 
3
+ //#region src/activity-utils.ts
4
+ /**
5
+ * Extract activity definitions for a specific workflow from a contract
6
+ *
7
+ * This includes both:
8
+ * - Workflow-specific activities defined under workflow.activities
9
+ * - Global activities defined under contract.activities
10
+ *
11
+ * @param contract - The contract definition
12
+ * @param workflowName - The name of the workflow
13
+ * @returns Activity definitions for the workflow (workflow-specific + global activities merged)
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * const orderWorkflowActivities = getWorkflowActivities(myContract, 'processOrder');
18
+ * // Returns: { processPayment: ActivityDef, reserveInventory: ActivityDef, sendEmail: ActivityDef }
19
+ * // where sendEmail is a global activity
20
+ * ```
21
+ */
22
+ function getWorkflowActivities(contract, workflowName) {
23
+ const workflowActivities = contract.workflows[workflowName]?.activities || {};
24
+ return {
25
+ ...contract.activities || {},
26
+ ...workflowActivities
27
+ };
28
+ }
29
+ /**
30
+ * Extract all activity names for a specific workflow from a contract
31
+ *
32
+ * @param contract - The contract definition
33
+ * @param workflowName - The name of the workflow
34
+ * @returns Array of activity names (strings) available for the workflow
35
+ *
36
+ * @example
37
+ * ```ts
38
+ * const activityNames = getWorkflowActivityNames(myContract, 'processOrder');
39
+ * // Returns: ['processPayment', 'reserveInventory', 'sendEmail']
40
+ * ```
41
+ */
42
+ function getWorkflowActivityNames(contract, workflowName) {
43
+ const activities = getWorkflowActivities(contract, workflowName);
44
+ return Object.keys(activities);
45
+ }
46
+ /**
47
+ * Check if an activity belongs to a specific workflow
48
+ *
49
+ * @param contract - The contract definition
50
+ * @param workflowName - The name of the workflow
51
+ * @param activityName - The name of the activity to check
52
+ * @returns True if the activity is available for the workflow, false otherwise
53
+ *
54
+ * @example
55
+ * ```ts
56
+ * if (isWorkflowActivity(myContract, 'processOrder', 'processPayment')) {
57
+ * // Activity is available for this workflow
58
+ * }
59
+ * ```
60
+ */
61
+ function isWorkflowActivity(contract, workflowName, activityName) {
62
+ return activityName in getWorkflowActivities(contract, workflowName);
63
+ }
64
+ /**
65
+ * Get all workflow names from a contract
66
+ *
67
+ * @param contract - The contract definition
68
+ * @returns Array of workflow names defined in the contract
69
+ *
70
+ * @example
71
+ * ```ts
72
+ * const workflows = getWorkflowNames(myContract);
73
+ * // Returns: ['processOrder', 'processRefund']
74
+ * ```
75
+ */
76
+ function getWorkflowNames(contract) {
77
+ return Object.keys(contract.workflows);
78
+ }
79
+
80
+ //#endregion
3
81
  //#region src/activity.ts
4
82
  /**
5
83
  * Activity error class that should be used to wrap all technical exceptions
@@ -28,7 +106,7 @@ var ActivityError = class ActivityError extends Error {
28
106
  * @example
29
107
  * ```ts
30
108
  * import { declareActivitiesHandler, ActivityError } from '@temporal-contract/worker/activity';
31
- * import { Result, Future } from '@temporal-contract/boxed';
109
+ * import { Result, Future } from '@swan-io/boxed';
32
110
  * import myContract from './contract';
33
111
  *
34
112
  * export const activities = declareActivitiesHandler({
@@ -106,4 +184,8 @@ exports.ActivityDefinitionNotFoundError = require_errors.ActivityDefinitionNotFo
106
184
  exports.ActivityError = ActivityError;
107
185
  exports.ActivityInputValidationError = require_errors.ActivityInputValidationError;
108
186
  exports.ActivityOutputValidationError = require_errors.ActivityOutputValidationError;
109
- exports.declareActivitiesHandler = declareActivitiesHandler;
187
+ exports.declareActivitiesHandler = declareActivitiesHandler;
188
+ exports.getWorkflowActivities = getWorkflowActivities;
189
+ exports.getWorkflowActivityNames = getWorkflowActivityNames;
190
+ exports.getWorkflowNames = getWorkflowNames;
191
+ exports.isWorkflowActivity = isWorkflowActivity;
@@ -1,98 +1,3 @@
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 };
1
+ import { n as ActivityInputValidationError, r as ActivityOutputValidationError, t as ActivityDefinitionNotFoundError } from "./errors-BqYWpdvd.cjs";
2
+ import { a as getWorkflowActivityNames, i as getWorkflowActivities, n as ActivityError, o as getWorkflowNames, r as declareActivitiesHandler, s as isWorkflowActivity, t as ActivitiesHandler } from "./activity-Csptpwgf.cjs";
3
+ export { ActivitiesHandler, ActivityDefinitionNotFoundError, ActivityError, ActivityInputValidationError, ActivityOutputValidationError, declareActivitiesHandler, getWorkflowActivities, getWorkflowActivityNames, getWorkflowNames, isWorkflowActivity };
@@ -1,98 +1,3 @@
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 };
1
+ import { n as ActivityInputValidationError, r as ActivityOutputValidationError, t as ActivityDefinitionNotFoundError } from "./errors-C1RFkCuD.mjs";
2
+ import { a as getWorkflowActivityNames, i as getWorkflowActivities, n as ActivityError, o as getWorkflowNames, r as declareActivitiesHandler, s as isWorkflowActivity, t as ActivitiesHandler } from "./activity-UOnp_bSX.mjs";
3
+ export { ActivitiesHandler, ActivityDefinitionNotFoundError, ActivityError, ActivityInputValidationError, ActivityOutputValidationError, declareActivitiesHandler, getWorkflowActivities, getWorkflowActivityNames, getWorkflowNames, isWorkflowActivity };
package/dist/activity.mjs CHANGED
@@ -1,5 +1,83 @@
1
1
  import { n as ActivityInputValidationError, r as ActivityOutputValidationError, t as ActivityDefinitionNotFoundError } from "./errors-BqVTpfcf.mjs";
2
2
 
3
+ //#region src/activity-utils.ts
4
+ /**
5
+ * Extract activity definitions for a specific workflow from a contract
6
+ *
7
+ * This includes both:
8
+ * - Workflow-specific activities defined under workflow.activities
9
+ * - Global activities defined under contract.activities
10
+ *
11
+ * @param contract - The contract definition
12
+ * @param workflowName - The name of the workflow
13
+ * @returns Activity definitions for the workflow (workflow-specific + global activities merged)
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * const orderWorkflowActivities = getWorkflowActivities(myContract, 'processOrder');
18
+ * // Returns: { processPayment: ActivityDef, reserveInventory: ActivityDef, sendEmail: ActivityDef }
19
+ * // where sendEmail is a global activity
20
+ * ```
21
+ */
22
+ function getWorkflowActivities(contract, workflowName) {
23
+ const workflowActivities = contract.workflows[workflowName]?.activities || {};
24
+ return {
25
+ ...contract.activities || {},
26
+ ...workflowActivities
27
+ };
28
+ }
29
+ /**
30
+ * Extract all activity names for a specific workflow from a contract
31
+ *
32
+ * @param contract - The contract definition
33
+ * @param workflowName - The name of the workflow
34
+ * @returns Array of activity names (strings) available for the workflow
35
+ *
36
+ * @example
37
+ * ```ts
38
+ * const activityNames = getWorkflowActivityNames(myContract, 'processOrder');
39
+ * // Returns: ['processPayment', 'reserveInventory', 'sendEmail']
40
+ * ```
41
+ */
42
+ function getWorkflowActivityNames(contract, workflowName) {
43
+ const activities = getWorkflowActivities(contract, workflowName);
44
+ return Object.keys(activities);
45
+ }
46
+ /**
47
+ * Check if an activity belongs to a specific workflow
48
+ *
49
+ * @param contract - The contract definition
50
+ * @param workflowName - The name of the workflow
51
+ * @param activityName - The name of the activity to check
52
+ * @returns True if the activity is available for the workflow, false otherwise
53
+ *
54
+ * @example
55
+ * ```ts
56
+ * if (isWorkflowActivity(myContract, 'processOrder', 'processPayment')) {
57
+ * // Activity is available for this workflow
58
+ * }
59
+ * ```
60
+ */
61
+ function isWorkflowActivity(contract, workflowName, activityName) {
62
+ return activityName in getWorkflowActivities(contract, workflowName);
63
+ }
64
+ /**
65
+ * Get all workflow names from a contract
66
+ *
67
+ * @param contract - The contract definition
68
+ * @returns Array of workflow names defined in the contract
69
+ *
70
+ * @example
71
+ * ```ts
72
+ * const workflows = getWorkflowNames(myContract);
73
+ * // Returns: ['processOrder', 'processRefund']
74
+ * ```
75
+ */
76
+ function getWorkflowNames(contract) {
77
+ return Object.keys(contract.workflows);
78
+ }
79
+
80
+ //#endregion
3
81
  //#region src/activity.ts
4
82
  /**
5
83
  * Activity error class that should be used to wrap all technical exceptions
@@ -28,7 +106,7 @@ var ActivityError = class ActivityError extends Error {
28
106
  * @example
29
107
  * ```ts
30
108
  * import { declareActivitiesHandler, ActivityError } from '@temporal-contract/worker/activity';
31
- * import { Result, Future } from '@temporal-contract/boxed';
109
+ * import { Result, Future } from '@swan-io/boxed';
32
110
  * import myContract from './contract';
33
111
  *
34
112
  * export const activities = declareActivitiesHandler({
@@ -102,4 +180,4 @@ function declareActivitiesHandler(options) {
102
180
  }
103
181
 
104
182
  //#endregion
105
- export { ActivityDefinitionNotFoundError, ActivityError, ActivityInputValidationError, ActivityOutputValidationError, declareActivitiesHandler };
183
+ export { ActivityDefinitionNotFoundError, ActivityError, ActivityInputValidationError, ActivityOutputValidationError, declareActivitiesHandler, getWorkflowActivities, getWorkflowActivityNames, getWorkflowNames, isWorkflowActivity };
package/dist/worker.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { ActivitiesHandler } from "./activity.cjs";
1
+ import { t as ActivitiesHandler } from "./activity-Csptpwgf.cjs";
2
2
  import { ContractDefinition } from "@temporal-contract/contract";
3
3
  import { Worker, WorkerOptions } from "@temporalio/worker";
4
4
 
package/dist/worker.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { ActivitiesHandler } from "./activity.mjs";
1
+ import { t as ActivitiesHandler } from "./activity-UOnp_bSX.mjs";
2
2
  import { Worker, WorkerOptions } from "@temporalio/worker";
3
3
  import { ContractDefinition } from "@temporal-contract/contract";
4
4
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@temporal-contract/worker",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "description": "Worker utilities with Result/Future pattern for implementing temporal-contract workflows and activities",
5
5
  "keywords": [
6
6
  "contract",
@@ -63,8 +63,9 @@
63
63
  ],
64
64
  "dependencies": {
65
65
  "@standard-schema/spec": "1.1.0",
66
- "@temporal-contract/boxed": "0.0.6",
67
- "@temporal-contract/contract": "0.0.6"
66
+ "@swan-io/boxed": "3.2.1",
67
+ "@temporal-contract/boxed": "0.0.7",
68
+ "@temporal-contract/contract": "0.0.7"
68
69
  },
69
70
  "devDependencies": {
70
71
  "@temporalio/client": "1.14.0",
@@ -76,9 +77,9 @@
76
77
  "typescript": "5.9.3",
77
78
  "vitest": "4.0.16",
78
79
  "zod": "4.2.1",
79
- "@temporal-contract/client": "0.0.6",
80
- "@temporal-contract/testing": "0.0.6",
81
- "@temporal-contract/tsconfig": "0.0.6"
80
+ "@temporal-contract/client": "0.0.7",
81
+ "@temporal-contract/testing": "0.0.7",
82
+ "@temporal-contract/tsconfig": "0.0.7"
82
83
  },
83
84
  "peerDependencies": {
84
85
  "@temporalio/worker": "^1",