@hatchet-dev/typescript-sdk 1.0.3 → 1.0.5
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/clients/dispatcher/heartbeat/heartbeat-controller.d.ts +5 -0
- package/clients/dispatcher/heartbeat/heartbeat-controller.js +6 -2
- package/clients/dispatcher/heartbeat/heartbeat-worker.js +37 -5
- package/package.json +4 -2
- package/step.d.ts +16 -18
- package/step.js +38 -16
- package/v1/client/client.d.ts +22 -21
- package/v1/client/client.js +8 -8
- package/v1/declaration.d.ts +47 -42
- package/v1/declaration.js +8 -8
- package/v1/examples/cancellations/workflow.d.ts +2 -2
- package/v1/examples/durable-sleep/workflow.d.ts +1 -1
- package/v1/examples/on_failure/workflow.d.ts +1 -1
- package/v1/examples/on_success/workflow.d.ts +1 -1
- package/v1/examples/rate_limit/workflow.d.ts +1 -1
- package/v1/examples/retries/workflow.d.ts +3 -3
- package/v1/examples/sticky/workflow.d.ts +1 -1
- package/v1/examples/timeouts/workflow.d.ts +2 -2
- package/v1/index.d.ts +1 -0
- package/v1/index.js +1 -0
- package/v1/task.d.ts +26 -25
- package/v1/types.d.ts +17 -0
- package/v1/types.js +2 -0
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/workflow.d.ts +8 -8
|
@@ -3,6 +3,11 @@ import { DispatcherClient as PbDispatcherClient } from '../../../protoc/dispatch
|
|
|
3
3
|
import { Worker } from 'worker_threads';
|
|
4
4
|
import { ClientConfig } from '../../hatchet-client';
|
|
5
5
|
import { DispatcherClient } from '../dispatcher-client';
|
|
6
|
+
export interface HeartbeatMessage {
|
|
7
|
+
type: 'info' | 'warn' | 'error' | 'debug';
|
|
8
|
+
message: string;
|
|
9
|
+
}
|
|
10
|
+
export declare const STOP_HEARTBEAT = "stop";
|
|
6
11
|
export declare class Heartbeat {
|
|
7
12
|
config: ClientConfig;
|
|
8
13
|
client: PbDispatcherClient;
|
|
@@ -12,9 +12,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
12
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.Heartbeat = void 0;
|
|
15
|
+
exports.Heartbeat = exports.STOP_HEARTBEAT = void 0;
|
|
16
16
|
const path_1 = __importDefault(require("path"));
|
|
17
17
|
const thread_helper_1 = require("../../../util/thread-helper");
|
|
18
|
+
exports.STOP_HEARTBEAT = 'stop';
|
|
18
19
|
class Heartbeat {
|
|
19
20
|
constructor(client, workerId) {
|
|
20
21
|
this.config = client.config;
|
|
@@ -31,13 +32,16 @@ class Heartbeat {
|
|
|
31
32
|
workerId: this.workerId,
|
|
32
33
|
},
|
|
33
34
|
});
|
|
35
|
+
this.heartbeatWorker.on('message', (message) => {
|
|
36
|
+
this.logger[message.type](message.message);
|
|
37
|
+
});
|
|
34
38
|
}
|
|
35
39
|
});
|
|
36
40
|
}
|
|
37
41
|
stop() {
|
|
38
42
|
return __awaiter(this, void 0, void 0, function* () {
|
|
39
43
|
var _a, _b;
|
|
40
|
-
(_a = this.heartbeatWorker) === null || _a === void 0 ? void 0 : _a.postMessage(
|
|
44
|
+
(_a = this.heartbeatWorker) === null || _a === void 0 ? void 0 : _a.postMessage(exports.STOP_HEARTBEAT);
|
|
41
45
|
(_b = this.heartbeatWorker) === null || _b === void 0 ? void 0 : _b.terminate();
|
|
42
46
|
this.heartbeatWorker = undefined;
|
|
43
47
|
});
|
|
@@ -14,16 +14,25 @@ const hatchet_client_1 = require("../../hatchet-client");
|
|
|
14
14
|
const config_loader_1 = require("../../../util/config-loader");
|
|
15
15
|
const nice_grpc_1 = require("nice-grpc");
|
|
16
16
|
const dispatcher_client_1 = require("../dispatcher-client");
|
|
17
|
+
const heartbeat_controller_1 = require("./heartbeat-controller");
|
|
17
18
|
const HEARTBEAT_INTERVAL = 4000;
|
|
19
|
+
const postMessage = (message) => {
|
|
20
|
+
worker_threads_1.parentPort === null || worker_threads_1.parentPort === void 0 ? void 0 : worker_threads_1.parentPort.postMessage(message);
|
|
21
|
+
};
|
|
18
22
|
class HeartbeatWorker {
|
|
19
23
|
constructor(config, workerId) {
|
|
20
24
|
this.timeLastHeartbeat = new Date().getTime();
|
|
21
25
|
this.workerId = workerId;
|
|
22
|
-
this.logger = new hatchet_client_1.HatchetLogger(`
|
|
26
|
+
this.logger = new hatchet_client_1.HatchetLogger(`HeartbeatThread`, config.log_level);
|
|
27
|
+
this.logger.debug('Heartbeat thread starting...');
|
|
23
28
|
const credentials = config_loader_1.ConfigLoader.createCredentials(config.tls_config);
|
|
24
29
|
const clientFactory = (0, nice_grpc_1.createClientFactory)().use((0, hatchet_client_1.addTokenMiddleware)(config.token));
|
|
25
30
|
const dispatcher = new dispatcher_client_1.DispatcherClient(Object.assign(Object.assign({}, config), { logger: (ctx, level) => new hatchet_client_1.HatchetLogger(ctx, level) }), (0, hatchet_client_1.channelFactory)(config, credentials), clientFactory);
|
|
26
31
|
this.client = dispatcher.client;
|
|
32
|
+
postMessage({
|
|
33
|
+
type: 'debug',
|
|
34
|
+
message: 'Heartbeat thread started.',
|
|
35
|
+
});
|
|
27
36
|
}
|
|
28
37
|
start() {
|
|
29
38
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -33,6 +42,10 @@ class HeartbeatWorker {
|
|
|
33
42
|
const beat = () => __awaiter(this, void 0, void 0, function* () {
|
|
34
43
|
try {
|
|
35
44
|
this.logger.debug('Heartbeat sending...');
|
|
45
|
+
postMessage({
|
|
46
|
+
type: 'debug',
|
|
47
|
+
message: 'Heartbeat sending...',
|
|
48
|
+
});
|
|
36
49
|
yield this.client.heartbeat({
|
|
37
50
|
workerId: this.workerId,
|
|
38
51
|
heartbeatAt: new Date(),
|
|
@@ -40,19 +53,38 @@ class HeartbeatWorker {
|
|
|
40
53
|
const now = new Date().getTime();
|
|
41
54
|
const actualInterval = now - this.timeLastHeartbeat;
|
|
42
55
|
if (actualInterval > HEARTBEAT_INTERVAL * 1.2) {
|
|
43
|
-
|
|
56
|
+
const message = `Heartbeat interval delay (${actualInterval}ms >> ${HEARTBEAT_INTERVAL}ms)`;
|
|
57
|
+
this.logger.warn(message);
|
|
58
|
+
postMessage({
|
|
59
|
+
type: 'warn',
|
|
60
|
+
message,
|
|
61
|
+
});
|
|
44
62
|
}
|
|
45
63
|
this.logger.debug(`Heartbeat sent ${actualInterval}ms ago`);
|
|
64
|
+
postMessage({
|
|
65
|
+
type: 'debug',
|
|
66
|
+
message: `Heartbeat sent ${actualInterval}ms ago`,
|
|
67
|
+
});
|
|
46
68
|
this.timeLastHeartbeat = now;
|
|
47
69
|
}
|
|
48
70
|
catch (e) {
|
|
49
71
|
if (e.code === nice_grpc_1.Status.UNIMPLEMENTED) {
|
|
50
72
|
// break out of interval
|
|
51
|
-
|
|
73
|
+
const message = 'Heartbeat not implemented, closing heartbeat';
|
|
74
|
+
this.logger.debug(message);
|
|
75
|
+
postMessage({
|
|
76
|
+
type: 'error',
|
|
77
|
+
message,
|
|
78
|
+
});
|
|
52
79
|
this.stop();
|
|
53
80
|
return;
|
|
54
81
|
}
|
|
55
|
-
|
|
82
|
+
const message = `Failed to send heartbeat: ${e.message}`;
|
|
83
|
+
this.logger.debug(message);
|
|
84
|
+
postMessage({
|
|
85
|
+
type: 'error',
|
|
86
|
+
message,
|
|
87
|
+
});
|
|
56
88
|
}
|
|
57
89
|
});
|
|
58
90
|
// start with a heartbeat
|
|
@@ -69,6 +101,6 @@ class HeartbeatWorker {
|
|
|
69
101
|
}
|
|
70
102
|
const heartbeat = new HeartbeatWorker(worker_threads_1.workerData.config, worker_threads_1.workerData.workerId);
|
|
71
103
|
heartbeat.start();
|
|
72
|
-
worker_threads_1.parentPort === null || worker_threads_1.parentPort === void 0 ? void 0 : worker_threads_1.parentPort.on(
|
|
104
|
+
worker_threads_1.parentPort === null || worker_threads_1.parentPort === void 0 ? void 0 : worker_threads_1.parentPort.on(heartbeat_controller_1.STOP_HEARTBEAT, () => {
|
|
73
105
|
heartbeat.stop();
|
|
74
106
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hatchet-dev/typescript-sdk",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "Background task orchestration & visibility for developers",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -70,7 +70,8 @@
|
|
|
70
70
|
"author": "",
|
|
71
71
|
"license": "MIT",
|
|
72
72
|
"devDependencies": {
|
|
73
|
-
"@
|
|
73
|
+
"@tsd/typescript": "^5.8.2",
|
|
74
|
+
"@types/jest": "^29.5.14",
|
|
74
75
|
"@types/node": "^22.0.0",
|
|
75
76
|
"@typescript-eslint/eslint-plugin": "^6.4.0",
|
|
76
77
|
"autoprefixer": "^10.4.16",
|
|
@@ -91,6 +92,7 @@
|
|
|
91
92
|
"eslint-plugin-unused-imports": "^4.1.3",
|
|
92
93
|
"grpc-tools": "^1.12.4",
|
|
93
94
|
"jest": "^29.7.0",
|
|
95
|
+
"jest-tsd": "^0.2.2",
|
|
94
96
|
"pino": "^9.6.0",
|
|
95
97
|
"prettier": "^3.1.1",
|
|
96
98
|
"resolve-tspaths": "^0.8.17",
|
package/step.d.ts
CHANGED
|
@@ -9,9 +9,10 @@ import { V0Worker } from './clients/worker';
|
|
|
9
9
|
import { WorkerLabels } from './clients/dispatcher/dispatcher-client';
|
|
10
10
|
import { CreateStepRateLimit, RateLimitDuration, WorkerLabelComparator } from './protoc/workflows';
|
|
11
11
|
import { CreateWorkflowTaskOpts } from './v1/task';
|
|
12
|
-
import { BaseWorkflowDeclaration as WorkflowV1 } from './v1/declaration';
|
|
12
|
+
import { TaskWorkflowDeclaration, BaseWorkflowDeclaration as WorkflowV1 } from './v1/declaration';
|
|
13
13
|
import { Conditions } from './v1/conditions';
|
|
14
14
|
import { Duration } from './v1/client/duration';
|
|
15
|
+
import { JsonObject, JsonValue, OutputType } from './v1/types';
|
|
15
16
|
export declare const CreateRateLimitSchema: z.ZodObject<{
|
|
16
17
|
key: z.ZodOptional<z.ZodString>;
|
|
17
18
|
staticKey: z.ZodOptional<z.ZodString>;
|
|
@@ -150,14 +151,6 @@ export declare const CreateStepSchema: z.ZodObject<{
|
|
|
150
151
|
maxSeconds?: number | undefined;
|
|
151
152
|
} | undefined;
|
|
152
153
|
}>;
|
|
153
|
-
export type JsonObject = {
|
|
154
|
-
[Key in string]: JsonValue;
|
|
155
|
-
} & {
|
|
156
|
-
[Key in string]?: JsonValue | undefined;
|
|
157
|
-
};
|
|
158
|
-
export type JsonArray = JsonValue[] | readonly JsonValue[];
|
|
159
|
-
export type JsonPrimitive = string | number | boolean | null;
|
|
160
|
-
export type JsonValue = JsonPrimitive | JsonObject | JsonArray;
|
|
161
154
|
export type NextStep = {
|
|
162
155
|
[key: string]: JsonValue;
|
|
163
156
|
};
|
|
@@ -211,12 +204,11 @@ export declare class Context<T, K = {}> {
|
|
|
211
204
|
get cancelled(): boolean;
|
|
212
205
|
/**
|
|
213
206
|
* Retrieves the output of a parent task.
|
|
214
|
-
* @param
|
|
207
|
+
* @param parentTask - The a CreateTaskOpts or string of the parent task name.
|
|
215
208
|
* @returns The output of the specified parent task.
|
|
216
209
|
* @throws An error if the task output is not found.
|
|
217
|
-
*
|
|
218
210
|
*/
|
|
219
|
-
parentOutput<L
|
|
211
|
+
parentOutput<L extends OutputType>(parentTask: CreateWorkflowTaskOpts<any, L> | string): Promise<L>;
|
|
220
212
|
/**
|
|
221
213
|
* Get the output of a task.
|
|
222
214
|
* @param task - The name of the task to get the output for.
|
|
@@ -229,7 +221,7 @@ export declare class Context<T, K = {}> {
|
|
|
229
221
|
* Returns errors from any task runs in the workflow.
|
|
230
222
|
* @returns A record mapping task names to error messages.
|
|
231
223
|
* @throws A warning if no errors are found (this method should be used in on-failure tasks).
|
|
232
|
-
* @deprecated use ctx.errors instead
|
|
224
|
+
* @deprecated use ctx.errors() instead
|
|
233
225
|
*/
|
|
234
226
|
stepRunErrors(): Record<string, string>;
|
|
235
227
|
/**
|
|
@@ -251,6 +243,7 @@ export declare class Context<T, K = {}> {
|
|
|
251
243
|
/**
|
|
252
244
|
* Gets the input data for the current workflow.
|
|
253
245
|
* @returns The input data for the workflow.
|
|
246
|
+
* @deprecated use task input parameter instead
|
|
254
247
|
*/
|
|
255
248
|
workflowInput(): T;
|
|
256
249
|
/**
|
|
@@ -279,6 +272,11 @@ export declare class Context<T, K = {}> {
|
|
|
279
272
|
* @returns The workflow run ID.
|
|
280
273
|
*/
|
|
281
274
|
workflowRunId(): string;
|
|
275
|
+
/**
|
|
276
|
+
* Gets the ID of the current task run.
|
|
277
|
+
* @returns The task run ID.
|
|
278
|
+
*/
|
|
279
|
+
taskRunId(): string;
|
|
282
280
|
/**
|
|
283
281
|
* Gets the number of times the current task has been retried.
|
|
284
282
|
* @returns The retry count.
|
|
@@ -357,10 +355,10 @@ export declare class Context<T, K = {}> {
|
|
|
357
355
|
*
|
|
358
356
|
* @param workflow - The workflow to run (name, Workflow instance, or WorkflowV1 instance).
|
|
359
357
|
* @param input - The input data for the workflow.
|
|
360
|
-
* @param
|
|
358
|
+
* @param optionsOrKey - Either a string key or an options object containing key, sticky, and additionalMetadata.
|
|
361
359
|
* @returns The result of the workflow.
|
|
362
360
|
*/
|
|
363
|
-
runChild<Q extends JsonObject, P extends JsonObject>(workflow: string | Workflow | WorkflowV1<Q, P>, input: Q,
|
|
361
|
+
runChild<Q extends JsonObject, P extends JsonObject>(workflow: string | Workflow | WorkflowV1<Q, P> | TaskWorkflowDeclaration<Q, P>, input: Q, optionsOrKey?: string | {
|
|
364
362
|
key?: string;
|
|
365
363
|
sticky?: boolean;
|
|
366
364
|
additionalMetadata?: Record<string, string>;
|
|
@@ -370,10 +368,10 @@ export declare class Context<T, K = {}> {
|
|
|
370
368
|
*
|
|
371
369
|
* @param workflow - The workflow to enqueue (name, Workflow instance, or WorkflowV1 instance).
|
|
372
370
|
* @param input - The input data for the workflow.
|
|
373
|
-
* @param
|
|
371
|
+
* @param optionsOrKey - Either a string key or an options object containing key, sticky, and additionalMetadata.
|
|
374
372
|
* @returns A reference to the spawned workflow run.
|
|
375
373
|
*/
|
|
376
|
-
runNoWaitChild<Q extends JsonObject, P extends JsonObject>(workflow: string | Workflow | WorkflowV1<Q, P>, input: Q,
|
|
374
|
+
runNoWaitChild<Q extends JsonObject, P extends JsonObject>(workflow: string | Workflow | WorkflowV1<Q, P>, input: Q, optionsOrKey?: string | {
|
|
377
375
|
key?: string;
|
|
378
376
|
sticky?: boolean;
|
|
379
377
|
additionalMetadata?: Record<string, string>;
|
|
@@ -387,7 +385,7 @@ export declare class Context<T, K = {}> {
|
|
|
387
385
|
* @returns A reference to the spawned workflow run.
|
|
388
386
|
* @deprecated Use runChild or runNoWaitChild instead.
|
|
389
387
|
*/
|
|
390
|
-
spawnWorkflow<Q extends JsonObject, P extends JsonObject>(workflow: string | Workflow | WorkflowV1<Q, P>, input: Q, options?: string | {
|
|
388
|
+
spawnWorkflow<Q extends JsonObject, P extends JsonObject>(workflow: string | Workflow | WorkflowV1<Q, P> | TaskWorkflowDeclaration<Q, P>, input: Q, options?: string | {
|
|
391
389
|
key?: string;
|
|
392
390
|
sticky?: boolean;
|
|
393
391
|
additionalMetadata?: Record<string, string>;
|
package/step.js
CHANGED
|
@@ -52,6 +52,7 @@ const hatchet_error_1 = __importDefault(require("./util/errors/hatchet-error"));
|
|
|
52
52
|
const z = __importStar(require("zod"));
|
|
53
53
|
const parse_1 = require("./util/parse");
|
|
54
54
|
const workflows_1 = require("./protoc/workflows");
|
|
55
|
+
const declaration_1 = require("./v1/declaration");
|
|
55
56
|
const conditions_1 = require("./v1/conditions");
|
|
56
57
|
const condition_1 = require("./protoc/v1/shared/condition");
|
|
57
58
|
const transformer_1 = require("./v1/conditions/transformer");
|
|
@@ -162,18 +163,17 @@ class Context {
|
|
|
162
163
|
}
|
|
163
164
|
/**
|
|
164
165
|
* Retrieves the output of a parent task.
|
|
165
|
-
* @param
|
|
166
|
+
* @param parentTask - The a CreateTaskOpts or string of the parent task name.
|
|
166
167
|
* @returns The output of the specified parent task.
|
|
167
168
|
* @throws An error if the task output is not found.
|
|
168
|
-
*
|
|
169
169
|
*/
|
|
170
|
-
parentOutput(
|
|
170
|
+
parentOutput(parentTask) {
|
|
171
171
|
return __awaiter(this, void 0, void 0, function* () {
|
|
172
172
|
// NOTE: parentOutput is async since we plan on potentially making this a cacheable server call
|
|
173
|
-
if (typeof
|
|
174
|
-
return this.stepOutput(
|
|
173
|
+
if (typeof parentTask === 'string') {
|
|
174
|
+
return this.stepOutput(parentTask);
|
|
175
175
|
}
|
|
176
|
-
return this.stepOutput(
|
|
176
|
+
return this.stepOutput(parentTask.name);
|
|
177
177
|
});
|
|
178
178
|
}
|
|
179
179
|
/**
|
|
@@ -185,10 +185,10 @@ class Context {
|
|
|
185
185
|
*/
|
|
186
186
|
stepOutput(step) {
|
|
187
187
|
if (!this.data.parents) {
|
|
188
|
-
throw new hatchet_error_1.default('
|
|
188
|
+
throw new hatchet_error_1.default('Parent task outputs not found');
|
|
189
189
|
}
|
|
190
190
|
if (!this.data.parents[step]) {
|
|
191
|
-
throw new hatchet_error_1.default(`
|
|
191
|
+
throw new hatchet_error_1.default(`Output for parent task '${step}' not found`);
|
|
192
192
|
}
|
|
193
193
|
return this.data.parents[step];
|
|
194
194
|
}
|
|
@@ -196,7 +196,7 @@ class Context {
|
|
|
196
196
|
* Returns errors from any task runs in the workflow.
|
|
197
197
|
* @returns A record mapping task names to error messages.
|
|
198
198
|
* @throws A warning if no errors are found (this method should be used in on-failure tasks).
|
|
199
|
-
* @deprecated use ctx.errors instead
|
|
199
|
+
* @deprecated use ctx.errors() instead
|
|
200
200
|
*/
|
|
201
201
|
stepRunErrors() {
|
|
202
202
|
return this.errors();
|
|
@@ -231,6 +231,7 @@ class Context {
|
|
|
231
231
|
/**
|
|
232
232
|
* Gets the input data for the current workflow.
|
|
233
233
|
* @returns The input data for the workflow.
|
|
234
|
+
* @deprecated use task input parameter instead
|
|
234
235
|
*/
|
|
235
236
|
workflowInput() {
|
|
236
237
|
return this.input;
|
|
@@ -272,6 +273,13 @@ class Context {
|
|
|
272
273
|
workflowRunId() {
|
|
273
274
|
return this.action.workflowRunId;
|
|
274
275
|
}
|
|
276
|
+
/**
|
|
277
|
+
* Gets the ID of the current task run.
|
|
278
|
+
* @returns The task run ID.
|
|
279
|
+
*/
|
|
280
|
+
taskRunId() {
|
|
281
|
+
return this.action.stepRunId;
|
|
282
|
+
}
|
|
275
283
|
/**
|
|
276
284
|
* Gets the number of times the current task has been retried.
|
|
277
285
|
* @returns The retry count.
|
|
@@ -353,7 +361,14 @@ class Context {
|
|
|
353
361
|
bulkRunChildren(children) {
|
|
354
362
|
return __awaiter(this, void 0, void 0, function* () {
|
|
355
363
|
const runs = yield this.bulkRunNoWaitChildren(children);
|
|
356
|
-
const res = runs.map((run) =>
|
|
364
|
+
const res = runs.map((run, index) => {
|
|
365
|
+
const wf = children[index].workflow;
|
|
366
|
+
if (wf instanceof declaration_1.TaskWorkflowDeclaration) {
|
|
367
|
+
// eslint-disable-next-line no-underscore-dangle
|
|
368
|
+
return run.output[wf._standalone_task_name];
|
|
369
|
+
}
|
|
370
|
+
return run.output;
|
|
371
|
+
});
|
|
357
372
|
return Promise.all(res);
|
|
358
373
|
});
|
|
359
374
|
}
|
|
@@ -414,12 +429,19 @@ class Context {
|
|
|
414
429
|
*
|
|
415
430
|
* @param workflow - The workflow to run (name, Workflow instance, or WorkflowV1 instance).
|
|
416
431
|
* @param input - The input data for the workflow.
|
|
417
|
-
* @param
|
|
432
|
+
* @param optionsOrKey - Either a string key or an options object containing key, sticky, and additionalMetadata.
|
|
418
433
|
* @returns The result of the workflow.
|
|
419
434
|
*/
|
|
420
|
-
runChild(workflow, input,
|
|
435
|
+
runChild(workflow, input, optionsOrKey) {
|
|
421
436
|
return __awaiter(this, void 0, void 0, function* () {
|
|
422
|
-
const run = yield this.spawnWorkflow(workflow, input,
|
|
437
|
+
const run = yield this.spawnWorkflow(workflow, input, optionsOrKey);
|
|
438
|
+
if (workflow instanceof declaration_1.TaskWorkflowDeclaration) {
|
|
439
|
+
// eslint-disable-next-line no-underscore-dangle
|
|
440
|
+
if (workflow._standalone_task_name) {
|
|
441
|
+
// eslint-disable-next-line no-underscore-dangle
|
|
442
|
+
return run.output[workflow._standalone_task_name];
|
|
443
|
+
}
|
|
444
|
+
}
|
|
423
445
|
return run.output;
|
|
424
446
|
});
|
|
425
447
|
}
|
|
@@ -428,11 +450,11 @@ class Context {
|
|
|
428
450
|
*
|
|
429
451
|
* @param workflow - The workflow to enqueue (name, Workflow instance, or WorkflowV1 instance).
|
|
430
452
|
* @param input - The input data for the workflow.
|
|
431
|
-
* @param
|
|
453
|
+
* @param optionsOrKey - Either a string key or an options object containing key, sticky, and additionalMetadata.
|
|
432
454
|
* @returns A reference to the spawned workflow run.
|
|
433
455
|
*/
|
|
434
|
-
runNoWaitChild(workflow, input,
|
|
435
|
-
return this.spawnWorkflow(workflow, input,
|
|
456
|
+
runNoWaitChild(workflow, input, optionsOrKey) {
|
|
457
|
+
return this.spawnWorkflow(workflow, input, optionsOrKey);
|
|
436
458
|
}
|
|
437
459
|
/**
|
|
438
460
|
* Spawns a new workflow.
|
package/v1/client/client.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { ClientConfig, InternalHatchetClient, HatchetClientOptions } from '../..
|
|
|
2
2
|
import { AxiosRequestConfig } from 'axios';
|
|
3
3
|
import WorkflowRunRef from '../../util/workflow-run-ref';
|
|
4
4
|
import { Workflow as V0Workflow } from '../../workflow';
|
|
5
|
-
import {
|
|
5
|
+
import { DurableContext } from '../../step';
|
|
6
6
|
import { Api } from '../../clients/rest';
|
|
7
7
|
import { CreateTaskWorkflowOpts, CreateWorkflowOpts, RunOpts, BaseWorkflowDeclaration, WorkflowDeclaration, TaskWorkflowDeclaration } from '../declaration';
|
|
8
8
|
import { IHatchetClient } from './client.interface';
|
|
@@ -12,6 +12,7 @@ import { WorkersClient } from './features/workers';
|
|
|
12
12
|
import { WorkflowsClient } from './features/workflows';
|
|
13
13
|
import { RunsClient } from './features/runs';
|
|
14
14
|
import { CreateStandaloneDurableTaskOpts } from '../task';
|
|
15
|
+
import { InputType, OutputType, UnknownInputType, WorkflowOutputType } from '../types';
|
|
15
16
|
/**
|
|
16
17
|
* HatchetV1 implements the main client interface for interacting with the Hatchet workflow engine.
|
|
17
18
|
* It provides methods for creating and executing workflows, as well as managing workers.
|
|
@@ -45,80 +46,80 @@ export declare class HatchetClient implements IHatchetClient {
|
|
|
45
46
|
static init(config?: Partial<ClientConfig>, options?: HatchetClientOptions, axiosConfig?: AxiosRequestConfig): HatchetClient;
|
|
46
47
|
/**
|
|
47
48
|
* Creates a new workflow definition.
|
|
48
|
-
* @template
|
|
49
|
-
* @template
|
|
49
|
+
* @template I - The input type for the workflow
|
|
50
|
+
* @template O - The return type of the workflow
|
|
50
51
|
* @param options - Configuration options for creating the workflow
|
|
51
52
|
* @returns A new Workflow instance
|
|
52
53
|
* @note It is possible to create an orphaned workflow if no client is available using @hatchet/client CreateWorkflow
|
|
53
54
|
*/
|
|
54
|
-
workflow<
|
|
55
|
+
workflow<I extends InputType = UnknownInputType, O extends WorkflowOutputType = {}>(options: CreateWorkflowOpts): WorkflowDeclaration<I, O>;
|
|
55
56
|
/**
|
|
56
57
|
* Creates a new task workflow.
|
|
57
58
|
* Types can be explicitly specified as generics or inferred from the function signature.
|
|
58
|
-
* @template
|
|
59
|
-
* @template
|
|
59
|
+
* @template I The input type for the task
|
|
60
|
+
* @template O The output type of the task
|
|
60
61
|
* @param options Task configuration options
|
|
61
62
|
* @returns A TaskWorkflowDeclaration instance
|
|
62
63
|
*/
|
|
63
|
-
task<
|
|
64
|
+
task<I extends InputType = UnknownInputType, O extends OutputType = void>(options: CreateTaskWorkflowOpts<I, O>): TaskWorkflowDeclaration<I, O>;
|
|
64
65
|
/**
|
|
65
66
|
* Creates a new task workflow with types inferred from the function parameter.
|
|
66
67
|
* @template Fn The type of the task function with input and output extending JsonObject
|
|
67
68
|
* @param options Task configuration options with function that defines types
|
|
68
69
|
* @returns A TaskWorkflowDeclaration instance with inferred types
|
|
69
70
|
*/
|
|
70
|
-
task<Fn extends (input: I, ctx?: any) => O | Promise<O>, I extends
|
|
71
|
+
task<Fn extends (input: I, ctx?: any) => O | Promise<O>, I extends InputType = Parameters<Fn>[0] | UnknownInputType, O extends OutputType = ReturnType<Fn> extends Promise<infer P> ? P extends OutputType ? P : void : ReturnType<Fn> extends OutputType ? ReturnType<Fn> : void>(options: {
|
|
71
72
|
fn: Fn;
|
|
72
73
|
} & Omit<CreateTaskWorkflowOpts<I, O>, 'fn'>): TaskWorkflowDeclaration<I, O>;
|
|
73
74
|
/**
|
|
74
75
|
* Creates a new durable task workflow.
|
|
75
76
|
* Types can be explicitly specified as generics or inferred from the function signature.
|
|
76
|
-
* @template
|
|
77
|
-
* @template
|
|
77
|
+
* @template I The input type for the durable task
|
|
78
|
+
* @template O The output type of the durable task
|
|
78
79
|
* @param options Durable task configuration options
|
|
79
80
|
* @returns A TaskWorkflowDeclaration instance for a durable task
|
|
80
81
|
*/
|
|
81
|
-
durableTask<
|
|
82
|
+
durableTask<I extends InputType, O extends OutputType>(options: CreateStandaloneDurableTaskOpts<I, O>): TaskWorkflowDeclaration<I, O>;
|
|
82
83
|
/**
|
|
83
84
|
* Creates a new durable task workflow with types inferred from the function parameter.
|
|
84
85
|
* @template Fn The type of the durable task function with input and output extending JsonObject
|
|
85
86
|
* @param options Durable task configuration options with function that defines types
|
|
86
87
|
* @returns A TaskWorkflowDeclaration instance with inferred types
|
|
87
88
|
*/
|
|
88
|
-
durableTask<Fn extends (input: I, ctx: DurableContext<I>) => O | Promise<O>, I extends
|
|
89
|
+
durableTask<Fn extends (input: I, ctx: DurableContext<I>) => O | Promise<O>, I extends InputType = Parameters<Fn>[0], O extends OutputType = ReturnType<Fn> extends Promise<infer P> ? P extends OutputType ? P : void : ReturnType<Fn> extends OutputType ? ReturnType<Fn> : void>(options: {
|
|
89
90
|
fn: Fn;
|
|
90
91
|
} & Omit<CreateStandaloneDurableTaskOpts<I, O>, 'fn'>): TaskWorkflowDeclaration<I, O>;
|
|
91
92
|
/**
|
|
92
93
|
* Triggers a workflow run without waiting for completion.
|
|
93
|
-
* @template
|
|
94
|
-
* @template
|
|
94
|
+
* @template I - The input type for the workflow
|
|
95
|
+
* @template O - The return type of the workflow
|
|
95
96
|
* @param workflow - The workflow to run, either as a Workflow instance or workflow name
|
|
96
97
|
* @param input - The input data for the workflow
|
|
97
98
|
* @param options - Configuration options for the workflow run
|
|
98
99
|
* @returns A WorkflowRunRef containing the run ID and methods to interact with the run
|
|
99
100
|
*/
|
|
100
|
-
runNoWait<
|
|
101
|
+
runNoWait<I extends InputType = UnknownInputType, O extends OutputType = void>(workflow: BaseWorkflowDeclaration<I, O> | string | V0Workflow, input: I, options: RunOpts): WorkflowRunRef<O>;
|
|
101
102
|
/**
|
|
102
103
|
* @alias run
|
|
103
104
|
* Triggers a workflow run and waits for the result.
|
|
104
|
-
* @template
|
|
105
|
-
* @template
|
|
105
|
+
* @template I - The input type for the workflow
|
|
106
|
+
* @template O - The return type of the workflow
|
|
106
107
|
* @param workflow - The workflow to run, either as a Workflow instance or workflow name
|
|
107
108
|
* @param input - The input data for the workflow
|
|
108
109
|
* @param options - Configuration options for the workflow run
|
|
109
110
|
* @returns A promise that resolves with the workflow result
|
|
110
111
|
*/
|
|
111
|
-
runAndWait<
|
|
112
|
+
runAndWait<I extends InputType = UnknownInputType, O extends OutputType = void>(workflow: BaseWorkflowDeclaration<I, O> | string | V0Workflow, input: I, options?: RunOpts): Promise<O>;
|
|
112
113
|
/**
|
|
113
114
|
* Triggers a workflow run and waits for the result.
|
|
114
|
-
* @template
|
|
115
|
-
* @template
|
|
115
|
+
* @template I - The input type for the workflow
|
|
116
|
+
* @template O - The return type of the workflow
|
|
116
117
|
* @param workflow - The workflow to run, either as a Workflow instance or workflow name
|
|
117
118
|
* @param input - The input data for the workflow
|
|
118
119
|
* @param options - Configuration options for the workflow run
|
|
119
120
|
* @returns A promise that resolves with the workflow result
|
|
120
121
|
*/
|
|
121
|
-
run<
|
|
122
|
+
run<I extends InputType = UnknownInputType, O extends OutputType = void>(workflow: BaseWorkflowDeclaration<I, O> | string | V0Workflow, input: I, options?: RunOpts): Promise<O>;
|
|
122
123
|
/**
|
|
123
124
|
* Get the cron client for creating and managing cron workflow runs
|
|
124
125
|
* @returns A cron client instance
|
package/v1/client/client.js
CHANGED
|
@@ -82,8 +82,8 @@ class HatchetClient {
|
|
|
82
82
|
}
|
|
83
83
|
/**
|
|
84
84
|
* Creates a new workflow definition.
|
|
85
|
-
* @template
|
|
86
|
-
* @template
|
|
85
|
+
* @template I - The input type for the workflow
|
|
86
|
+
* @template O - The return type of the workflow
|
|
87
87
|
* @param options - Configuration options for creating the workflow
|
|
88
88
|
* @returns A new Workflow instance
|
|
89
89
|
* @note It is possible to create an orphaned workflow if no client is available using @hatchet/client CreateWorkflow
|
|
@@ -105,8 +105,8 @@ class HatchetClient {
|
|
|
105
105
|
}
|
|
106
106
|
/**
|
|
107
107
|
* Triggers a workflow run without waiting for completion.
|
|
108
|
-
* @template
|
|
109
|
-
* @template
|
|
108
|
+
* @template I - The input type for the workflow
|
|
109
|
+
* @template O - The return type of the workflow
|
|
110
110
|
* @param workflow - The workflow to run, either as a Workflow instance or workflow name
|
|
111
111
|
* @param input - The input data for the workflow
|
|
112
112
|
* @param options - Configuration options for the workflow run
|
|
@@ -128,8 +128,8 @@ class HatchetClient {
|
|
|
128
128
|
/**
|
|
129
129
|
* @alias run
|
|
130
130
|
* Triggers a workflow run and waits for the result.
|
|
131
|
-
* @template
|
|
132
|
-
* @template
|
|
131
|
+
* @template I - The input type for the workflow
|
|
132
|
+
* @template O - The return type of the workflow
|
|
133
133
|
* @param workflow - The workflow to run, either as a Workflow instance or workflow name
|
|
134
134
|
* @param input - The input data for the workflow
|
|
135
135
|
* @param options - Configuration options for the workflow run
|
|
@@ -142,8 +142,8 @@ class HatchetClient {
|
|
|
142
142
|
}
|
|
143
143
|
/**
|
|
144
144
|
* Triggers a workflow run and waits for the result.
|
|
145
|
-
* @template
|
|
146
|
-
* @template
|
|
145
|
+
* @template I - The input type for the workflow
|
|
146
|
+
* @template O - The return type of the workflow
|
|
147
147
|
* @param workflow - The workflow to run, either as a Workflow instance or workflow name
|
|
148
148
|
* @param input - The input data for the workflow
|
|
149
149
|
* @param options - Configuration options for the workflow run
|
package/v1/declaration.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import WorkflowRunRef from '../util/workflow-run-ref';
|
|
2
|
-
import { Context, DurableContext
|
|
2
|
+
import { Context, DurableContext } from '../step';
|
|
3
3
|
import { CronWorkflows, ScheduledWorkflows } from '../clients/rest/generated/data-contracts';
|
|
4
4
|
import { Workflow as WorkflowV0 } from '../workflow';
|
|
5
5
|
import { IHatchetClient } from './client/client.interface';
|
|
6
6
|
import { CreateWorkflowTaskOpts, CreateOnFailureTaskOpts, TaskConcurrency, TaskFn, CreateWorkflowDurableTaskOpts, CreateBaseTaskOpts, CreateOnSuccessTaskOpts } from './task';
|
|
7
7
|
import { Duration } from './client/duration';
|
|
8
8
|
import { MetricsClient } from './client/features/metrics';
|
|
9
|
+
import { InputType, OutputType, UnknownInputType, JsonObject } from './types';
|
|
9
10
|
/**
|
|
10
11
|
* Additional metadata that can be attached to a workflow run.
|
|
11
12
|
*/
|
|
@@ -19,10 +20,14 @@ export type RunOpts = {
|
|
|
19
20
|
*/
|
|
20
21
|
additionalMetadata?: AdditionalMetadata;
|
|
21
22
|
};
|
|
23
|
+
/**
|
|
24
|
+
* Helper type to safely extract output types from task results
|
|
25
|
+
*/
|
|
26
|
+
export type TaskOutput<O, Key extends string, Fallback> = O extends Record<Key, infer Value> ? (Value extends OutputType ? Value : Fallback) : Fallback;
|
|
22
27
|
/**
|
|
23
28
|
* Extracts a property from an object type based on task name, or falls back to inferred type
|
|
24
29
|
*/
|
|
25
|
-
export type TaskOutputType<
|
|
30
|
+
export type TaskOutputType<O, TaskName extends string, InferredType extends OutputType> = TaskName extends keyof O ? O[TaskName] extends OutputType ? O[TaskName] : InferredType : InferredType;
|
|
26
31
|
export type CreateBaseWorkflowOpts = {
|
|
27
32
|
/**
|
|
28
33
|
* The name of the workflow.
|
|
@@ -55,7 +60,7 @@ export type CreateBaseWorkflowOpts = {
|
|
|
55
60
|
onEvents?: string[];
|
|
56
61
|
concurrency?: TaskConcurrency;
|
|
57
62
|
};
|
|
58
|
-
export type CreateTaskWorkflowOpts<
|
|
63
|
+
export type CreateTaskWorkflowOpts<I extends InputType = UnknownInputType, O extends OutputType = void> = CreateBaseWorkflowOpts & CreateBaseTaskOpts<I, O, TaskFn<I, O>>;
|
|
59
64
|
/**
|
|
60
65
|
* Options for creating a new workflow.
|
|
61
66
|
*/
|
|
@@ -141,10 +146,10 @@ export type WorkflowDefinition = CreateWorkflowOpts & {
|
|
|
141
146
|
};
|
|
142
147
|
/**
|
|
143
148
|
* Represents a workflow that can be executed by Hatchet.
|
|
144
|
-
* @template
|
|
145
|
-
* @template
|
|
149
|
+
* @template I The input type for the workflow.
|
|
150
|
+
* @template O The return type of the workflow.
|
|
146
151
|
*/
|
|
147
|
-
export declare class BaseWorkflowDeclaration<
|
|
152
|
+
export declare class BaseWorkflowDeclaration<I extends InputType = UnknownInputType, O extends OutputType = void> {
|
|
148
153
|
/**
|
|
149
154
|
* The Hatchet client instance used to execute the workflow.
|
|
150
155
|
*/
|
|
@@ -166,18 +171,18 @@ export declare class BaseWorkflowDeclaration<T extends JsonObject | unknown, K e
|
|
|
166
171
|
* @returns A WorkflowRunRef containing the run ID and methods to get results and interact with the run.
|
|
167
172
|
* @throws Error if the workflow is not bound to a Hatchet client.
|
|
168
173
|
*/
|
|
169
|
-
runNoWait(input:
|
|
174
|
+
runNoWait(input: I, options?: RunOpts): WorkflowRunRef<O>;
|
|
170
175
|
/**
|
|
171
176
|
* @alias run
|
|
172
177
|
* Triggers a workflow run and waits for the result.
|
|
173
|
-
* @template
|
|
174
|
-
* @template
|
|
178
|
+
* @template I - The input type for the workflow
|
|
179
|
+
* @template O - The return type of the workflow
|
|
175
180
|
* @param input - The input data for the workflow
|
|
176
181
|
* @param options - Configuration options for the workflow run
|
|
177
182
|
* @returns A promise that resolves with the workflow result
|
|
178
183
|
*/
|
|
179
|
-
runAndWait(input:
|
|
180
|
-
runAndWait(input:
|
|
184
|
+
runAndWait(input: I, options?: RunOpts): Promise<O>;
|
|
185
|
+
runAndWait(input: I[], options?: RunOpts): Promise<O[]>;
|
|
181
186
|
/**
|
|
182
187
|
* Executes the workflow with the given input and awaits the results.
|
|
183
188
|
* @param input The input data for the workflow.
|
|
@@ -185,8 +190,8 @@ export declare class BaseWorkflowDeclaration<T extends JsonObject | unknown, K e
|
|
|
185
190
|
* @returns A promise that resolves with the workflow result.
|
|
186
191
|
* @throws Error if the workflow is not bound to a Hatchet client.
|
|
187
192
|
*/
|
|
188
|
-
run(input:
|
|
189
|
-
run(input:
|
|
193
|
+
run(input: I, options?: RunOpts): Promise<O>;
|
|
194
|
+
run(input: I[], options?: RunOpts): Promise<O[]>;
|
|
190
195
|
/**
|
|
191
196
|
* Schedules a workflow to run at a specific date and time in the future.
|
|
192
197
|
* @param enqueueAt The date when the workflow should be triggered.
|
|
@@ -195,7 +200,7 @@ export declare class BaseWorkflowDeclaration<T extends JsonObject | unknown, K e
|
|
|
195
200
|
* @returns A promise that resolves with the scheduled workflow details.
|
|
196
201
|
* @throws Error if the workflow is not bound to a Hatchet client.
|
|
197
202
|
*/
|
|
198
|
-
schedule(enqueueAt: Date, input:
|
|
203
|
+
schedule(enqueueAt: Date, input: I, options?: RunOpts): Promise<ScheduledWorkflows>;
|
|
199
204
|
/**
|
|
200
205
|
* Schedules a workflow to run after a specified delay.
|
|
201
206
|
* @param duration The delay in seconds before the workflow should run.
|
|
@@ -204,7 +209,7 @@ export declare class BaseWorkflowDeclaration<T extends JsonObject | unknown, K e
|
|
|
204
209
|
* @returns A promise that resolves with the scheduled workflow details.
|
|
205
210
|
* @throws Error if the workflow is not bound to a Hatchet client.
|
|
206
211
|
*/
|
|
207
|
-
delay(duration: number, input:
|
|
212
|
+
delay(duration: number, input: I, options?: RunOpts): Promise<ScheduledWorkflows>;
|
|
208
213
|
/**
|
|
209
214
|
* Creates a cron schedule for the workflow.
|
|
210
215
|
* @param name The name of the cron schedule.
|
|
@@ -214,7 +219,7 @@ export declare class BaseWorkflowDeclaration<T extends JsonObject | unknown, K e
|
|
|
214
219
|
* @returns A promise that resolves with the cron workflow details.
|
|
215
220
|
* @throws Error if the workflow is not bound to a Hatchet client.
|
|
216
221
|
*/
|
|
217
|
-
cron(name: string, expression: string, input:
|
|
222
|
+
cron(name: string, expression: string, input: I, options?: RunOpts): Promise<CronWorkflows>;
|
|
218
223
|
/**
|
|
219
224
|
* Get metrics for the workflow.
|
|
220
225
|
* @param opts Optional configuration for the metrics request.
|
|
@@ -242,20 +247,20 @@ export declare class BaseWorkflowDeclaration<T extends JsonObject | unknown, K e
|
|
|
242
247
|
*/
|
|
243
248
|
get name(): string;
|
|
244
249
|
}
|
|
245
|
-
export declare class WorkflowDeclaration<
|
|
250
|
+
export declare class WorkflowDeclaration<I extends InputType = UnknownInputType, O extends OutputType = void> extends BaseWorkflowDeclaration<I, O> {
|
|
246
251
|
/**
|
|
247
252
|
* Adds a task to the workflow.
|
|
248
|
-
* The return type will be either the property on
|
|
253
|
+
* The return type will be either the property on O that corresponds to the task name,
|
|
249
254
|
* or if there is no matching property, the inferred return type of the function.
|
|
250
255
|
* @template Name The literal string name of the task.
|
|
251
|
-
* @template
|
|
256
|
+
* @template Fn The type of the task function.
|
|
252
257
|
* @param options The task configuration options.
|
|
253
258
|
* @returns The task options that were added.
|
|
254
259
|
*/
|
|
255
|
-
task<Name extends string,
|
|
260
|
+
task<Name extends string, Fn extends Name extends keyof O ? (input: I, ctx: Context<I>) => O[Name] extends OutputType ? O[Name] | Promise<O[Name]> : void : (input: I, ctx: Context<I>) => void, FnReturn = ReturnType<Fn> extends Promise<infer P> ? P : ReturnType<Fn>, TO extends OutputType = Name extends keyof O ? O[Name] extends OutputType ? O[Name] : never : FnReturn extends OutputType ? FnReturn : never>(options: (Omit<CreateWorkflowTaskOpts<I, TO>, 'fn'> & {
|
|
256
261
|
name: Name;
|
|
257
|
-
fn:
|
|
258
|
-
}) | TaskWorkflowDeclaration<
|
|
262
|
+
fn: Fn;
|
|
263
|
+
}) | TaskWorkflowDeclaration<I, TO>): CreateWorkflowTaskOpts<I, TO>;
|
|
259
264
|
/**
|
|
260
265
|
* Adds an onFailure task to the workflow.
|
|
261
266
|
* This will only run if any task in the workflow fails.
|
|
@@ -264,9 +269,9 @@ export declare class WorkflowDeclaration<T extends JsonObject | unknown = unknow
|
|
|
264
269
|
* @param options The task configuration options.
|
|
265
270
|
* @returns The task options that were added.
|
|
266
271
|
*/
|
|
267
|
-
onFailure<Name extends string, L>(options: (Omit<CreateOnFailureTaskOpts<
|
|
268
|
-
fn: (input:
|
|
269
|
-
}) | TaskWorkflowDeclaration<any, any>): CreateWorkflowTaskOpts<
|
|
272
|
+
onFailure<Name extends string, L extends OutputType>(options: (Omit<CreateOnFailureTaskOpts<I, TaskOutputType<O, Name, L>>, 'fn'> & {
|
|
273
|
+
fn: (input: I, ctx: Context<I>) => TaskOutputType<O, Name, L> | Promise<TaskOutputType<O, Name, L>>;
|
|
274
|
+
}) | TaskWorkflowDeclaration<any, any>): CreateWorkflowTaskOpts<I, TaskOutputType<O, Name, L>>;
|
|
270
275
|
/**
|
|
271
276
|
* Adds an onSuccess task to the workflow.
|
|
272
277
|
* This will only run if all tasks in the workflow complete successfully.
|
|
@@ -275,28 +280,28 @@ export declare class WorkflowDeclaration<T extends JsonObject | unknown = unknow
|
|
|
275
280
|
* @param options The task configuration options.
|
|
276
281
|
* @returns The task options that were added.
|
|
277
282
|
*/
|
|
278
|
-
onSuccess<Name extends string, L>(options: (Omit<CreateOnSuccessTaskOpts<
|
|
279
|
-
fn: (input:
|
|
280
|
-
}) | TaskWorkflowDeclaration<any, any>): CreateWorkflowTaskOpts<
|
|
283
|
+
onSuccess<Name extends string, L extends OutputType>(options: (Omit<CreateOnSuccessTaskOpts<I, TaskOutputType<O, Name, L>>, 'fn'> & {
|
|
284
|
+
fn: (input: I, ctx: Context<I>) => TaskOutputType<O, Name, L> | Promise<TaskOutputType<O, Name, L>>;
|
|
285
|
+
}) | TaskWorkflowDeclaration<any, any>): CreateWorkflowTaskOpts<I, TaskOutputType<O, Name, L>>;
|
|
281
286
|
/**
|
|
282
287
|
* Adds a durable task to the workflow.
|
|
283
|
-
* The return type will be either the property on
|
|
288
|
+
* The return type will be either the property on O that corresponds to the task name,
|
|
284
289
|
* or if there is no matching property, the inferred return type of the function.
|
|
285
290
|
* @template Name The literal string name of the task.
|
|
286
|
-
* @template
|
|
291
|
+
* @template Fn The type of the task function.
|
|
287
292
|
* @param options The task configuration options.
|
|
288
293
|
* @returns The task options that were added.
|
|
289
294
|
*/
|
|
290
|
-
durableTask<Name extends string,
|
|
295
|
+
durableTask<Name extends string, Fn extends Name extends keyof O ? (input: I, ctx: DurableContext<I>) => O[Name] extends OutputType ? O[Name] | Promise<O[Name]> : void : (input: I, ctx: DurableContext<I>) => void, FnReturn = ReturnType<Fn> extends Promise<infer P> ? P : ReturnType<Fn>, TO extends OutputType = Name extends keyof O ? O[Name] extends OutputType ? O[Name] : never : FnReturn extends OutputType ? FnReturn : never>(options: Omit<CreateWorkflowTaskOpts<I, TO>, 'fn'> & {
|
|
291
296
|
name: Name;
|
|
292
|
-
fn:
|
|
293
|
-
}): CreateWorkflowDurableTaskOpts<
|
|
297
|
+
fn: Fn;
|
|
298
|
+
}): CreateWorkflowDurableTaskOpts<I, TO>;
|
|
294
299
|
}
|
|
295
|
-
export declare class TaskWorkflowDeclaration<
|
|
296
|
-
|
|
297
|
-
constructor(options: CreateTaskWorkflowOpts<
|
|
298
|
-
run(input:
|
|
299
|
-
run(input:
|
|
300
|
+
export declare class TaskWorkflowDeclaration<I extends InputType = UnknownInputType, O extends OutputType = void> extends BaseWorkflowDeclaration<I, O> {
|
|
301
|
+
_standalone_task_name: string;
|
|
302
|
+
constructor(options: CreateTaskWorkflowOpts<I, O>, client?: IHatchetClient);
|
|
303
|
+
run(input: I, options?: RunOpts): Promise<O>;
|
|
304
|
+
run(input: I[], options?: RunOpts): Promise<O[]>;
|
|
300
305
|
get taskDef(): CreateWorkflowTaskOpts<any, any>;
|
|
301
306
|
}
|
|
302
307
|
/**
|
|
@@ -306,18 +311,18 @@ export declare class TaskWorkflowDeclaration<T extends JsonObject | unknown = un
|
|
|
306
311
|
* @param client Optional Hatchet client instance.
|
|
307
312
|
* @returns A new TaskWorkflowDeclaration with inferred types.
|
|
308
313
|
*/
|
|
309
|
-
export declare function CreateTaskWorkflow<Fn extends (input: I, ctx?: any) => O | Promise<O>, I extends
|
|
314
|
+
export declare function CreateTaskWorkflow<Fn extends (input: I, ctx?: any) => O | Promise<O>, I extends InputType = Parameters<Fn>[0], O extends OutputType = ReturnType<Fn> extends Promise<infer P> ? P extends OutputType ? P : void : ReturnType<Fn> extends OutputType ? ReturnType<Fn> : void>(options: {
|
|
310
315
|
fn: Fn;
|
|
311
316
|
} & Omit<CreateTaskWorkflowOpts<I, O>, 'fn'>, client?: IHatchetClient): TaskWorkflowDeclaration<I, O>;
|
|
312
317
|
/**
|
|
313
318
|
* Creates a new workflow instance.
|
|
314
|
-
* @template
|
|
315
|
-
* @template
|
|
319
|
+
* @template I The input type for the workflow.
|
|
320
|
+
* @template O The return type of the workflow.
|
|
316
321
|
* @param options The options for creating the workflow.
|
|
317
322
|
* @param client Optional Hatchet client instance.
|
|
318
323
|
* @returns A new Workflow instance.
|
|
319
324
|
*/
|
|
320
|
-
export declare function CreateWorkflow<
|
|
325
|
+
export declare function CreateWorkflow<I extends InputType = UnknownInputType, O extends OutputType = void>(options: CreateWorkflowOpts, client?: IHatchetClient): WorkflowDeclaration<I, O>;
|
|
321
326
|
/**
|
|
322
327
|
* Creates a new durable task workflow declaration with types inferred from the function parameter.
|
|
323
328
|
* @template Fn The type of the durable task function
|
package/v1/declaration.js
CHANGED
|
@@ -16,8 +16,8 @@ exports.CreateDurableTaskWorkflow = CreateDurableTaskWorkflow;
|
|
|
16
16
|
const UNBOUND_ERR = new Error('workflow unbound to hatchet client, hint: use client.run instead');
|
|
17
17
|
/**
|
|
18
18
|
* Represents a workflow that can be executed by Hatchet.
|
|
19
|
-
* @template
|
|
20
|
-
* @template
|
|
19
|
+
* @template I The input type for the workflow.
|
|
20
|
+
* @template O The return type of the workflow.
|
|
21
21
|
*/
|
|
22
22
|
class BaseWorkflowDeclaration {
|
|
23
23
|
/**
|
|
@@ -197,10 +197,10 @@ exports.BaseWorkflowDeclaration = BaseWorkflowDeclaration;
|
|
|
197
197
|
class WorkflowDeclaration extends BaseWorkflowDeclaration {
|
|
198
198
|
/**
|
|
199
199
|
* Adds a task to the workflow.
|
|
200
|
-
* The return type will be either the property on
|
|
200
|
+
* The return type will be either the property on O that corresponds to the task name,
|
|
201
201
|
* or if there is no matching property, the inferred return type of the function.
|
|
202
202
|
* @template Name The literal string name of the task.
|
|
203
|
-
* @template
|
|
203
|
+
* @template Fn The type of the task function.
|
|
204
204
|
* @param options The task configuration options.
|
|
205
205
|
* @returns The task options that were added.
|
|
206
206
|
*/
|
|
@@ -263,10 +263,10 @@ class WorkflowDeclaration extends BaseWorkflowDeclaration {
|
|
|
263
263
|
}
|
|
264
264
|
/**
|
|
265
265
|
* Adds a durable task to the workflow.
|
|
266
|
-
* The return type will be either the property on
|
|
266
|
+
* The return type will be either the property on O that corresponds to the task name,
|
|
267
267
|
* or if there is no matching property, the inferred return type of the function.
|
|
268
268
|
* @template Name The literal string name of the task.
|
|
269
|
-
* @template
|
|
269
|
+
* @template Fn The type of the task function.
|
|
270
270
|
* @param options The task configuration options.
|
|
271
271
|
* @returns The task options that were added.
|
|
272
272
|
*/
|
|
@@ -312,8 +312,8 @@ function CreateTaskWorkflow(options, client) {
|
|
|
312
312
|
}
|
|
313
313
|
/**
|
|
314
314
|
* Creates a new workflow instance.
|
|
315
|
-
* @template
|
|
316
|
-
* @template
|
|
315
|
+
* @template I The input type for the workflow.
|
|
316
|
+
* @template O The return type of the workflow.
|
|
317
317
|
* @param options The options for creating the workflow.
|
|
318
318
|
* @param client Optional Hatchet client instance.
|
|
319
319
|
* @returns A new Workflow instance.
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export declare const cancellation: import("../..").TaskWorkflowDeclaration<
|
|
2
|
-
Completed:
|
|
1
|
+
export declare const cancellation: import("../..").TaskWorkflowDeclaration<import("../..").UnknownInputType, {
|
|
2
|
+
Completed: true;
|
|
3
3
|
}>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const durableSleep: import("../..").WorkflowDeclaration<
|
|
1
|
+
export declare const durableSleep: import("../..").WorkflowDeclaration<import("../..").UnknownInputType, {}>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const failureWorkflow: import("../..").WorkflowDeclaration<
|
|
1
|
+
export declare const failureWorkflow: import("../..").WorkflowDeclaration<import("../..").UnknownInputType, {}>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const onSuccessDag: import("../..").WorkflowDeclaration<
|
|
1
|
+
export declare const onSuccessDag: import("../..").WorkflowDeclaration<import("../..").UnknownInputType, {}>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export declare const retries: import("../..").TaskWorkflowDeclaration<
|
|
2
|
-
export declare const retriesWithCount: import("../..").TaskWorkflowDeclaration<
|
|
1
|
+
export declare const retries: import("../..").TaskWorkflowDeclaration<import("../..").UnknownInputType, never>;
|
|
2
|
+
export declare const retriesWithCount: import("../..").TaskWorkflowDeclaration<import("../..").UnknownInputType, {
|
|
3
3
|
message: string;
|
|
4
4
|
}>;
|
|
5
|
-
export declare const withBackoff: import("../..").TaskWorkflowDeclaration<
|
|
5
|
+
export declare const withBackoff: import("../..").TaskWorkflowDeclaration<import("../..").UnknownInputType, never>;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export declare const cancellation: import("../..").TaskWorkflowDeclaration<
|
|
2
|
-
Completed:
|
|
1
|
+
export declare const cancellation: import("../..").TaskWorkflowDeclaration<import("../..").UnknownInputType, {
|
|
2
|
+
Completed: true;
|
|
3
3
|
}>;
|
package/v1/index.d.ts
CHANGED
package/v1/index.js
CHANGED
package/v1/task.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { ConcurrencyLimitStrategy } from '../protoc/v1/workflows';
|
|
|
2
2
|
import { Context, CreateStep, DurableContext } from '../step';
|
|
3
3
|
import { Conditions } from './conditions';
|
|
4
4
|
import { Duration } from './client/duration';
|
|
5
|
+
import { InputType, OutputType, UnknownInputType } from './types';
|
|
5
6
|
/**
|
|
6
7
|
* Options for configuring the concurrency for a task.
|
|
7
8
|
*/
|
|
@@ -28,14 +29,14 @@ export type TaskConcurrency = {
|
|
|
28
29
|
*/
|
|
29
30
|
limitStrategy?: ConcurrencyLimitStrategy;
|
|
30
31
|
};
|
|
31
|
-
export type TaskFn<
|
|
32
|
-
export type DurableTaskFn<
|
|
32
|
+
export type TaskFn<I extends InputType = UnknownInputType, O extends OutputType = void, C = Context<I>> = (input: I, ctx: C) => O | Promise<O>;
|
|
33
|
+
export type DurableTaskFn<I extends InputType = UnknownInputType, O extends OutputType = void> = TaskFn<I, O, DurableContext<I>>;
|
|
33
34
|
/**
|
|
34
35
|
* Options for creating a hatchet task which is an atomic unit of work in a workflow.
|
|
35
|
-
* @template
|
|
36
|
-
* @template
|
|
36
|
+
* @template I The input type for the task function.
|
|
37
|
+
* @template O The return type of the task function (can be inferred from the return value of fn).
|
|
37
38
|
*/
|
|
38
|
-
export type CreateBaseTaskOpts<
|
|
39
|
+
export type CreateBaseTaskOpts<I extends InputType = UnknownInputType, O extends OutputType = void, C = TaskFn<I, O>> = {
|
|
39
40
|
/**
|
|
40
41
|
* The name of the task.
|
|
41
42
|
*/
|
|
@@ -50,7 +51,7 @@ export type CreateBaseTaskOpts<T, K, C> = {
|
|
|
50
51
|
/**
|
|
51
52
|
* @deprecated use executionTimeout instead
|
|
52
53
|
*/
|
|
53
|
-
timeout?: CreateStep<
|
|
54
|
+
timeout?: CreateStep<I, O>['timeout'];
|
|
54
55
|
/**
|
|
55
56
|
* (optional) execution timeout duration for the task after it starts running
|
|
56
57
|
* go duration format (e.g., "1s", "5m", "1h").
|
|
@@ -70,17 +71,17 @@ export type CreateBaseTaskOpts<T, K, C> = {
|
|
|
70
71
|
*
|
|
71
72
|
* default: 0
|
|
72
73
|
*/
|
|
73
|
-
retries?: CreateStep<
|
|
74
|
+
retries?: CreateStep<I, O>['retries'];
|
|
74
75
|
/**
|
|
75
76
|
* (optional) backoff strategy configuration for retries.
|
|
76
77
|
* - factor: Base of the exponential backoff (base ^ retry count)
|
|
77
78
|
* - maxSeconds: Maximum backoff duration in seconds
|
|
78
79
|
*/
|
|
79
|
-
backoff?: CreateStep<
|
|
80
|
+
backoff?: CreateStep<I, O>['backoff'];
|
|
80
81
|
/**
|
|
81
82
|
* (optional) rate limits for the task.
|
|
82
83
|
*/
|
|
83
|
-
rateLimits?: CreateStep<
|
|
84
|
+
rateLimits?: CreateStep<I, O>['rate_limits'];
|
|
84
85
|
/**
|
|
85
86
|
* (optional) worker labels for task routing and scheduling.
|
|
86
87
|
* Each label can be a simple string/number value or an object with additional configuration:
|
|
@@ -89,18 +90,18 @@ export type CreateBaseTaskOpts<T, K, C> = {
|
|
|
89
90
|
* - weight: Priority weight for worker selection
|
|
90
91
|
* - comparator: Custom comparison logic for label matching
|
|
91
92
|
*/
|
|
92
|
-
desiredWorkerLabels?: CreateStep<
|
|
93
|
+
desiredWorkerLabels?: CreateStep<I, O>['worker_labels'];
|
|
93
94
|
/**
|
|
94
95
|
* (optional) the concurrency options for the task
|
|
95
96
|
*/
|
|
96
97
|
concurrency?: TaskConcurrency | TaskConcurrency[];
|
|
97
98
|
};
|
|
98
|
-
export type CreateWorkflowTaskOpts<
|
|
99
|
+
export type CreateWorkflowTaskOpts<I extends InputType = UnknownInputType, O extends OutputType = void, C extends TaskFn<I, O> | DurableTaskFn<I, O> = TaskFn<I, O>> = CreateBaseTaskOpts<I, O, C> & {
|
|
99
100
|
/**
|
|
100
101
|
* Parent tasks that must complete before this task runs.
|
|
101
102
|
* Used to define the directed acyclic graph (DAG) of the workflow.
|
|
102
103
|
*/
|
|
103
|
-
parents?: CreateWorkflowTaskOpts<
|
|
104
|
+
parents?: CreateWorkflowTaskOpts<I, any, any>[];
|
|
104
105
|
/**
|
|
105
106
|
* (optional) the conditions to match before the task is queued
|
|
106
107
|
* all provided conditions must be met (AND logic)
|
|
@@ -154,28 +155,28 @@ export type CreateWorkflowTaskOpts<T, K, C = TaskFn<T, K>> = CreateBaseTaskOpts<
|
|
|
154
155
|
*/
|
|
155
156
|
skipIf?: Conditions | Conditions[];
|
|
156
157
|
};
|
|
157
|
-
export type CreateStandaloneTaskOpts<
|
|
158
|
+
export type CreateStandaloneTaskOpts<I extends InputType = UnknownInputType, O extends OutputType = void, C extends TaskFn<I, O> = TaskFn<I, O>> = CreateBaseTaskOpts<I, O, C>;
|
|
158
159
|
/**
|
|
159
160
|
* Options for creating a hatchet durable task which is an atomic unit of work in a workflow.
|
|
160
|
-
* @template
|
|
161
|
-
* @template
|
|
161
|
+
* @template I The input type for the task function.
|
|
162
|
+
* @template O The return type of the task function (can be inferred from the return value of fn).
|
|
162
163
|
*/
|
|
163
|
-
export type CreateWorkflowDurableTaskOpts<
|
|
164
|
+
export type CreateWorkflowDurableTaskOpts<I extends InputType = UnknownInputType, O extends OutputType = void, C extends DurableTaskFn<I, O> = DurableTaskFn<I, O>> = CreateWorkflowTaskOpts<I, O, C>;
|
|
164
165
|
/**
|
|
165
166
|
* Options for creating a hatchet task which is an atomic unit of work in a workflow.
|
|
166
|
-
* @template
|
|
167
|
-
* @template
|
|
167
|
+
* @template I The input type for the task function.
|
|
168
|
+
* @template O The return type of the task function (can be inferred from the return value of fn).
|
|
168
169
|
*/
|
|
169
|
-
export type CreateStandaloneDurableTaskOpts<
|
|
170
|
+
export type CreateStandaloneDurableTaskOpts<I extends InputType = UnknownInputType, O extends OutputType = void, C extends DurableTaskFn<I, O> = DurableTaskFn<I, O>> = CreateBaseTaskOpts<I, O, C>;
|
|
170
171
|
/**
|
|
171
172
|
* Options for configuring the onSuccess task that is invoked when a task succeeds.
|
|
172
|
-
* @template
|
|
173
|
-
* @template
|
|
173
|
+
* @template I The input type for the task function.
|
|
174
|
+
* @template O The return type of the task function (can be inferred from the return value of fn).
|
|
174
175
|
*/
|
|
175
|
-
export type CreateOnSuccessTaskOpts<
|
|
176
|
+
export type CreateOnSuccessTaskOpts<I extends InputType = UnknownInputType, O extends OutputType = void, C extends TaskFn<I, O> = TaskFn<I, O>> = Omit<CreateBaseTaskOpts<I, O, C>, 'name'>;
|
|
176
177
|
/**
|
|
177
178
|
* Options for configuring the onFailure task that is invoked when a task fails.
|
|
178
|
-
* @template
|
|
179
|
-
* @template
|
|
179
|
+
* @template I The input type for the task function.
|
|
180
|
+
* @template O The return type of the task function (can be inferred from the return value of fn).
|
|
180
181
|
*/
|
|
181
|
-
export type CreateOnFailureTaskOpts<
|
|
182
|
+
export type CreateOnFailureTaskOpts<I extends InputType = UnknownInputType, O extends OutputType = void, C extends TaskFn<I, O> = TaskFn<I, O>> = Omit<CreateBaseTaskOpts<I, O, C>, 'name'>;
|
package/v1/types.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export type JsonPrimitive = string | number | boolean | null | undefined;
|
|
2
|
+
export type JsonArray = JsonValue[];
|
|
3
|
+
export type JsonValue = JsonPrimitive | JsonObject | JsonArray;
|
|
4
|
+
export type JsonObject = {
|
|
5
|
+
[Key in string]: JsonValue;
|
|
6
|
+
} & {
|
|
7
|
+
[Key in string]?: JsonValue | undefined;
|
|
8
|
+
};
|
|
9
|
+
export type InputType = JsonObject;
|
|
10
|
+
export type UnknownInputType = {};
|
|
11
|
+
export type OutputType = JsonObject | void;
|
|
12
|
+
type IsValidWorkflowOutput<T> = T extends Record<string, JsonObject> ? true : false;
|
|
13
|
+
export type WorkflowOutputType<T = any> = IsValidWorkflowOutput<T> extends true ? T : (Record<string, JsonObject> | void) & {
|
|
14
|
+
[ERROR_WORKFLOW_OUTPUT]?: 'Workflow output must be shaped as Record<"task-name", JsonObject>. Each property must be an object, not a primitive value.';
|
|
15
|
+
};
|
|
16
|
+
declare const ERROR_WORKFLOW_OUTPUT: unique symbol;
|
|
17
|
+
export {};
|
package/v1/types.js
ADDED
package/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const HATCHET_VERSION = "1.0.
|
|
1
|
+
export declare const HATCHET_VERSION = "1.0.5";
|
package/version.js
CHANGED
package/workflow.d.ts
CHANGED
|
@@ -380,10 +380,6 @@ export declare const CreateWorkflowSchema: z.ZodObject<{
|
|
|
380
380
|
} | undefined;
|
|
381
381
|
}[];
|
|
382
382
|
id: string;
|
|
383
|
-
version?: string | undefined;
|
|
384
|
-
scheduleTimeout?: string | undefined;
|
|
385
|
-
sticky?: PbStickyStrategy | undefined;
|
|
386
|
-
timeout?: string | undefined;
|
|
387
383
|
on?: {
|
|
388
384
|
cron: string;
|
|
389
385
|
event?: undefined;
|
|
@@ -391,6 +387,10 @@ export declare const CreateWorkflowSchema: z.ZodObject<{
|
|
|
391
387
|
event: string;
|
|
392
388
|
cron?: undefined;
|
|
393
389
|
} | undefined;
|
|
390
|
+
version?: string | undefined;
|
|
391
|
+
scheduleTimeout?: string | undefined;
|
|
392
|
+
sticky?: PbStickyStrategy | undefined;
|
|
393
|
+
timeout?: string | undefined;
|
|
394
394
|
onFailure?: {
|
|
395
395
|
name: string;
|
|
396
396
|
timeout?: string | undefined;
|
|
@@ -442,10 +442,6 @@ export declare const CreateWorkflowSchema: z.ZodObject<{
|
|
|
442
442
|
} | undefined;
|
|
443
443
|
}[];
|
|
444
444
|
id: string;
|
|
445
|
-
version?: string | undefined;
|
|
446
|
-
scheduleTimeout?: string | undefined;
|
|
447
|
-
sticky?: PbStickyStrategy | undefined;
|
|
448
|
-
timeout?: string | undefined;
|
|
449
445
|
on?: {
|
|
450
446
|
cron: string;
|
|
451
447
|
event?: undefined;
|
|
@@ -453,6 +449,10 @@ export declare const CreateWorkflowSchema: z.ZodObject<{
|
|
|
453
449
|
event: string;
|
|
454
450
|
cron?: undefined;
|
|
455
451
|
} | undefined;
|
|
452
|
+
version?: string | undefined;
|
|
453
|
+
scheduleTimeout?: string | undefined;
|
|
454
|
+
sticky?: PbStickyStrategy | undefined;
|
|
455
|
+
timeout?: string | undefined;
|
|
456
456
|
onFailure?: {
|
|
457
457
|
name: string;
|
|
458
458
|
timeout?: string | undefined;
|