@hatchet-dev/typescript-sdk 0.1.31 → 0.2.1
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/admin/admin-client.d.ts +6 -1
- package/clients/admin/admin-client.js +2 -5
- package/clients/dispatcher/action-listener.d.ts +11 -0
- package/clients/dispatcher/action-listener.js +71 -1
- package/clients/hatchet-client/hatchet-client.js +1 -1
- package/clients/listener/listener-client.d.ts +32 -9
- package/clients/listener/listener-client.js +187 -53
- package/clients/worker/worker.js +2 -2
- package/package.json +10 -6
- package/protoc/dispatcher/dispatcher.d.ts +93 -2
- package/protoc/dispatcher/dispatcher.js +437 -266
- package/protoc/events/events.d.ts +2 -2
- package/protoc/events/events.js +57 -53
- package/protoc/google/protobuf/timestamp.d.ts +1 -1
- package/protoc/google/protobuf/timestamp.js +2 -2
- package/protoc/google/protobuf/wrappers.d.ts +1 -1
- package/protoc/google/protobuf/wrappers.js +10 -10
- package/protoc/workflows/workflows.d.ts +32 -2
- package/protoc/workflows/workflows.js +298 -138
- package/step.d.ts +18 -5
- package/step.js +89 -4
package/step.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import * as z from 'zod';
|
|
2
2
|
import { Action } from './clients/dispatcher/action-listener';
|
|
3
|
-
import {
|
|
4
|
-
import { EventClient, LogLevel } from './clients/event/event-client';
|
|
3
|
+
import { LogLevel } from './clients/event/event-client';
|
|
5
4
|
import { Logger } from './util/logger';
|
|
5
|
+
import { HatchetClient } from './clients/hatchet-client';
|
|
6
|
+
import { RunEventType } from './clients/listener/listener-client';
|
|
6
7
|
export declare const CreateStepSchema: z.ZodObject<{
|
|
7
8
|
name: z.ZodString;
|
|
8
9
|
parents: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
@@ -29,16 +30,27 @@ interface ContextData<T, K> {
|
|
|
29
30
|
triggered_by: string;
|
|
30
31
|
user_data: K;
|
|
31
32
|
}
|
|
33
|
+
declare class ChildWorkflowRef<T> {
|
|
34
|
+
workflowRunId: Promise<string>;
|
|
35
|
+
client: HatchetClient;
|
|
36
|
+
constructor(workflowRunId: Promise<string>, client: HatchetClient);
|
|
37
|
+
stream(): Promise<AsyncGenerator<{
|
|
38
|
+
type: RunEventType;
|
|
39
|
+
payload: string;
|
|
40
|
+
}, void, unknown>>;
|
|
41
|
+
result(): Promise<T>;
|
|
42
|
+
toJSON(): Promise<string>;
|
|
43
|
+
}
|
|
32
44
|
export declare class Context<T, K> {
|
|
33
45
|
data: ContextData<T, K>;
|
|
34
46
|
input: T;
|
|
35
47
|
controller: AbortController;
|
|
36
48
|
action: Action;
|
|
37
|
-
client:
|
|
38
|
-
eventClient: EventClient;
|
|
49
|
+
client: HatchetClient;
|
|
39
50
|
overridesData: Record<string, any>;
|
|
40
51
|
logger: Logger;
|
|
41
|
-
|
|
52
|
+
spawnIndex: number;
|
|
53
|
+
constructor(action: Action, client: HatchetClient);
|
|
42
54
|
stepOutput(step: string): NextStep;
|
|
43
55
|
triggeredByEvent(): boolean;
|
|
44
56
|
workflowInput(): T;
|
|
@@ -47,6 +59,7 @@ export declare class Context<T, K> {
|
|
|
47
59
|
workflowRunId(): string;
|
|
48
60
|
playground(name: string, defaultValue?: string): string;
|
|
49
61
|
log(message: string, level?: LogLevel): void;
|
|
62
|
+
spawnWorkflow<P = unknown>(workflowName: string, input: T, key?: string): ChildWorkflowRef<P>;
|
|
50
63
|
}
|
|
51
64
|
export type StepRunFunction<T, K> = (ctx: Context<T, K>) => Promise<NextStep> | NextStep | void;
|
|
52
65
|
export interface CreateStep<T, K> extends z.infer<typeof CreateStepSchema> {
|
package/step.js
CHANGED
|
@@ -22,32 +22,106 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
22
22
|
__setModuleDefault(result, mod);
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
35
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
36
|
+
var m = o[Symbol.asyncIterator], i;
|
|
37
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
38
|
+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
39
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
40
|
+
};
|
|
25
41
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
42
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
43
|
};
|
|
28
44
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
45
|
exports.Context = exports.CreateStepSchema = void 0;
|
|
46
|
+
/* eslint-disable max-classes-per-file */
|
|
30
47
|
const hatchet_error_1 = __importDefault(require("./util/errors/hatchet-error"));
|
|
31
48
|
const z = __importStar(require("zod"));
|
|
32
49
|
const workflow_1 = require("./workflow");
|
|
33
50
|
const logger_1 = require("./util/logger");
|
|
34
51
|
const parse_1 = require("./util/parse");
|
|
52
|
+
const listener_client_1 = require("./clients/listener/listener-client");
|
|
35
53
|
exports.CreateStepSchema = z.object({
|
|
36
54
|
name: z.string(),
|
|
37
55
|
parents: z.array(z.string()).optional(),
|
|
38
56
|
timeout: workflow_1.HatchetTimeoutSchema.optional(),
|
|
39
57
|
retries: z.number().optional(),
|
|
40
58
|
});
|
|
59
|
+
class ChildWorkflowRef {
|
|
60
|
+
constructor(workflowRunId, client) {
|
|
61
|
+
this.workflowRunId = workflowRunId;
|
|
62
|
+
this.client = client;
|
|
63
|
+
}
|
|
64
|
+
stream() {
|
|
65
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
66
|
+
const workflowRunId = yield this.workflowRunId;
|
|
67
|
+
return this.client.listener.stream(workflowRunId);
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
result() {
|
|
71
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
72
|
+
const workflowRunId = yield this.workflowRunId;
|
|
73
|
+
const listener = yield this.client.listener.get(workflowRunId);
|
|
74
|
+
return new Promise((resolve, reject) => {
|
|
75
|
+
(() => __awaiter(this, void 0, void 0, function* () {
|
|
76
|
+
var _a, e_1, _b, _c;
|
|
77
|
+
try {
|
|
78
|
+
for (var _d = true, _e = __asyncValues(yield listener.stream()), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) {
|
|
79
|
+
_c = _f.value;
|
|
80
|
+
_d = false;
|
|
81
|
+
const event = _c;
|
|
82
|
+
if (event.type === listener_client_1.RunEventType.WORKFLOW_RUN_EVENT_TYPE_FAILED ||
|
|
83
|
+
event.type === listener_client_1.RunEventType.WORKFLOW_RUN_EVENT_TYPE_CANCELLED ||
|
|
84
|
+
event.type === listener_client_1.RunEventType.WORKFLOW_RUN_EVENT_TYPE_TIMED_OUT) {
|
|
85
|
+
reject(new hatchet_error_1.default(event.type));
|
|
86
|
+
listener.close();
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (event.type === listener_client_1.RunEventType.WORKFLOW_RUN_EVENT_TYPE_COMPLETED) {
|
|
90
|
+
resolve(JSON.parse(event.payload));
|
|
91
|
+
listener.close();
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
97
|
+
finally {
|
|
98
|
+
try {
|
|
99
|
+
if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
|
|
100
|
+
}
|
|
101
|
+
finally { if (e_1) throw e_1.error; }
|
|
102
|
+
}
|
|
103
|
+
}))();
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
toJSON() {
|
|
108
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
109
|
+
return JSON.stringify({
|
|
110
|
+
workflowRunId: yield this.workflowRunId,
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
}
|
|
41
115
|
class Context {
|
|
42
|
-
constructor(action, client
|
|
116
|
+
constructor(action, client) {
|
|
43
117
|
this.controller = new AbortController();
|
|
44
118
|
this.overridesData = {};
|
|
119
|
+
this.spawnIndex = 0;
|
|
45
120
|
try {
|
|
46
121
|
const data = (0, parse_1.parseJSON)(action.actionPayload);
|
|
47
122
|
this.data = data;
|
|
48
123
|
this.action = action;
|
|
49
124
|
this.client = client;
|
|
50
|
-
this.eventClient = eventClient;
|
|
51
125
|
this.logger = new logger_1.Logger(`Context Logger`, client.config.log_level);
|
|
52
126
|
// if this is a getGroupKeyRunId, the data is the workflow input
|
|
53
127
|
if (action.getGroupKeyRunId !== '') {
|
|
@@ -92,7 +166,7 @@ class Context {
|
|
|
92
166
|
if (name in this.overridesData) {
|
|
93
167
|
return this.overridesData[name];
|
|
94
168
|
}
|
|
95
|
-
this.client.putOverridesData({
|
|
169
|
+
this.client.dispatcher.putOverridesData({
|
|
96
170
|
stepRunId: this.action.stepRunId,
|
|
97
171
|
path: name,
|
|
98
172
|
value: JSON.stringify(defaultValue),
|
|
@@ -106,7 +180,18 @@ class Context {
|
|
|
106
180
|
this.logger.warn('cannot log from context without stepRunId');
|
|
107
181
|
return;
|
|
108
182
|
}
|
|
109
|
-
this.
|
|
183
|
+
this.client.event.putLog(stepRunId, message, level);
|
|
184
|
+
}
|
|
185
|
+
spawnWorkflow(workflowName, input, key) {
|
|
186
|
+
const { workflowRunId, stepRunId } = this.action;
|
|
187
|
+
const childWorkflowRunIdPromise = this.client.admin.run_workflow(workflowName, input, {
|
|
188
|
+
parentId: workflowRunId,
|
|
189
|
+
parentStepRunId: stepRunId,
|
|
190
|
+
childKey: key,
|
|
191
|
+
childIndex: this.spawnIndex,
|
|
192
|
+
});
|
|
193
|
+
this.spawnIndex += 1;
|
|
194
|
+
return new ChildWorkflowRef(childWorkflowRunIdPromise, this.client);
|
|
110
195
|
}
|
|
111
196
|
}
|
|
112
197
|
exports.Context = Context;
|