@hatchet-dev/typescript-sdk 0.1.11 → 0.1.13

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.
@@ -0,0 +1,8 @@
1
+ import api from './api';
2
+ import { Api } from './generated/Api';
3
+ import { Worker as _Worker, Workflow as _Workflow } from './generated/data-contracts';
4
+ import * as APIContracts from './generated/data-contracts';
5
+ type ApiWorker = _Worker;
6
+ type ApiWorkflow = _Workflow;
7
+ export { ApiWorker, ApiWorkflow, APIContracts, Api };
8
+ export default api;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.Api = exports.APIContracts = void 0;
30
+ const api_1 = __importDefault(require("./api"));
31
+ const Api_1 = require("./generated/Api");
32
+ Object.defineProperty(exports, "Api", { enumerable: true, get: function () { return Api_1.Api; } });
33
+ const APIContracts = __importStar(require("./generated/data-contracts"));
34
+ exports.APIContracts = APIContracts;
35
+ exports.default = api_1.default;
@@ -4,7 +4,7 @@ import { StepActionEvent, StepActionEventType, GroupKeyActionEvent, GroupKeyActi
4
4
  import HatchetPromise from '../../util/hatchet-promise/hatchet-promise';
5
5
  import { Workflow } from '../../workflow';
6
6
  import { Logger } from '../../util/logger';
7
- import { Context } from '../../step';
7
+ import { Context, StepRunFunction } from '../../step';
8
8
  export type ActionRegistry = Record<Action['actionId'], Function>;
9
9
  export declare class Worker {
10
10
  serviceName: string;
@@ -15,13 +15,14 @@ export declare class Worker {
15
15
  action_registry: ActionRegistry;
16
16
  listener: ActionListener | undefined;
17
17
  futures: Record<Action['stepRunId'], HatchetPromise<any>>;
18
- contexts: Record<Action['stepRunId'], Context<any>>;
18
+ contexts: Record<Action['stepRunId'], Context<any, any>>;
19
19
  logger: Logger;
20
20
  constructor(client: HatchetClient, options: {
21
21
  name: string;
22
22
  handleKill?: boolean;
23
23
  });
24
24
  registerWorkflow(workflow: Workflow): Promise<void>;
25
+ registerAction<T, K>(actionId: string, action: StepRunFunction<T, K>): void;
25
26
  handleStartStepRun(action: Action): void;
26
27
  handleStartGroupKeyRun(action: Action): void;
27
28
  getStepActionEvent(action: Action, eventType: StepActionEventType, payload?: any): StepActionEvent;
@@ -73,6 +73,7 @@ class Worker {
73
73
  timeout: step.timeout || '60s',
74
74
  inputs: '{}',
75
75
  parents: (_a = step.parents) !== null && _a !== void 0 ? _a : [],
76
+ userData: '{}',
76
77
  });
77
78
  }),
78
79
  },
@@ -90,6 +91,9 @@ class Worker {
90
91
  ? Object.assign(Object.assign({}, this.action_registry), { [`${this.serviceName}:${workflow.concurrency.name}`]: workflow.concurrency.key }) : Object.assign({}, this.action_registry);
91
92
  });
92
93
  }
94
+ registerAction(actionId, action) {
95
+ this.action_registry[actionId] = action;
96
+ }
93
97
  handleStartStepRun(action) {
94
98
  const { actionId } = action;
95
99
  const context = new step_1.Context(action.actionPayload);
package/index.d.ts CHANGED
@@ -2,4 +2,6 @@ import { HatchetClient as Hatchet } from './clients/hatchet-client';
2
2
  export * from './workflow';
3
3
  export * from './step';
4
4
  export * from './clients/worker';
5
+ export * from './clients/rest';
6
+ export * from './clients/admin';
5
7
  export default Hatchet;
package/index.js CHANGED
@@ -18,4 +18,6 @@ const hatchet_client_1 = require("./clients/hatchet-client");
18
18
  __exportStar(require("./workflow"), exports);
19
19
  __exportStar(require("./step"), exports);
20
20
  __exportStar(require("./clients/worker"), exports);
21
+ __exportStar(require("./clients/rest"), exports);
22
+ __exportStar(require("./clients/admin"), exports);
21
23
  exports.default = hatchet_client_1.HatchetClient;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hatchet-dev/typescript-sdk",
3
- "version": "0.1.11",
3
+ "version": "0.1.13",
4
4
  "description": "Background task orchestration & visibility for developers",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -33,14 +33,18 @@
33
33
  "worker:dag": "npm run exec -- ./examples/dag-worker.ts",
34
34
  "worker:concurrency": "npm run exec -- ./examples/concurrency/concurrency-worker.ts",
35
35
  "event:concurrency": "npm run exec -- ./examples/concurrency/concurrency-event.ts",
36
+ "api": "npm run exec -- ./examples/api.ts",
36
37
  "prepublish": "cp package.json dist/package.json;",
37
- "publish:ci": "rm -rf ./dist && npm run tsc:build && npm run prepublish && cd dist && npm publish --access public --no-git-checks"
38
+ "publish:ci": "rm -rf ./dist && npm run tsc:build && npm run prepublish && cd dist && npm publish --access public --no-git-checks",
39
+ "generate-docs": "typedoc"
38
40
  },
39
41
  "keywords": [],
40
42
  "author": "",
41
43
  "license": "MIT",
42
44
  "devDependencies": {
45
+ "@types/node": "^20.11.16",
43
46
  "@types/jest": "^29.5.11",
47
+ "@types/node": "^20.11.16",
44
48
  "@typescript-eslint/eslint-plugin": "^6.4.0",
45
49
  "autoprefixer": "^10.4.16",
46
50
  "dotenv-cli": "^7.3.0",
@@ -62,14 +66,19 @@
62
66
  "ts-jest": "^29.1.1",
63
67
  "ts-node": "^10.9.2",
64
68
  "ts-proto": "^1.167.0",
69
+ "typedoc": "^0.25.7",
70
+ "typedoc-plugin-markdown": "^3.17.1",
65
71
  "typescript": "^5.3.3"
66
72
  },
67
73
  "dependencies": {
74
+ "@types/qs": "^6.9.11",
75
+ "axios": "^1.6.7",
68
76
  "long": "^5.2.3",
69
77
  "nice-grpc": "^2.1.7",
70
78
  "nice-grpc-common": "^2.0.2",
71
79
  "protobufjs": "^7.2.6",
80
+ "qs": "^6.11.2",
72
81
  "yaml": "^2.3.4",
73
82
  "zod": "^3.22.4"
74
83
  }
75
- }
84
+ }
@@ -62,6 +62,8 @@ export interface CreateWorkflowStepOpts {
62
62
  inputs: string;
63
63
  /** (optional) the step parents. if none are passed in, this is a root step */
64
64
  parents: string[];
65
+ /** (optional) the custom step user data, assuming string representation of JSON */
66
+ userData: string;
65
67
  }
66
68
  /** ListWorkflowsRequest is the request for ListWorkflows. */
67
69
  export interface ListWorkflowsRequest {
@@ -470,7 +470,7 @@ exports.CreateWorkflowJobOpts = {
470
470
  },
471
471
  };
472
472
  function createBaseCreateWorkflowStepOpts() {
473
- return { readableId: "", action: "", timeout: "", inputs: "", parents: [] };
473
+ return { readableId: "", action: "", timeout: "", inputs: "", parents: [], userData: "" };
474
474
  }
475
475
  exports.CreateWorkflowStepOpts = {
476
476
  encode(message, writer = _m0.Writer.create()) {
@@ -489,6 +489,9 @@ exports.CreateWorkflowStepOpts = {
489
489
  for (const v of message.parents) {
490
490
  writer.uint32(42).string(v);
491
491
  }
492
+ if (message.userData !== "") {
493
+ writer.uint32(50).string(message.userData);
494
+ }
492
495
  return writer;
493
496
  },
494
497
  decode(input, length) {
@@ -528,6 +531,12 @@ exports.CreateWorkflowStepOpts = {
528
531
  }
529
532
  message.parents.push(reader.string());
530
533
  continue;
534
+ case 6:
535
+ if (tag !== 50) {
536
+ break;
537
+ }
538
+ message.userData = reader.string();
539
+ continue;
531
540
  }
532
541
  if ((tag & 7) === 4 || tag === 0) {
533
542
  break;
@@ -543,6 +552,7 @@ exports.CreateWorkflowStepOpts = {
543
552
  timeout: isSet(object.timeout) ? globalThis.String(object.timeout) : "",
544
553
  inputs: isSet(object.inputs) ? globalThis.String(object.inputs) : "",
545
554
  parents: globalThis.Array.isArray(object === null || object === void 0 ? void 0 : object.parents) ? object.parents.map((e) => globalThis.String(e)) : [],
555
+ userData: isSet(object.userData) ? globalThis.String(object.userData) : "",
546
556
  };
547
557
  },
548
558
  toJSON(message) {
@@ -563,19 +573,23 @@ exports.CreateWorkflowStepOpts = {
563
573
  if ((_a = message.parents) === null || _a === void 0 ? void 0 : _a.length) {
564
574
  obj.parents = message.parents;
565
575
  }
576
+ if (message.userData !== "") {
577
+ obj.userData = message.userData;
578
+ }
566
579
  return obj;
567
580
  },
568
581
  create(base) {
569
582
  return exports.CreateWorkflowStepOpts.fromPartial(base !== null && base !== void 0 ? base : {});
570
583
  },
571
584
  fromPartial(object) {
572
- var _a, _b, _c, _d, _e;
585
+ var _a, _b, _c, _d, _e, _f;
573
586
  const message = createBaseCreateWorkflowStepOpts();
574
587
  message.readableId = (_a = object.readableId) !== null && _a !== void 0 ? _a : "";
575
588
  message.action = (_b = object.action) !== null && _b !== void 0 ? _b : "";
576
589
  message.timeout = (_c = object.timeout) !== null && _c !== void 0 ? _c : "";
577
590
  message.inputs = (_d = object.inputs) !== null && _d !== void 0 ? _d : "";
578
591
  message.parents = ((_e = object.parents) === null || _e === void 0 ? void 0 : _e.map((e) => e)) || [];
592
+ message.userData = (_f = object.userData) !== null && _f !== void 0 ? _f : "";
579
593
  return message;
580
594
  },
581
595
  };
package/step.d.ts CHANGED
@@ -15,20 +15,23 @@ export declare const CreateStepSchema: z.ZodObject<{
15
15
  export type NextStep = {
16
16
  [key: string]: string;
17
17
  };
18
- interface ContextData<T = unknown> {
18
+ interface ContextData<T, K> {
19
19
  input: T;
20
20
  parents: Record<string, any>;
21
- triggered_by_event: string;
21
+ triggered_by: string;
22
+ user_data: K;
22
23
  }
23
- export declare class Context<T = unknown> {
24
- data: ContextData<T>;
24
+ export declare class Context<T, K> {
25
+ data: ContextData<T, K>;
25
26
  controller: AbortController;
26
27
  constructor(payload: string);
27
28
  stepOutput(step: string): string;
28
29
  triggeredByEvent(): boolean;
29
- workflowInput(): any;
30
+ workflowInput(): T;
31
+ userData(): K;
30
32
  }
31
- export interface CreateStep<T> extends z.infer<typeof CreateStepSchema> {
32
- run: (ctx: Context) => Promise<NextStep> | NextStep | void;
33
+ export type StepRunFunction<T, K> = (ctx: Context<T, K>) => Promise<NextStep> | NextStep | void;
34
+ export interface CreateStep<T, K> extends z.infer<typeof CreateStepSchema> {
35
+ run: StepRunFunction<T, K>;
33
36
  }
34
37
  export {};
package/step.js CHANGED
@@ -56,11 +56,15 @@ class Context {
56
56
  }
57
57
  triggeredByEvent() {
58
58
  var _a;
59
- return ((_a = this.data) === null || _a === void 0 ? void 0 : _a.triggered_by_event) === 'event';
59
+ return ((_a = this.data) === null || _a === void 0 ? void 0 : _a.triggered_by) === 'event';
60
60
  }
61
61
  workflowInput() {
62
62
  var _a;
63
- return ((_a = this.data) === null || _a === void 0 ? void 0 : _a.input) || {};
63
+ return (_a = this.data) === null || _a === void 0 ? void 0 : _a.input;
64
+ }
65
+ userData() {
66
+ var _a;
67
+ return (_a = this.data) === null || _a === void 0 ? void 0 : _a.user_data;
64
68
  }
65
69
  }
66
70
  exports.Context = Context;
@@ -4,10 +4,10 @@ interface LoadClientConfigOptions {
4
4
  path?: string;
5
5
  }
6
6
  export declare class ConfigLoader {
7
- static load_client_config(config?: LoadClientConfigOptions): Partial<ClientConfig>;
7
+ static loadClientConfig(config?: LoadClientConfigOptions): Partial<ClientConfig>;
8
8
  static get default_yaml_config_path(): string;
9
9
  static createCredentials(config: ClientConfig['tls_config']): ChannelCredentials;
10
- static load_yaml_config(path?: string): ClientConfig | undefined;
10
+ static loadYamlConfig(path?: string): ClientConfig | undefined;
11
11
  private static env;
12
12
  }
13
13
  export {};
@@ -30,11 +30,12 @@ const p = __importStar(require("path"));
30
30
  const zod_1 = require("zod");
31
31
  const hatchet_client_1 = require("../../clients/hatchet-client");
32
32
  const nice_grpc_1 = require("nice-grpc");
33
+ const token_1 = require("./token");
33
34
  const DEFAULT_CONFIG_FILE = '.hatchet.yaml';
34
35
  class ConfigLoader {
35
- static load_client_config(config) {
36
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
37
- const yaml = this.load_yaml_config(config === null || config === void 0 ? void 0 : config.path);
36
+ static loadClientConfig(config) {
37
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w;
38
+ const yaml = this.loadYamlConfig(config === null || config === void 0 ? void 0 : config.path);
38
39
  const tlsConfig = {
39
40
  tls_strategy: (_c = (_b = (_a = yaml === null || yaml === void 0 ? void 0 : yaml.tls_config) === null || _a === void 0 ? void 0 : _a.tls_strategy) !== null && _b !== void 0 ? _b : this.env('HATCHET_CLIENT_TLS_STRATEGY')) !== null && _c !== void 0 ? _c : 'tls',
40
41
  cert_file: (_e = (_d = yaml === null || yaml === void 0 ? void 0 : yaml.tls_config) === null || _d === void 0 ? void 0 : _d.cert_file) !== null && _e !== void 0 ? _e : this.env('HATCHET_CLIENT_TLS_CERT_FILE'),
@@ -42,11 +43,30 @@ class ConfigLoader {
42
43
  ca_file: (_j = (_h = yaml === null || yaml === void 0 ? void 0 : yaml.tls_config) === null || _h === void 0 ? void 0 : _h.ca_file) !== null && _j !== void 0 ? _j : this.env('HATCHET_CLIENT_TLS_ROOT_CA_FILE'),
43
44
  server_name: (_l = (_k = yaml === null || yaml === void 0 ? void 0 : yaml.tls_config) === null || _k === void 0 ? void 0 : _k.server_name) !== null && _l !== void 0 ? _l : this.env('HATCHET_CLIENT_TLS_SERVER_NAME'),
44
45
  };
46
+ const token = (_m = yaml === null || yaml === void 0 ? void 0 : yaml.token) !== null && _m !== void 0 ? _m : this.env('HATCHET_CLIENT_TOKEN');
47
+ let grpcBroadcastAddress;
48
+ let apiUrl;
49
+ const tenantId = (0, token_1.getTenantIdFromJWT)(token);
50
+ if (!tenantId) {
51
+ throw new Error('Tenant ID not found in subject claim of token');
52
+ }
53
+ try {
54
+ const addresses = (0, token_1.getAddressesFromJWT)(token);
55
+ grpcBroadcastAddress =
56
+ (_p = (_o = yaml === null || yaml === void 0 ? void 0 : yaml.host_port) !== null && _o !== void 0 ? _o : this.env('HATCHET_CLIENT_HOST_PORT')) !== null && _p !== void 0 ? _p : addresses.grpcBroadcastAddress;
57
+ apiUrl = (_r = (_q = yaml === null || yaml === void 0 ? void 0 : yaml.api_url) !== null && _q !== void 0 ? _q : this.env('HATCHET_CLIENT_API_URL')) !== null && _r !== void 0 ? _r : addresses.serverUrl;
58
+ }
59
+ catch (e) {
60
+ grpcBroadcastAddress = (_s = yaml === null || yaml === void 0 ? void 0 : yaml.host_port) !== null && _s !== void 0 ? _s : this.env('HATCHET_CLIENT_HOST_PORT');
61
+ apiUrl = (_t = yaml === null || yaml === void 0 ? void 0 : yaml.api_url) !== null && _t !== void 0 ? _t : this.env('HATCHET_CLIENT_API_URL');
62
+ }
45
63
  return {
46
- token: (_m = yaml === null || yaml === void 0 ? void 0 : yaml.token) !== null && _m !== void 0 ? _m : this.env('HATCHET_CLIENT_TOKEN'),
47
- host_port: (_o = yaml === null || yaml === void 0 ? void 0 : yaml.host_port) !== null && _o !== void 0 ? _o : this.env('HATCHET_CLIENT_HOST_PORT'),
64
+ token: (_u = yaml === null || yaml === void 0 ? void 0 : yaml.token) !== null && _u !== void 0 ? _u : this.env('HATCHET_CLIENT_TOKEN'),
65
+ host_port: grpcBroadcastAddress,
66
+ api_url: apiUrl,
48
67
  tls_config: tlsConfig,
49
- log_level: (_q = (_p = yaml === null || yaml === void 0 ? void 0 : yaml.log_level) !== null && _p !== void 0 ? _p : this.env('HATCHET_CLIENT_LOG_LEVEL')) !== null && _q !== void 0 ? _q : 'INFO',
68
+ log_level: (_w = (_v = yaml === null || yaml === void 0 ? void 0 : yaml.log_level) !== null && _v !== void 0 ? _v : this.env('HATCHET_CLIENT_LOG_LEVEL')) !== null && _w !== void 0 ? _w : 'INFO',
69
+ tenant_id: tenantId,
50
70
  };
51
71
  }
52
72
  static get default_yaml_config_path() {
@@ -62,7 +82,7 @@ class ConfigLoader {
62
82
  const certChain = config.cert_file ? (0, fs_1.readFileSync)(config.cert_file) : null;
63
83
  return nice_grpc_1.ChannelCredentials.createSsl(rootCerts, privateKey, certChain);
64
84
  }
65
- static load_yaml_config(path) {
85
+ static loadYamlConfig(path) {
66
86
  try {
67
87
  const configFile = (0, fs_1.readFileSync)(p.join(__dirname, path !== null && path !== void 0 ? path : this.default_yaml_config_path), 'utf8');
68
88
  const config = (0, yaml_1.parse)(configFile);
@@ -0,0 +1,5 @@
1
+ export declare function getTenantIdFromJWT(token: string): string;
2
+ export declare function getAddressesFromJWT(token: string): {
3
+ serverUrl: string;
4
+ grpcBroadcastAddress: string;
5
+ };
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getAddressesFromJWT = exports.getTenantIdFromJWT = void 0;
4
+ function getTenantIdFromJWT(token) {
5
+ const claims = extractClaimsFromJWT(token);
6
+ return claims.sub;
7
+ }
8
+ exports.getTenantIdFromJWT = getTenantIdFromJWT;
9
+ function getAddressesFromJWT(token) {
10
+ const claims = extractClaimsFromJWT(token);
11
+ return {
12
+ serverUrl: claims.server_url,
13
+ grpcBroadcastAddress: claims.grpc_broadcast_address,
14
+ };
15
+ }
16
+ exports.getAddressesFromJWT = getAddressesFromJWT;
17
+ function extractClaimsFromJWT(token) {
18
+ const parts = token.split('.');
19
+ if (parts.length !== 3) {
20
+ throw new Error('Invalid token format');
21
+ }
22
+ const claimsPart = parts[1];
23
+ const claimsData = atob(claimsPart.replace(/-/g, '+').replace(/_/g, '/'));
24
+ const claims = JSON.parse(claimsData);
25
+ return claims;
26
+ }
package/workflow.d.ts CHANGED
@@ -106,6 +106,6 @@ export interface Workflow extends z.infer<typeof CreateWorkflowSchema> {
106
106
  concurrency?: z.infer<typeof WorkflowConcurrency> & {
107
107
  key: (ctx: any) => string;
108
108
  };
109
- steps: CreateStep<any>[];
109
+ steps: CreateStep<any, any>[];
110
110
  }
111
111
  export {};