@temporal-contract/worker 0.0.7 → 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,162 +0,0 @@
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 };