@hatchet-dev/typescript-sdk 0.8.1 → 0.10.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/step.d.ts CHANGED
@@ -3,7 +3,7 @@ import { Action } from './clients/dispatcher/action-listener';
3
3
  import { LogLevel } from './clients/event/event-client';
4
4
  import { Logger } from './util/logger';
5
5
  import { HatchetClient } from './clients/hatchet-client';
6
- import { WorkflowRunEvent } from './protoc/dispatcher';
6
+ import WorkflowRunRef from './util/workflow-run-ref';
7
7
  export declare const CreateRateLimitSchema: z.ZodObject<{
8
8
  key: z.ZodString;
9
9
  units: z.ZodNumber;
@@ -48,9 +48,16 @@ export declare const CreateStepSchema: z.ZodObject<{
48
48
  units: number;
49
49
  }[] | undefined;
50
50
  }>;
51
- type JSONPrimitive = string | number | boolean | null | Array<JSONPrimitive>;
51
+ export type JsonObject = {
52
+ [Key in string]: JsonValue;
53
+ } & {
54
+ [Key in string]?: JsonValue | undefined;
55
+ };
56
+ export type JsonArray = JsonValue[] | readonly JsonValue[];
57
+ export type JsonPrimitive = string | number | boolean | null;
58
+ export type JsonValue = JsonPrimitive | JsonObject | JsonArray;
52
59
  export type NextStep = {
53
- [key: string]: NextStep | JSONPrimitive | Array<NextStep>;
60
+ [key: string]: JsonValue;
54
61
  };
55
62
  interface ContextData<T, K> {
56
63
  input: T;
@@ -58,15 +65,6 @@ interface ContextData<T, K> {
58
65
  triggered_by: string;
59
66
  user_data: K;
60
67
  }
61
- declare class ChildWorkflowRef<T> {
62
- workflowRunId: Promise<string>;
63
- parentWorkflowRunId: string;
64
- client: HatchetClient;
65
- constructor(workflowRunId: Promise<string>, parentWorkflowRunId: string, client: HatchetClient);
66
- stream(): Promise<AsyncGenerator<WorkflowRunEvent, void, unknown>>;
67
- result(): Promise<T>;
68
- toJSON(): Promise<string>;
69
- }
70
68
  export declare class Context<T, K = {}> {
71
69
  data: ContextData<T, K>;
72
70
  input: T;
@@ -96,7 +94,7 @@ export declare class Context<T, K = {}> {
96
94
  refreshTimeout(incrementBy: string): Promise<void>;
97
95
  releaseSlot(): Promise<void>;
98
96
  putStream(data: string | Uint8Array): Promise<void>;
99
- spawnWorkflow<P = unknown, Q = unknown>(workflowName: string, input: Q, key?: string): ChildWorkflowRef<P>;
97
+ spawnWorkflow<Q = JsonValue, P = JsonValue>(workflowName: string, input: Q, key?: string): WorkflowRunRef<P>;
100
98
  }
101
99
  export type StepRunFunction<T, K> = (ctx: Context<T, K>) => Promise<NextStep | void> | NextStep | void;
102
100
  export interface CreateStep<T, K> extends z.infer<typeof CreateStepSchema> {
package/step.js CHANGED
@@ -31,13 +31,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
31
31
  step((generator = generator.apply(thisArg, _arguments || [])).next());
32
32
  });
33
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
- };
41
34
  var __importDefault = (this && this.__importDefault) || function (mod) {
42
35
  return (mod && mod.__esModule) ? mod : { "default": mod };
43
36
  };
@@ -49,7 +42,6 @@ const z = __importStar(require("zod"));
49
42
  const workflow_1 = require("./workflow");
50
43
  const logger_1 = require("./util/logger");
51
44
  const parse_1 = require("./util/parse");
52
- const dispatcher_1 = require("./protoc/dispatcher");
53
45
  exports.CreateRateLimitSchema = z.object({
54
46
  key: z.string(),
55
47
  units: z.number().min(1),
@@ -61,60 +53,6 @@ exports.CreateStepSchema = z.object({
61
53
  retries: z.number().optional(),
62
54
  rate_limits: z.array(exports.CreateRateLimitSchema).optional(),
63
55
  });
64
- class ChildWorkflowRef {
65
- constructor(workflowRunId, parentWorkflowRunId, client) {
66
- this.workflowRunId = workflowRunId;
67
- this.parentWorkflowRunId = parentWorkflowRunId;
68
- this.client = client;
69
- }
70
- stream() {
71
- return __awaiter(this, void 0, void 0, function* () {
72
- const workflowRunId = yield this.workflowRunId;
73
- const listener = yield this.client.listener.getChildListener(workflowRunId, this.parentWorkflowRunId);
74
- return listener.stream();
75
- });
76
- }
77
- result() {
78
- return __awaiter(this, void 0, void 0, function* () {
79
- const listener = yield this.stream();
80
- return new Promise((resolve, reject) => {
81
- (() => __awaiter(this, void 0, void 0, function* () {
82
- var _a, e_1, _b, _c;
83
- try {
84
- for (var _d = true, _e = __asyncValues(yield listener), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) {
85
- _c = _f.value;
86
- _d = false;
87
- const event = _c;
88
- if (event.eventType === dispatcher_1.WorkflowRunEventType.WORKFLOW_RUN_EVENT_TYPE_FINISHED) {
89
- if (event.results.some((r) => !!r.error)) {
90
- reject(event.results);
91
- return;
92
- }
93
- const result = event.results.reduce((acc, r) => (Object.assign(Object.assign({}, acc), { [r.stepReadableId]: JSON.parse(r.output || '{}') })), {});
94
- resolve(result);
95
- return;
96
- }
97
- }
98
- }
99
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
100
- finally {
101
- try {
102
- if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
103
- }
104
- finally { if (e_1) throw e_1.error; }
105
- }
106
- }))();
107
- });
108
- });
109
- }
110
- toJSON() {
111
- return __awaiter(this, void 0, void 0, function* () {
112
- return JSON.stringify({
113
- workflowRunId: yield this.workflowRunId,
114
- });
115
- });
116
- }
117
- }
118
56
  class Context {
119
57
  constructor(action, client) {
120
58
  this.controller = new AbortController();
@@ -229,14 +167,19 @@ class Context {
229
167
  spawnWorkflow(workflowName, input, key) {
230
168
  const { workflowRunId, stepRunId } = this.action;
231
169
  const name = this.client.config.namespace + workflowName;
232
- const childWorkflowRunIdPromise = this.client.admin.runWorkflow(name, input, {
233
- parentId: workflowRunId,
234
- parentStepRunId: stepRunId,
235
- childKey: key,
236
- childIndex: this.spawnIndex,
237
- });
238
- this.spawnIndex += 1;
239
- return new ChildWorkflowRef(childWorkflowRunIdPromise, workflowRunId, this.client);
170
+ try {
171
+ const resp = this.client.admin.runWorkflow(name, input, {
172
+ parentId: workflowRunId,
173
+ parentStepRunId: stepRunId,
174
+ childKey: key,
175
+ childIndex: this.spawnIndex,
176
+ });
177
+ this.spawnIndex += 1;
178
+ return resp;
179
+ }
180
+ catch (e) {
181
+ throw new hatchet_error_1.default(e.message);
182
+ }
240
183
  }
241
184
  }
242
185
  exports.Context = Context;
@@ -1,11 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getAddressesFromJWT = exports.getTenantIdFromJWT = void 0;
3
+ exports.getTenantIdFromJWT = getTenantIdFromJWT;
4
+ exports.getAddressesFromJWT = getAddressesFromJWT;
4
5
  function getTenantIdFromJWT(token) {
5
6
  const claims = extractClaimsFromJWT(token);
6
7
  return claims.sub;
7
8
  }
8
- exports.getTenantIdFromJWT = getTenantIdFromJWT;
9
9
  function getAddressesFromJWT(token) {
10
10
  const claims = extractClaimsFromJWT(token);
11
11
  return {
@@ -13,7 +13,6 @@ function getAddressesFromJWT(token) {
13
13
  grpcBroadcastAddress: claims.grpc_broadcast_address,
14
14
  };
15
15
  }
16
- exports.getAddressesFromJWT = getAddressesFromJWT;
17
16
  function extractClaimsFromJWT(token) {
18
17
  const parts = token.split('.');
19
18
  if (parts.length !== 3) {
package/util/parse.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parseJSON = void 0;
3
+ exports.parseJSON = parseJSON;
4
4
  function parseJSON(json) {
5
5
  try {
6
6
  const firstParse = JSON.parse(json);
@@ -17,4 +17,3 @@ function parseJSON(json) {
17
17
  throw new Error(`Could not parse JSON: ${e.message}`);
18
18
  }
19
19
  }
20
- exports.parseJSON = parseJSON;
package/util/retrier.js CHANGED
@@ -12,7 +12,7 @@ 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.retrier = void 0;
15
+ exports.retrier = retrier;
16
16
  const sleep_1 = __importDefault(require("./sleep"));
17
17
  const DEFAULT_RETRY_INTERVAL = 5; // seconds
18
18
  const DEFAULT_RETRY_COUNT = 5;
@@ -33,4 +33,3 @@ function retrier(fn_1, logger_1) {
33
33
  throw lastError;
34
34
  });
35
35
  }
36
- exports.retrier = retrier;
@@ -1,3 +1,2 @@
1
- /// <reference types="node" />
2
1
  import { Worker, WorkerOptions } from 'worker_threads';
3
2
  export declare function runThreaded(scriptPath: string, options: WorkerOptions): Worker;
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.runThreaded = void 0;
6
+ exports.runThreaded = runThreaded;
7
7
  const worker_threads_1 = require("worker_threads");
8
8
  const path_1 = __importDefault(require("path"));
9
9
  function runThreaded(scriptPath, options) {
@@ -32,5 +32,4 @@ function runThreaded(scriptPath, options) {
32
32
  : resolvedPath;
33
33
  return new worker_threads_1.Worker(ex, Object.assign(Object.assign({}, options), { eval: isTs ? true : undefined }));
34
34
  }
35
- exports.runThreaded = runThreaded;
36
35
  // execArgv: ? ['--require', 'ts-node/register'] : undefined,
@@ -0,0 +1,17 @@
1
+ import { ListenerClient, StepRunEvent } from '../clients/listener/listener-client';
2
+ type EventualWorkflowRunId = string | Promise<string> | Promise<{
3
+ workflowRunId: string;
4
+ }>;
5
+ export default class WorkflowRunRef<T> {
6
+ workflowRunId: EventualWorkflowRunId;
7
+ parentWorkflowRunId?: string;
8
+ private client;
9
+ constructor(workflowRunId: string | Promise<string> | Promise<{
10
+ workflowRunId: string;
11
+ }>, client: ListenerClient, parentWorkflowRunId?: string);
12
+ getWorkflowRunId(): Promise<string>;
13
+ stream(): Promise<AsyncGenerator<StepRunEvent, void, unknown>>;
14
+ result(): Promise<T>;
15
+ toJSON(): Promise<string>;
16
+ }
17
+ export {};
@@ -0,0 +1,94 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __asyncValues = (this && this.__asyncValues) || function (o) {
12
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
13
+ var m = o[Symbol.asyncIterator], i;
14
+ 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);
15
+ 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); }); }; }
16
+ function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
17
+ };
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ const dispatcher_1 = require("../protoc/dispatcher");
20
+ function getWorkflowRunId(workflowRunId) {
21
+ return __awaiter(this, void 0, void 0, function* () {
22
+ if (typeof workflowRunId === 'string') {
23
+ return workflowRunId;
24
+ }
25
+ if (workflowRunId instanceof Promise) {
26
+ const resolved = yield workflowRunId;
27
+ if (typeof resolved === 'string') {
28
+ return resolved;
29
+ }
30
+ return resolved.workflowRunId;
31
+ }
32
+ throw new Error('Invalid workflowRunId');
33
+ });
34
+ }
35
+ class WorkflowRunRef {
36
+ constructor(workflowRunId, client, parentWorkflowRunId) {
37
+ this.workflowRunId = workflowRunId;
38
+ this.parentWorkflowRunId = parentWorkflowRunId;
39
+ this.client = client;
40
+ }
41
+ getWorkflowRunId() {
42
+ return __awaiter(this, void 0, void 0, function* () {
43
+ return getWorkflowRunId(this.workflowRunId);
44
+ });
45
+ }
46
+ stream() {
47
+ return __awaiter(this, void 0, void 0, function* () {
48
+ const workflowRunId = yield getWorkflowRunId(this.workflowRunId);
49
+ return this.client.stream(workflowRunId);
50
+ });
51
+ }
52
+ result() {
53
+ return __awaiter(this, void 0, void 0, function* () {
54
+ const workflowRunId = yield getWorkflowRunId(this.workflowRunId);
55
+ const streamable = yield this.client.get(workflowRunId);
56
+ return new Promise((resolve, reject) => {
57
+ (() => __awaiter(this, void 0, void 0, function* () {
58
+ var _a, e_1, _b, _c;
59
+ try {
60
+ for (var _d = true, _e = __asyncValues(streamable.stream()), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) {
61
+ _c = _f.value;
62
+ _d = false;
63
+ const event = _c;
64
+ if (event.eventType === dispatcher_1.WorkflowRunEventType.WORKFLOW_RUN_EVENT_TYPE_FINISHED) {
65
+ if (event.results.some((r) => !!r.error)) {
66
+ reject(event.results);
67
+ return;
68
+ }
69
+ const result = event.results.reduce((acc, r) => (Object.assign(Object.assign({}, acc), { [r.stepReadableId]: JSON.parse(r.output || '{}') })), {});
70
+ resolve(result);
71
+ return;
72
+ }
73
+ }
74
+ }
75
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
76
+ finally {
77
+ try {
78
+ if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
79
+ }
80
+ finally { if (e_1) throw e_1.error; }
81
+ }
82
+ }))();
83
+ });
84
+ });
85
+ }
86
+ toJSON() {
87
+ return __awaiter(this, void 0, void 0, function* () {
88
+ return JSON.stringify({
89
+ workflowRunId: yield this.workflowRunId,
90
+ });
91
+ });
92
+ }
93
+ }
94
+ exports.default = WorkflowRunRef;