@temporal-contract/worker 0.1.0 → 0.2.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.
@@ -1,161 +0,0 @@
1
- import { g as WorkerInferOutput, h as WorkerInferInput } from "./errors-CXpHOFmk.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
- * Extract activity definitions for a specific workflow from a contract
8
- *
9
- * This includes both:
10
- * - Workflow-specific activities defined under workflow.activities
11
- * - Global activities defined under contract.activities
12
- *
13
- * @param contract - The contract definition
14
- * @param workflowName - The name of the workflow
15
- * @returns Activity definitions for the workflow (workflow-specific + global activities merged)
16
- *
17
- * @example
18
- * ```ts
19
- * const orderWorkflowActivities = getWorkflowActivities(myContract, 'processOrder');
20
- * // Returns: { processPayment: ActivityDef, reserveInventory: ActivityDef, sendEmail: ActivityDef }
21
- * // where sendEmail is a global activity
22
- * ```
23
- */
24
- declare function getWorkflowActivities<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"]>(contract: TContract, workflowName: TWorkflowName): Record<string, ActivityDefinition>;
25
- /**
26
- * Extract all activity names for a specific workflow from a contract
27
- *
28
- * @param contract - The contract definition
29
- * @param workflowName - The name of the workflow
30
- * @returns Array of activity names (strings) available for the workflow
31
- *
32
- * @example
33
- * ```ts
34
- * const activityNames = getWorkflowActivityNames(myContract, 'processOrder');
35
- * // Returns: ['processPayment', 'reserveInventory', 'sendEmail']
36
- * ```
37
- */
38
- declare function getWorkflowActivityNames<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"]>(contract: TContract, workflowName: TWorkflowName): string[];
39
- /**
40
- * Check if an activity belongs to a specific workflow
41
- *
42
- * @param contract - The contract definition
43
- * @param workflowName - The name of the workflow
44
- * @param activityName - The name of the activity to check
45
- * @returns True if the activity is available for the workflow, false otherwise
46
- *
47
- * @example
48
- * ```ts
49
- * if (isWorkflowActivity(myContract, 'processOrder', 'processPayment')) {
50
- * // Activity is available for this workflow
51
- * }
52
- * ```
53
- */
54
- declare function isWorkflowActivity<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"]>(contract: TContract, workflowName: TWorkflowName, activityName: string): boolean;
55
- /**
56
- * Get all workflow names from a contract
57
- *
58
- * @param contract - The contract definition
59
- * @returns Array of workflow names defined in the contract
60
- *
61
- * @example
62
- * ```ts
63
- * const workflows = getWorkflowNames(myContract);
64
- * // Returns: ['processOrder', 'processRefund']
65
- * ```
66
- */
67
- declare function getWorkflowNames<TContract extends ContractDefinition>(contract: TContract): Array<keyof TContract["workflows"]>;
68
- //#endregion
69
- //#region src/activity.d.ts
70
- /**
71
- * Activity error class that should be used to wrap all technical exceptions
72
- * Forces proper error handling and enables retry policies
73
- */
74
- declare class ActivityError extends Error {
75
- readonly code: string;
76
- constructor(code: string, message: string, cause?: unknown);
77
- }
78
- /**
79
- * Activity implementation using Future/Result pattern
80
- *
81
- * Returns Future<Result<Output, ActivityError>> for explicit error handling instead of throwing exceptions.
82
- * All errors must be wrapped in ActivityError to enable proper retry policies.
83
- */
84
- type BoxedActivityImplementation<TActivity extends ActivityDefinition> = (args: WorkerInferInput<TActivity>) => Future<Result<WorkerInferOutput<TActivity>, ActivityError>>;
85
- /**
86
- * Map of all activity implementations for a contract (global + all workflow-specific)
87
- */
88
- 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"]> : {} };
89
- type BoxedActivitiesImplementations<TActivities extends Record<string, ActivityDefinition>> = { [K in keyof TActivities]: BoxedActivityImplementation<TActivities[K]> };
90
- /**
91
- * Options for creating activities handler
92
- */
93
- interface DeclareActivitiesHandlerOptions<TContract extends ContractDefinition> {
94
- contract: TContract;
95
- activities: ContractBoxedActivitiesImplementations<TContract>;
96
- }
97
- type ActivityImplementation<TActivity extends ActivityDefinition> = (args: WorkerInferInput<TActivity>) => Promise<WorkerInferOutput<TActivity>>;
98
- type ActivitiesImplementations<TActivities extends Record<string, ActivityDefinition>> = { [K in keyof TActivities]: ActivityImplementation<TActivities[K]> };
99
- type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
100
- /**
101
- * Activities handler ready for Temporal Worker
102
- *
103
- * Flat structure: all activities (global + all workflow-specific) are at the root level
104
- */
105
- 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"]]>;
106
- /**
107
- * Create a typed activities handler with automatic validation and Result pattern
108
- *
109
- * This wraps all activity implementations with:
110
- * - Validation at network boundaries
111
- * - Result<T, ActivityError> pattern for explicit error handling
112
- * - Automatic conversion from Result to Promise (throwing on Error)
113
- *
114
- * TypeScript ensures ALL activities (global + workflow-specific) are implemented.
115
- *
116
- * Use this to create the activities object for the Temporal Worker.
117
- *
118
- * @example
119
- * ```ts
120
- * import { declareActivitiesHandler, ActivityError } from '@temporal-contract/worker/activity';
121
- * import { Result, Future } from '@swan-io/boxed';
122
- * import myContract from './contract';
123
- *
124
- * export const activities = declareActivitiesHandler({
125
- * contract: myContract,
126
- * activities: {
127
- * // Activity returns Result instead of throwing
128
- * // All technical exceptions must be wrapped in ActivityError for retry policies
129
- * sendEmail: (args) => {
130
- * return Future.make(async resolve => {
131
- * try {
132
- * await emailService.send(args);
133
- * resolve(Result.Ok({ sent: true }));
134
- * } catch (error) {
135
- * // Wrap technical errors in ActivityError to enable retries
136
- * resolve(Result.Error(
137
- * new ActivityError(
138
- * 'EMAIL_SEND_FAILED',
139
- * 'Failed to send email',
140
- * error // Original error as cause for debugging
141
- * )
142
- * ));
143
- * }
144
- * });
145
- * },
146
- * },
147
- * });
148
- *
149
- * // Use with Temporal Worker
150
- * import { Worker } from '@temporalio/worker';
151
- *
152
- * const worker = await Worker.create({
153
- * workflowsPath: require.resolve('./workflows'),
154
- * activities: activities,
155
- * taskQueue: contract.taskQueue,
156
- * });
157
- * ```
158
- */
159
- declare function declareActivitiesHandler<TContract extends ContractDefinition>(options: DeclareActivitiesHandlerOptions<TContract>): ActivitiesHandler<TContract>;
160
- //#endregion
161
- export { getWorkflowActivityNames as a, getWorkflowActivities as i, ActivityError as n, getWorkflowNames as o, declareActivitiesHandler as r, isWorkflowActivity as s, ActivitiesHandler as t };
@@ -1,161 +0,0 @@
1
- import { g as WorkerInferOutput, h as WorkerInferInput } from "./errors-Vr-sKdW7.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
- * Extract activity definitions for a specific workflow from a contract
8
- *
9
- * This includes both:
10
- * - Workflow-specific activities defined under workflow.activities
11
- * - Global activities defined under contract.activities
12
- *
13
- * @param contract - The contract definition
14
- * @param workflowName - The name of the workflow
15
- * @returns Activity definitions for the workflow (workflow-specific + global activities merged)
16
- *
17
- * @example
18
- * ```ts
19
- * const orderWorkflowActivities = getWorkflowActivities(myContract, 'processOrder');
20
- * // Returns: { processPayment: ActivityDef, reserveInventory: ActivityDef, sendEmail: ActivityDef }
21
- * // where sendEmail is a global activity
22
- * ```
23
- */
24
- declare function getWorkflowActivities<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"]>(contract: TContract, workflowName: TWorkflowName): Record<string, ActivityDefinition>;
25
- /**
26
- * Extract all activity names for a specific workflow from a contract
27
- *
28
- * @param contract - The contract definition
29
- * @param workflowName - The name of the workflow
30
- * @returns Array of activity names (strings) available for the workflow
31
- *
32
- * @example
33
- * ```ts
34
- * const activityNames = getWorkflowActivityNames(myContract, 'processOrder');
35
- * // Returns: ['processPayment', 'reserveInventory', 'sendEmail']
36
- * ```
37
- */
38
- declare function getWorkflowActivityNames<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"]>(contract: TContract, workflowName: TWorkflowName): string[];
39
- /**
40
- * Check if an activity belongs to a specific workflow
41
- *
42
- * @param contract - The contract definition
43
- * @param workflowName - The name of the workflow
44
- * @param activityName - The name of the activity to check
45
- * @returns True if the activity is available for the workflow, false otherwise
46
- *
47
- * @example
48
- * ```ts
49
- * if (isWorkflowActivity(myContract, 'processOrder', 'processPayment')) {
50
- * // Activity is available for this workflow
51
- * }
52
- * ```
53
- */
54
- declare function isWorkflowActivity<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"]>(contract: TContract, workflowName: TWorkflowName, activityName: string): boolean;
55
- /**
56
- * Get all workflow names from a contract
57
- *
58
- * @param contract - The contract definition
59
- * @returns Array of workflow names defined in the contract
60
- *
61
- * @example
62
- * ```ts
63
- * const workflows = getWorkflowNames(myContract);
64
- * // Returns: ['processOrder', 'processRefund']
65
- * ```
66
- */
67
- declare function getWorkflowNames<TContract extends ContractDefinition>(contract: TContract): Array<keyof TContract["workflows"]>;
68
- //#endregion
69
- //#region src/activity.d.ts
70
- /**
71
- * Activity error class that should be used to wrap all technical exceptions
72
- * Forces proper error handling and enables retry policies
73
- */
74
- declare class ActivityError extends Error {
75
- readonly code: string;
76
- constructor(code: string, message: string, cause?: unknown);
77
- }
78
- /**
79
- * Activity implementation using Future/Result pattern
80
- *
81
- * Returns Future<Result<Output, ActivityError>> for explicit error handling instead of throwing exceptions.
82
- * All errors must be wrapped in ActivityError to enable proper retry policies.
83
- */
84
- type BoxedActivityImplementation<TActivity extends ActivityDefinition> = (args: WorkerInferInput<TActivity>) => Future<Result<WorkerInferOutput<TActivity>, ActivityError>>;
85
- /**
86
- * Map of all activity implementations for a contract (global + all workflow-specific)
87
- */
88
- 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"]> : {} };
89
- type BoxedActivitiesImplementations<TActivities extends Record<string, ActivityDefinition>> = { [K in keyof TActivities]: BoxedActivityImplementation<TActivities[K]> };
90
- /**
91
- * Options for creating activities handler
92
- */
93
- interface DeclareActivitiesHandlerOptions<TContract extends ContractDefinition> {
94
- contract: TContract;
95
- activities: ContractBoxedActivitiesImplementations<TContract>;
96
- }
97
- type ActivityImplementation<TActivity extends ActivityDefinition> = (args: WorkerInferInput<TActivity>) => Promise<WorkerInferOutput<TActivity>>;
98
- type ActivitiesImplementations<TActivities extends Record<string, ActivityDefinition>> = { [K in keyof TActivities]: ActivityImplementation<TActivities[K]> };
99
- type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
100
- /**
101
- * Activities handler ready for Temporal Worker
102
- *
103
- * Flat structure: all activities (global + all workflow-specific) are at the root level
104
- */
105
- 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"]]>;
106
- /**
107
- * Create a typed activities handler with automatic validation and Result pattern
108
- *
109
- * This wraps all activity implementations with:
110
- * - Validation at network boundaries
111
- * - Result<T, ActivityError> pattern for explicit error handling
112
- * - Automatic conversion from Result to Promise (throwing on Error)
113
- *
114
- * TypeScript ensures ALL activities (global + workflow-specific) are implemented.
115
- *
116
- * Use this to create the activities object for the Temporal Worker.
117
- *
118
- * @example
119
- * ```ts
120
- * import { declareActivitiesHandler, ActivityError } from '@temporal-contract/worker/activity';
121
- * import { Result, Future } from '@swan-io/boxed';
122
- * import myContract from './contract';
123
- *
124
- * export const activities = declareActivitiesHandler({
125
- * contract: myContract,
126
- * activities: {
127
- * // Activity returns Result instead of throwing
128
- * // All technical exceptions must be wrapped in ActivityError for retry policies
129
- * sendEmail: (args) => {
130
- * return Future.make(async resolve => {
131
- * try {
132
- * await emailService.send(args);
133
- * resolve(Result.Ok({ sent: true }));
134
- * } catch (error) {
135
- * // Wrap technical errors in ActivityError to enable retries
136
- * resolve(Result.Error(
137
- * new ActivityError(
138
- * 'EMAIL_SEND_FAILED',
139
- * 'Failed to send email',
140
- * error // Original error as cause for debugging
141
- * )
142
- * ));
143
- * }
144
- * });
145
- * },
146
- * },
147
- * });
148
- *
149
- * // Use with Temporal Worker
150
- * import { Worker } from '@temporalio/worker';
151
- *
152
- * const worker = await Worker.create({
153
- * workflowsPath: require.resolve('./workflows'),
154
- * activities: activities,
155
- * taskQueue: contract.taskQueue,
156
- * });
157
- * ```
158
- */
159
- declare function declareActivitiesHandler<TContract extends ContractDefinition>(options: DeclareActivitiesHandlerOptions<TContract>): ActivitiesHandler<TContract>;
160
- //#endregion
161
- export { getWorkflowActivityNames as a, getWorkflowActivities as i, ActivityError as n, getWorkflowNames as o, declareActivitiesHandler as r, isWorkflowActivity as s, ActivitiesHandler as t };