@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.
- package/README.md +6 -4
- package/dist/activity-utils-B3vP03_P.d.mts +68 -0
- package/dist/activity-utils-B3vP03_P.d.mts.map +1 -0
- package/dist/activity-utils-RsXOceIH.d.cts +68 -0
- package/dist/activity-utils-RsXOceIH.d.cts.map +1 -0
- package/dist/activity.cjs +1 -0
- package/dist/activity.d.cts +99 -3
- package/dist/activity.d.cts.map +1 -0
- package/dist/activity.d.mts +99 -3
- package/dist/activity.d.mts.map +1 -0
- package/dist/activity.mjs +3 -2
- package/dist/activity.mjs.map +1 -0
- package/dist/{errors-Vr-sKdW7.d.mts → errors-4jH78z8m.d.cts} +2 -1
- package/dist/errors-4jH78z8m.d.cts.map +1 -0
- package/dist/{errors-CXpHOFmk.d.cts → errors-CmTXZ3JW.d.mts} +2 -1
- package/dist/errors-CmTXZ3JW.d.mts.map +1 -0
- package/dist/{errors-BqVTpfcf.mjs → errors-Di6Ja4Rt.mjs} +2 -1
- package/dist/errors-Di6Ja4Rt.mjs.map +1 -0
- package/dist/worker.cjs +11 -5
- package/dist/worker.d.cts +14 -7
- package/dist/worker.d.cts.map +1 -0
- package/dist/worker.d.mts +14 -7
- package/dist/worker.d.mts.map +1 -0
- package/dist/worker.mjs +12 -6
- package/dist/worker.mjs.map +1 -0
- package/dist/workflow.cjs +92 -106
- package/dist/workflow.d.cts +32 -31
- package/dist/workflow.d.cts.map +1 -0
- package/dist/workflow.d.mts +32 -31
- package/dist/workflow.d.mts.map +1 -0
- package/dist/workflow.mjs +94 -108
- package/dist/workflow.mjs.map +1 -0
- package/package.json +13 -11
- package/dist/activity-5pVNjW7l.d.cts +0 -161
- package/dist/activity-BUEBZ7SL.d.mts +0 -161
package/dist/worker.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ActivitiesHandler } from "./activity.mjs";
|
|
2
2
|
import { Worker, WorkerOptions } from "@temporalio/worker";
|
|
3
3
|
import { ContractDefinition } from "@temporal-contract/contract";
|
|
4
4
|
|
|
@@ -6,7 +6,7 @@ import { ContractDefinition } from "@temporal-contract/contract";
|
|
|
6
6
|
/**
|
|
7
7
|
* Options for creating a Temporal worker
|
|
8
8
|
*/
|
|
9
|
-
|
|
9
|
+
type CreateWorkerOptions<TContract extends ContractDefinition> = Omit<WorkerOptions, "activities" | "taskQueue"> & {
|
|
10
10
|
/**
|
|
11
11
|
* The contract definition for this worker
|
|
12
12
|
*/
|
|
@@ -15,7 +15,7 @@ interface CreateWorkerOptions<TContract extends ContractDefinition> extends Omit
|
|
|
15
15
|
* Activities handler for this worker
|
|
16
16
|
*/
|
|
17
17
|
activities: ActivitiesHandler<TContract>;
|
|
18
|
-
}
|
|
18
|
+
};
|
|
19
19
|
/**
|
|
20
20
|
* Create a typed Temporal worker with contract-based configuration
|
|
21
21
|
*
|
|
@@ -46,9 +46,14 @@ interface CreateWorkerOptions<TContract extends ContractDefinition> extends Omit
|
|
|
46
46
|
*/
|
|
47
47
|
declare function createWorker<TContract extends ContractDefinition>(options: CreateWorkerOptions<TContract>): Promise<Worker>;
|
|
48
48
|
/**
|
|
49
|
-
* Helper to
|
|
49
|
+
* Helper to resolve a workflow file path relative to the current module's URL.
|
|
50
50
|
*
|
|
51
|
-
* Useful
|
|
51
|
+
* Useful when using ES modules (`import.meta.url`) to locate workflow files.
|
|
52
|
+
* The `relativePath` should include the file extension explicitly (e.g. `./workflows.js`)
|
|
53
|
+
* to ensure the resolved path is unambiguous in both source and built contexts.
|
|
54
|
+
*
|
|
55
|
+
* @param baseURL - The base URL to resolve from, typically `import.meta.url`
|
|
56
|
+
* @param relativePath - Relative path to the workflows file, **including extension**
|
|
52
57
|
*
|
|
53
58
|
* @example
|
|
54
59
|
* ```ts
|
|
@@ -57,11 +62,13 @@ declare function createWorker<TContract extends ContractDefinition>(options: Cre
|
|
|
57
62
|
* const worker = await createWorker({
|
|
58
63
|
* contract: myContract,
|
|
59
64
|
* connection,
|
|
60
|
-
*
|
|
65
|
+
* // Include the extension explicitly to work in both source (.ts) and build (.js) contexts
|
|
66
|
+
* workflowsPath: workflowsPathFromURL(import.meta.url, './workflows.js'),
|
|
61
67
|
* activities,
|
|
62
68
|
* });
|
|
63
69
|
* ```
|
|
64
70
|
*/
|
|
65
71
|
declare function workflowsPathFromURL(baseURL: string, relativePath: string): string;
|
|
66
72
|
//#endregion
|
|
67
|
-
export { CreateWorkerOptions, createWorker, workflowsPathFromURL };
|
|
73
|
+
export { CreateWorkerOptions, createWorker, workflowsPathFromURL };
|
|
74
|
+
//# sourceMappingURL=worker.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker.d.mts","names":[],"sources":["../src/worker.ts"],"mappings":";;;;;;;AASA;KAAY,mBAAA,mBAAsC,kBAAA,IAAsB,IAAA,CACtE,aAAA;EAD6B;;;EAO7B,QAAA,EAAU,SAAA;EAAA;;;EAKV,UAAA,EAAY,iBAAA,CAAkB,SAAA;AAAA;;;;;;;;;;;;AA+BhC;;;;;;;;;;;;;;;;;iBAAsB,YAAA,mBAA+B,kBAAA,CAAA,CACnD,OAAA,EAAS,mBAAA,CAAoB,SAAA,IAC5B,OAAA,CAAQ,MAAA;;AAkCX;;;;;;;;;;;;;;;;;;;;;;iBAAgB,oBAAA,CAAqB,OAAA,UAAiB,YAAA"}
|
package/dist/worker.mjs
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { Worker } from "@temporalio/worker";
|
|
2
2
|
import { fileURLToPath } from "node:url";
|
|
3
|
-
import { extname } from "node:path";
|
|
4
3
|
|
|
5
4
|
//#region src/worker.ts
|
|
6
5
|
/**
|
|
@@ -40,9 +39,14 @@ async function createWorker(options) {
|
|
|
40
39
|
});
|
|
41
40
|
}
|
|
42
41
|
/**
|
|
43
|
-
* Helper to
|
|
42
|
+
* Helper to resolve a workflow file path relative to the current module's URL.
|
|
44
43
|
*
|
|
45
|
-
* Useful
|
|
44
|
+
* Useful when using ES modules (`import.meta.url`) to locate workflow files.
|
|
45
|
+
* The `relativePath` should include the file extension explicitly (e.g. `./workflows.js`)
|
|
46
|
+
* to ensure the resolved path is unambiguous in both source and built contexts.
|
|
47
|
+
*
|
|
48
|
+
* @param baseURL - The base URL to resolve from, typically `import.meta.url`
|
|
49
|
+
* @param relativePath - Relative path to the workflows file, **including extension**
|
|
46
50
|
*
|
|
47
51
|
* @example
|
|
48
52
|
* ```ts
|
|
@@ -51,14 +55,16 @@ async function createWorker(options) {
|
|
|
51
55
|
* const worker = await createWorker({
|
|
52
56
|
* contract: myContract,
|
|
53
57
|
* connection,
|
|
54
|
-
*
|
|
58
|
+
* // Include the extension explicitly to work in both source (.ts) and build (.js) contexts
|
|
59
|
+
* workflowsPath: workflowsPathFromURL(import.meta.url, './workflows.js'),
|
|
55
60
|
* activities,
|
|
56
61
|
* });
|
|
57
62
|
* ```
|
|
58
63
|
*/
|
|
59
64
|
function workflowsPathFromURL(baseURL, relativePath) {
|
|
60
|
-
return fileURLToPath(new URL(
|
|
65
|
+
return fileURLToPath(new URL(relativePath, baseURL));
|
|
61
66
|
}
|
|
62
67
|
|
|
63
68
|
//#endregion
|
|
64
|
-
export { createWorker, workflowsPathFromURL };
|
|
69
|
+
export { createWorker, workflowsPathFromURL };
|
|
70
|
+
//# sourceMappingURL=worker.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker.mjs","names":[],"sources":["../src/worker.ts"],"sourcesContent":["// Entry point for worker creation utilities\nimport { ContractDefinition } from \"@temporal-contract/contract\";\nimport { Worker, WorkerOptions } from \"@temporalio/worker\";\nimport { fileURLToPath } from \"node:url\";\nimport type { ActivitiesHandler } from \"./activity.js\";\n\n/**\n * Options for creating a Temporal worker\n */\nexport type CreateWorkerOptions<TContract extends ContractDefinition> = Omit<\n WorkerOptions,\n \"activities\" | \"taskQueue\"\n> & {\n /**\n * The contract definition for this worker\n */\n contract: TContract;\n\n /**\n * Activities handler for this worker\n */\n activities: ActivitiesHandler<TContract>;\n};\n\n/**\n * Create a typed Temporal worker with contract-based configuration\n *\n * This helper simplifies worker creation by:\n * - Using the contract's task queue automatically\n * - Providing type-safe configuration\n *\n * @example\n * ```ts\n * import { NativeConnection } from '@temporalio/worker';\n * import { createWorker } from '@temporal-contract/worker/worker';\n * import { activities } from './activities';\n * import myContract from './contract';\n *\n * const connection = await NativeConnection.connect({\n * address: 'localhost:7233',\n * });\n *\n * const worker = await createWorker({\n * contract: myContract,\n * connection,\n * workflowsPath: require.resolve('./workflows'),\n * activities,\n * });\n *\n * await worker.run();\n * ```\n */\nexport async function createWorker<TContract extends ContractDefinition>(\n options: CreateWorkerOptions<TContract>,\n): Promise<Worker> {\n const { contract, activities, ...workerOptions } = options;\n\n // Create the worker with contract's task queue\n return await Worker.create({\n ...workerOptions,\n activities,\n taskQueue: contract.taskQueue,\n });\n}\n\n/**\n * Helper to resolve a workflow file path relative to the current module's URL.\n *\n * Useful when using ES modules (`import.meta.url`) to locate workflow files.\n * The `relativePath` should include the file extension explicitly (e.g. `./workflows.js`)\n * to ensure the resolved path is unambiguous in both source and built contexts.\n *\n * @param baseURL - The base URL to resolve from, typically `import.meta.url`\n * @param relativePath - Relative path to the workflows file, **including extension**\n *\n * @example\n * ```ts\n * import { workflowsPathFromURL } from '@temporal-contract/worker/worker';\n *\n * const worker = await createWorker({\n * contract: myContract,\n * connection,\n * // Include the extension explicitly to work in both source (.ts) and build (.js) contexts\n * workflowsPath: workflowsPathFromURL(import.meta.url, './workflows.js'),\n * activities,\n * });\n * ```\n */\nexport function workflowsPathFromURL(baseURL: string, relativePath: string): string {\n return fileURLToPath(new URL(relativePath, baseURL));\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoDA,eAAsB,aACpB,SACiB;CACjB,MAAM,EAAE,UAAU,YAAY,GAAG,kBAAkB;AAGnD,QAAO,MAAM,OAAO,OAAO;EACzB,GAAG;EACH;EACA,WAAW,SAAS;EACrB,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;AA0BJ,SAAgB,qBAAqB,SAAiB,cAA8B;AAClF,QAAO,cAAc,IAAI,IAAI,cAAc,QAAQ,CAAC"}
|
package/dist/workflow.cjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
1
2
|
const require_errors = require('./errors-DjSZg-93.cjs');
|
|
2
3
|
let _temporal_contract_boxed = require("@temporal-contract/boxed");
|
|
3
4
|
let _temporalio_workflow = require("@temporalio/workflow");
|
|
@@ -17,53 +18,53 @@ let _temporalio_workflow = require("@temporalio/workflow");
|
|
|
17
18
|
* @example
|
|
18
19
|
* ```ts
|
|
19
20
|
* // workflows/processOrder.ts
|
|
20
|
-
* import { declareWorkflow } from '@temporal-contract/worker';
|
|
21
|
+
* import { declareWorkflow } from '@temporal-contract/worker/workflow';
|
|
21
22
|
* import myContract from '../contract';
|
|
22
23
|
*
|
|
23
24
|
* export const processOrder = declareWorkflow({
|
|
24
25
|
* workflowName: 'processOrder',
|
|
25
26
|
* contract: myContract,
|
|
26
|
-
*
|
|
27
|
+
* activityOptions: {
|
|
28
|
+
* startToCloseTimeout: '1 minute',
|
|
29
|
+
* },
|
|
30
|
+
* implementation: async (context, args) => {
|
|
27
31
|
* // context.activities: typed activities (workflow + global)
|
|
28
32
|
* // context.info: WorkflowInfo
|
|
29
33
|
*
|
|
30
|
-
* const inventory = await context.activities.validateInventory(
|
|
34
|
+
* const inventory = await context.activities.validateInventory({
|
|
35
|
+
* orderId: args.orderId,
|
|
36
|
+
* });
|
|
31
37
|
*
|
|
32
38
|
* if (!inventory.available) {
|
|
33
|
-
*
|
|
39
|
+
* return { orderId: args.orderId, status: 'out_of_stock' };
|
|
34
40
|
* }
|
|
35
41
|
*
|
|
36
|
-
* const payment = await context.activities.chargePayment(
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
* customerId,
|
|
41
|
-
* 'Order processed',
|
|
42
|
-
* 'Your order has been processed'
|
|
43
|
-
* );
|
|
42
|
+
* const payment = await context.activities.chargePayment({
|
|
43
|
+
* customerId: args.customerId,
|
|
44
|
+
* amount: 100,
|
|
45
|
+
* });
|
|
44
46
|
*
|
|
45
47
|
* return {
|
|
46
|
-
* orderId,
|
|
48
|
+
* orderId: args.orderId,
|
|
47
49
|
* status: payment.success ? 'success' : 'failed',
|
|
48
50
|
* transactionId: payment.transactionId,
|
|
49
51
|
* };
|
|
50
52
|
* },
|
|
51
|
-
* activityOptions: {
|
|
52
|
-
* startToCloseTimeout: '1 minute',
|
|
53
|
-
* },
|
|
54
53
|
* });
|
|
55
54
|
* ```
|
|
56
55
|
*
|
|
57
56
|
* Then in your worker setup:
|
|
58
57
|
* ```ts
|
|
59
58
|
* // worker.ts
|
|
60
|
-
* import {
|
|
61
|
-
* import {
|
|
59
|
+
* import { createWorker } from '@temporal-contract/worker/worker';
|
|
60
|
+
* import { activities } from './activities';
|
|
61
|
+
* import myContract from './contract';
|
|
62
62
|
*
|
|
63
|
-
* const worker = await
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
63
|
+
* const worker = await createWorker({
|
|
64
|
+
* contract: myContract,
|
|
65
|
+
* connection,
|
|
66
|
+
* workflowsPath: workflowsPathFromURL(import.meta.url, './workflows.js'),
|
|
67
|
+
* activities,
|
|
67
68
|
* });
|
|
68
69
|
* ```
|
|
69
70
|
*/
|
|
@@ -76,20 +77,20 @@ function declareWorkflow({ workflowName, contract, implementation, activityOptio
|
|
|
76
77
|
const validatedInput = inputResult.value;
|
|
77
78
|
let contextActivities = {};
|
|
78
79
|
if (definition.activities || contract.activities) contextActivities = createValidatedActivities((0, _temporalio_workflow.proxyActivities)(activityOptions), definition.activities, contract.activities);
|
|
79
|
-
async function validateChildWorkflowOutput(childDefinition, result
|
|
80
|
-
const outputResult
|
|
81
|
-
if (outputResult
|
|
82
|
-
return _temporal_contract_boxed.Result.Ok(outputResult
|
|
80
|
+
async function validateChildWorkflowOutput(childDefinition, result, childWorkflowName) {
|
|
81
|
+
const outputResult = await childDefinition.output["~standard"].validate(result);
|
|
82
|
+
if (outputResult.issues) return _temporal_contract_boxed.Result.Error(new require_errors.ChildWorkflowError(`Child workflow "${childWorkflowName}" output validation failed: ${outputResult.issues.map((i) => i.message).join("; ")}`));
|
|
83
|
+
return _temporal_contract_boxed.Result.Ok(outputResult.value);
|
|
83
84
|
}
|
|
84
|
-
async function getAndValidateChildWorkflow(childContract, childWorkflowName, args
|
|
85
|
+
async function getAndValidateChildWorkflow(childContract, childWorkflowName, args) {
|
|
85
86
|
const childDefinition = childContract.workflows[childWorkflowName];
|
|
86
87
|
if (!childDefinition) return _temporal_contract_boxed.Result.Error(new require_errors.ChildWorkflowNotFoundError(String(childWorkflowName), Object.keys(childContract.workflows)));
|
|
87
|
-
const inputResult
|
|
88
|
-
if (inputResult
|
|
89
|
-
const validatedInput
|
|
88
|
+
const inputResult = await childDefinition.input["~standard"].validate(args);
|
|
89
|
+
if (inputResult.issues) return _temporal_contract_boxed.Result.Error(new require_errors.ChildWorkflowError(`Child workflow "${String(childWorkflowName)}" input validation failed: ${inputResult.issues.map((i) => i.message).join("; ")}`));
|
|
90
|
+
const validatedInput = inputResult.value;
|
|
90
91
|
return _temporal_contract_boxed.Result.Ok({
|
|
91
92
|
definition: childDefinition,
|
|
92
|
-
validatedInput
|
|
93
|
+
validatedInput,
|
|
93
94
|
taskQueue: childContract.taskQueue
|
|
94
95
|
});
|
|
95
96
|
}
|
|
@@ -97,107 +98,92 @@ function declareWorkflow({ workflowName, contract, implementation, activityOptio
|
|
|
97
98
|
return {
|
|
98
99
|
workflowId: handle.workflowId,
|
|
99
100
|
result: () => {
|
|
100
|
-
return _temporal_contract_boxed.Future.
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
}
|
|
107
|
-
})();
|
|
101
|
+
return _temporal_contract_boxed.Future.fromAsync(async () => {
|
|
102
|
+
try {
|
|
103
|
+
return validateChildWorkflowOutput(childDefinition, await handle.result(), childWorkflowName);
|
|
104
|
+
} catch (error) {
|
|
105
|
+
return _temporal_contract_boxed.Result.Error(new require_errors.ChildWorkflowError(`Child workflow execution failed: ${error instanceof Error ? error.message : String(error)}`, error));
|
|
106
|
+
}
|
|
108
107
|
});
|
|
109
108
|
}
|
|
110
109
|
};
|
|
111
110
|
}
|
|
112
111
|
function createStartChildWorkflow(childContract, childWorkflowName, options) {
|
|
113
|
-
return _temporal_contract_boxed.Future.
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
resolve(_temporal_contract_boxed.Result.Ok(typedHandle));
|
|
129
|
-
} catch (error) {
|
|
130
|
-
resolve(_temporal_contract_boxed.Result.Error(new require_errors.ChildWorkflowError(`Failed to start child workflow: ${error instanceof Error ? error.message : String(error)}`, error)));
|
|
131
|
-
}
|
|
132
|
-
})();
|
|
112
|
+
return _temporal_contract_boxed.Future.fromAsync(async () => {
|
|
113
|
+
const validationResult = await getAndValidateChildWorkflow(childContract, childWorkflowName, options.args);
|
|
114
|
+
if (validationResult.isError()) return _temporal_contract_boxed.Result.Error(validationResult.error);
|
|
115
|
+
const { definition: childDefinition, validatedInput, taskQueue } = validationResult.value;
|
|
116
|
+
try {
|
|
117
|
+
const { args: _args, ...temporalOptions } = options;
|
|
118
|
+
const typedHandle = createTypedChildHandle(await (0, _temporalio_workflow.startChild)(childWorkflowName, {
|
|
119
|
+
...temporalOptions,
|
|
120
|
+
taskQueue,
|
|
121
|
+
args: [validatedInput]
|
|
122
|
+
}), childDefinition, String(childWorkflowName));
|
|
123
|
+
return _temporal_contract_boxed.Result.Ok(typedHandle);
|
|
124
|
+
} catch (error) {
|
|
125
|
+
return _temporal_contract_boxed.Result.Error(new require_errors.ChildWorkflowError(`Failed to start child workflow: ${error instanceof Error ? error.message : String(error)}`, error));
|
|
126
|
+
}
|
|
133
127
|
});
|
|
134
128
|
}
|
|
135
129
|
function createExecuteChildWorkflow(childContract, childWorkflowName, options) {
|
|
136
|
-
return _temporal_contract_boxed.Future.
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
resolve(_temporal_contract_boxed.Result.Error(outputValidationResult.error));
|
|
153
|
-
return;
|
|
154
|
-
}
|
|
155
|
-
resolve(_temporal_contract_boxed.Result.Ok(outputValidationResult.value));
|
|
156
|
-
} catch (error) {
|
|
157
|
-
resolve(_temporal_contract_boxed.Result.Error(new require_errors.ChildWorkflowError(`Failed to execute child workflow: ${error instanceof Error ? error.message : String(error)}`, error)));
|
|
158
|
-
}
|
|
159
|
-
})();
|
|
130
|
+
return _temporal_contract_boxed.Future.fromAsync(async () => {
|
|
131
|
+
const validationResult = await getAndValidateChildWorkflow(childContract, childWorkflowName, options.args);
|
|
132
|
+
if (validationResult.isError()) return _temporal_contract_boxed.Result.Error(validationResult.error);
|
|
133
|
+
const { definition: childDefinition, validatedInput, taskQueue } = validationResult.value;
|
|
134
|
+
try {
|
|
135
|
+
const { args: _args, ...temporalOptions } = options;
|
|
136
|
+
const outputValidationResult = await validateChildWorkflowOutput(childDefinition, await (0, _temporalio_workflow.executeChild)(childWorkflowName, {
|
|
137
|
+
...temporalOptions,
|
|
138
|
+
taskQueue,
|
|
139
|
+
args: [validatedInput]
|
|
140
|
+
}), String(childWorkflowName));
|
|
141
|
+
if (outputValidationResult.isError()) return _temporal_contract_boxed.Result.Error(outputValidationResult.error);
|
|
142
|
+
return _temporal_contract_boxed.Result.Ok(outputValidationResult.value);
|
|
143
|
+
} catch (error) {
|
|
144
|
+
return _temporal_contract_boxed.Result.Error(new require_errors.ChildWorkflowError(`Failed to execute child workflow: ${error instanceof Error ? error.message : String(error)}`, error));
|
|
145
|
+
}
|
|
160
146
|
});
|
|
161
147
|
}
|
|
162
148
|
function createDefineSignal(signalName, handler) {
|
|
163
149
|
if (!definition.signals) throw new Error(`Signal "${String(signalName)}" cannot be defined: workflow "${String(workflowName)}" has no signals in its contract`);
|
|
164
150
|
const signalDef = definition.signals[signalName];
|
|
165
151
|
if (!signalDef) throw new Error(`Signal "${String(signalName)}" not found in workflow "${String(workflowName)}" contract`);
|
|
166
|
-
(0, _temporalio_workflow.setHandler)((0, _temporalio_workflow.defineSignal)(signalName), async (...args
|
|
167
|
-
const input
|
|
168
|
-
const inputResult
|
|
169
|
-
if (inputResult
|
|
170
|
-
await handler(inputResult
|
|
152
|
+
(0, _temporalio_workflow.setHandler)((0, _temporalio_workflow.defineSignal)(signalName), async (...args) => {
|
|
153
|
+
const input = args.length === 1 ? args[0] : args;
|
|
154
|
+
const inputResult = await signalDef.input["~standard"].validate(input);
|
|
155
|
+
if (inputResult.issues) throw new require_errors.SignalInputValidationError(signalName, inputResult.issues);
|
|
156
|
+
await handler(inputResult.value);
|
|
171
157
|
});
|
|
172
158
|
}
|
|
173
159
|
function createDefineQuery(queryName, handler) {
|
|
174
160
|
if (!definition.queries) throw new Error(`Query "${String(queryName)}" cannot be defined: workflow "${String(workflowName)}" has no queries in its contract`);
|
|
175
161
|
const queryDef = definition.queries[queryName];
|
|
176
162
|
if (!queryDef) throw new Error(`Query "${String(queryName)}" not found in workflow "${String(workflowName)}" contract`);
|
|
177
|
-
(0, _temporalio_workflow.setHandler)((0, _temporalio_workflow.defineQuery)(queryName), (...args
|
|
178
|
-
const input
|
|
179
|
-
const inputResult
|
|
180
|
-
if (inputResult
|
|
181
|
-
if (inputResult
|
|
182
|
-
const result
|
|
183
|
-
const outputResult
|
|
184
|
-
if (outputResult
|
|
185
|
-
if (outputResult
|
|
186
|
-
return outputResult
|
|
163
|
+
(0, _temporalio_workflow.setHandler)((0, _temporalio_workflow.defineQuery)(queryName), (...args) => {
|
|
164
|
+
const input = args.length === 1 ? args[0] : args;
|
|
165
|
+
const inputResult = queryDef.input["~standard"].validate(input);
|
|
166
|
+
if (inputResult instanceof Promise) throw new Error(`Query "${String(queryName)}" validation must be synchronous. Use a schema library that supports synchronous validation for queries.`);
|
|
167
|
+
if (inputResult.issues) throw new require_errors.QueryInputValidationError(queryName, inputResult.issues);
|
|
168
|
+
const result = handler(inputResult.value);
|
|
169
|
+
const outputResult = queryDef.output["~standard"].validate(result);
|
|
170
|
+
if (outputResult instanceof Promise) throw new Error(`Query "${String(queryName)}" output validation must be synchronous. Use a schema library that supports synchronous validation for queries.`);
|
|
171
|
+
if (outputResult.issues) throw new require_errors.QueryOutputValidationError(queryName, outputResult.issues);
|
|
172
|
+
return outputResult.value;
|
|
187
173
|
});
|
|
188
174
|
}
|
|
189
175
|
function createDefineUpdate(updateName, handler) {
|
|
190
176
|
if (!definition.updates) throw new Error(`Update "${String(updateName)}" cannot be defined: workflow "${String(workflowName)}" has no updates in its contract`);
|
|
191
177
|
const updateDef = definition.updates[updateName];
|
|
192
178
|
if (!updateDef) throw new Error(`Update "${String(updateName)}" not found in workflow "${String(workflowName)}" contract`);
|
|
193
|
-
(0, _temporalio_workflow.setHandler)((0, _temporalio_workflow.defineUpdate)(updateName), async (...args
|
|
194
|
-
const input
|
|
195
|
-
const inputResult
|
|
196
|
-
if (inputResult
|
|
197
|
-
const result
|
|
198
|
-
const outputResult
|
|
199
|
-
if (outputResult
|
|
200
|
-
return outputResult
|
|
179
|
+
(0, _temporalio_workflow.setHandler)((0, _temporalio_workflow.defineUpdate)(updateName), async (...args) => {
|
|
180
|
+
const input = args.length === 1 ? args[0] : args;
|
|
181
|
+
const inputResult = await updateDef.input["~standard"].validate(input);
|
|
182
|
+
if (inputResult.issues) throw new require_errors.UpdateInputValidationError(updateName, inputResult.issues);
|
|
183
|
+
const result = await handler(inputResult.value);
|
|
184
|
+
const outputResult = await updateDef.output["~standard"].validate(result);
|
|
185
|
+
if (outputResult.issues) throw new require_errors.UpdateOutputValidationError(updateName, outputResult.issues);
|
|
186
|
+
return outputResult.value;
|
|
201
187
|
});
|
|
202
188
|
}
|
|
203
189
|
const result = await implementation({
|
package/dist/workflow.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as ChildWorkflowNotFoundError, c as SignalInputValidationError, d as WorkflowInputValidationError, f as WorkflowOutputValidationError, g as WorkerInferOutput, h as WorkerInferInput, i as ChildWorkflowError, l as UpdateInputValidationError, m as ClientInferOutput, n as ActivityInputValidationError, o as QueryInputValidationError, p as ClientInferInput, r as ActivityOutputValidationError, s as QueryOutputValidationError, u as UpdateOutputValidationError } from "./errors-
|
|
1
|
+
import { a as ChildWorkflowNotFoundError, c as SignalInputValidationError, d as WorkflowInputValidationError, f as WorkflowOutputValidationError, g as WorkerInferOutput, h as WorkerInferInput, i as ChildWorkflowError, l as UpdateInputValidationError, m as ClientInferOutput, n as ActivityInputValidationError, o as QueryInputValidationError, p as ClientInferInput, r as ActivityOutputValidationError, s as QueryOutputValidationError, u as UpdateOutputValidationError } from "./errors-4jH78z8m.cjs";
|
|
2
2
|
import { ActivityDefinition, ContractDefinition, QueryDefinition, SignalDefinition, UpdateDefinition, WorkflowDefinition } from "@temporal-contract/contract";
|
|
3
3
|
import { Future, Result } from "@temporal-contract/boxed";
|
|
4
4
|
import { ActivityOptions, ChildWorkflowOptions, WorkflowInfo } from "@temporalio/workflow";
|
|
@@ -18,53 +18,53 @@ import { ActivityOptions, ChildWorkflowOptions, WorkflowInfo } from "@temporalio
|
|
|
18
18
|
* @example
|
|
19
19
|
* ```ts
|
|
20
20
|
* // workflows/processOrder.ts
|
|
21
|
-
* import { declareWorkflow } from '@temporal-contract/worker';
|
|
21
|
+
* import { declareWorkflow } from '@temporal-contract/worker/workflow';
|
|
22
22
|
* import myContract from '../contract';
|
|
23
23
|
*
|
|
24
24
|
* export const processOrder = declareWorkflow({
|
|
25
25
|
* workflowName: 'processOrder',
|
|
26
26
|
* contract: myContract,
|
|
27
|
-
*
|
|
27
|
+
* activityOptions: {
|
|
28
|
+
* startToCloseTimeout: '1 minute',
|
|
29
|
+
* },
|
|
30
|
+
* implementation: async (context, args) => {
|
|
28
31
|
* // context.activities: typed activities (workflow + global)
|
|
29
32
|
* // context.info: WorkflowInfo
|
|
30
33
|
*
|
|
31
|
-
* const inventory = await context.activities.validateInventory(
|
|
34
|
+
* const inventory = await context.activities.validateInventory({
|
|
35
|
+
* orderId: args.orderId,
|
|
36
|
+
* });
|
|
32
37
|
*
|
|
33
38
|
* if (!inventory.available) {
|
|
34
|
-
*
|
|
39
|
+
* return { orderId: args.orderId, status: 'out_of_stock' };
|
|
35
40
|
* }
|
|
36
41
|
*
|
|
37
|
-
* const payment = await context.activities.chargePayment(
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
* customerId,
|
|
42
|
-
* 'Order processed',
|
|
43
|
-
* 'Your order has been processed'
|
|
44
|
-
* );
|
|
42
|
+
* const payment = await context.activities.chargePayment({
|
|
43
|
+
* customerId: args.customerId,
|
|
44
|
+
* amount: 100,
|
|
45
|
+
* });
|
|
45
46
|
*
|
|
46
47
|
* return {
|
|
47
|
-
* orderId,
|
|
48
|
+
* orderId: args.orderId,
|
|
48
49
|
* status: payment.success ? 'success' : 'failed',
|
|
49
50
|
* transactionId: payment.transactionId,
|
|
50
51
|
* };
|
|
51
52
|
* },
|
|
52
|
-
* activityOptions: {
|
|
53
|
-
* startToCloseTimeout: '1 minute',
|
|
54
|
-
* },
|
|
55
53
|
* });
|
|
56
54
|
* ```
|
|
57
55
|
*
|
|
58
56
|
* Then in your worker setup:
|
|
59
57
|
* ```ts
|
|
60
58
|
* // worker.ts
|
|
61
|
-
* import {
|
|
62
|
-
* import {
|
|
59
|
+
* import { createWorker } from '@temporal-contract/worker/worker';
|
|
60
|
+
* import { activities } from './activities';
|
|
61
|
+
* import myContract from './contract';
|
|
63
62
|
*
|
|
64
|
-
* const worker = await
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
63
|
+
* const worker = await createWorker({
|
|
64
|
+
* contract: myContract,
|
|
65
|
+
* connection,
|
|
66
|
+
* workflowsPath: workflowsPathFromURL(import.meta.url, './workflows.js'),
|
|
67
|
+
* activities,
|
|
68
68
|
* });
|
|
69
69
|
* ```
|
|
70
70
|
*/
|
|
@@ -98,7 +98,7 @@ type UpdateHandlerImplementation<TUpdate extends UpdateDefinition> = (args: Work
|
|
|
98
98
|
/**
|
|
99
99
|
* Options for declaring a workflow implementation
|
|
100
100
|
*/
|
|
101
|
-
|
|
101
|
+
type DeclareWorkflowOptions<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"]> = {
|
|
102
102
|
workflowName: TWorkflowName;
|
|
103
103
|
contract: TContract;
|
|
104
104
|
implementation: WorkflowImplementation<TContract, TWorkflowName>;
|
|
@@ -120,7 +120,7 @@ interface DeclareWorkflowOptions<TContract extends ContractDefinition, TWorkflow
|
|
|
120
120
|
* ```
|
|
121
121
|
*/
|
|
122
122
|
activityOptions: ActivityOptions;
|
|
123
|
-
}
|
|
123
|
+
};
|
|
124
124
|
/**
|
|
125
125
|
* Workflow implementation function
|
|
126
126
|
*
|
|
@@ -137,7 +137,7 @@ type WorkflowImplementation<TContract extends ContractDefinition, TWorkflowName
|
|
|
137
137
|
* - Signal, query, and update handler registration
|
|
138
138
|
* - Child workflow execution capabilities
|
|
139
139
|
*/
|
|
140
|
-
|
|
140
|
+
type WorkflowContext<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"]> = {
|
|
141
141
|
activities: WorkflowInferWorkflowContextActivities<TContract, TWorkflowName>;
|
|
142
142
|
info: WorkflowInfo;
|
|
143
143
|
/**
|
|
@@ -254,7 +254,7 @@ interface WorkflowContext<TContract extends ContractDefinition, TWorkflowName ex
|
|
|
254
254
|
* ```
|
|
255
255
|
*/
|
|
256
256
|
executeChildWorkflow: <TChildContract extends ContractDefinition, TChildWorkflowName extends keyof TChildContract["workflows"]>(contract: TChildContract, workflowName: TChildWorkflowName, options: TypedChildWorkflowOptions<TChildContract, TChildWorkflowName>) => Future<Result<ClientInferOutput<TChildContract["workflows"][TChildWorkflowName]>, ChildWorkflowError>>;
|
|
257
|
-
}
|
|
257
|
+
};
|
|
258
258
|
/**
|
|
259
259
|
* Options for starting a child workflow
|
|
260
260
|
*/
|
|
@@ -264,7 +264,7 @@ type TypedChildWorkflowOptions<TChildContract extends ContractDefinition, TChild
|
|
|
264
264
|
/**
|
|
265
265
|
* Typed handle for a child workflow with Future/Result pattern
|
|
266
266
|
*/
|
|
267
|
-
|
|
267
|
+
type TypedChildWorkflowHandle<TWorkflow extends WorkflowDefinition> = {
|
|
268
268
|
/**
|
|
269
269
|
* Get child workflow result with Result pattern
|
|
270
270
|
*/
|
|
@@ -273,7 +273,7 @@ interface TypedChildWorkflowHandle<TWorkflow extends WorkflowDefinition> {
|
|
|
273
273
|
* Child workflow ID
|
|
274
274
|
*/
|
|
275
275
|
workflowId: string;
|
|
276
|
-
}
|
|
276
|
+
};
|
|
277
277
|
/**
|
|
278
278
|
* Activity function signature from workflow execution perspective
|
|
279
279
|
*
|
|
@@ -293,6 +293,7 @@ type WorkflowInferWorkflowActivities<T extends WorkflowDefinition> = T["activiti
|
|
|
293
293
|
*
|
|
294
294
|
* Combines workflow-specific activities with global contract activities
|
|
295
295
|
*/
|
|
296
|
-
type WorkflowInferWorkflowContextActivities<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"]> = WorkflowInferWorkflowActivities<TContract["workflows"][TWorkflowName]> & WorkflowInferActivities<TContract>;
|
|
296
|
+
type WorkflowInferWorkflowContextActivities<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"]> = WorkflowInferWorkflowActivities<TContract["workflows"][TWorkflowName]> & WorkflowInferActivities<TContract>; //# sourceMappingURL=workflow.d.ts.map
|
|
297
297
|
//#endregion
|
|
298
|
-
export { ActivityInputValidationError, ActivityOutputValidationError, ChildWorkflowError, ChildWorkflowNotFoundError, QueryInputValidationError, QueryOutputValidationError, SignalInputValidationError, UpdateInputValidationError, UpdateOutputValidationError, WorkflowInputValidationError, WorkflowOutputValidationError, declareWorkflow };
|
|
298
|
+
export { ActivityInputValidationError, ActivityOutputValidationError, ChildWorkflowError, ChildWorkflowNotFoundError, QueryInputValidationError, QueryOutputValidationError, SignalInputValidationError, UpdateInputValidationError, UpdateOutputValidationError, WorkflowInputValidationError, WorkflowOutputValidationError, declareWorkflow };
|
|
299
|
+
//# sourceMappingURL=workflow.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"workflow.d.cts","names":[],"sources":["../src/workflow.ts"],"mappings":";;;;;;AA2HA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8aC;;;;;;;;;;;;;;;;;AAUkB;;;;;;;AAxbnB,iBAAgB,eAAA,mBACI,kBAAA,8BACU,SAAA,cAAA,CAAA;EAE5B,YAAA;EACA,QAAA;EACA,cAAA;EACA;AAAA,GACC,sBAAA,CAAuB,SAAA,EAAW,aAAA,QAChC,IAAA,gBACA,OAAA,CAAQ,iBAAA,CAAkB,SAAA,cAAuB,aAAA;;;;;;;KA4ajD,2BAAA,iBAA4C,gBAAA,KAC/C,IAAA,EAAM,gBAAA,CAAiB,OAAA,aACb,OAAA;;;;;AAUiB;;KAFxB,0BAAA,gBAA0C,eAAA,KAC7C,IAAA,EAAM,gBAAA,CAAiB,MAAA,MACpB,iBAAA,CAAkB,MAAA;;;;;;;KAQlB,2BAAA,iBAA4C,gBAAA,KAC/C,IAAA,EAAM,gBAAA,CAAiB,OAAA,MACpB,OAAA,CAAQ,iBAAA,CAAkB,OAAA;;;;KAK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