@hatchet-dev/typescript-sdk 1.6.2 → 1.7.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.
Files changed (50) hide show
  1. package/clients/admin/admin-client.d.ts +1 -0
  2. package/clients/admin/admin-client.js +5 -11
  3. package/clients/event/event-client.js +3 -4
  4. package/clients/hatchet-client/hatchet-client.d.ts +0 -4
  5. package/clients/hatchet-client/hatchet-client.js +0 -5
  6. package/clients/rest/generated/Api.d.ts +62 -1
  7. package/clients/rest/generated/Api.js +39 -0
  8. package/clients/rest/generated/data-contracts.d.ts +201 -161
  9. package/clients/rest/generated/data-contracts.js +168 -168
  10. package/clients/worker/worker.d.ts +1 -1
  11. package/clients/worker/worker.js +5 -4
  12. package/package.json +2 -2
  13. package/protoc/v1/workflows.d.ts +11 -0
  14. package/protoc/v1/workflows.js +105 -3
  15. package/step.js +3 -2
  16. package/util/apply-namespace.d.ts +1 -0
  17. package/util/apply-namespace.js +9 -0
  18. package/util/config-loader/config-loader.js +5 -2
  19. package/util/uuid.d.ts +1 -0
  20. package/util/uuid.js +7 -0
  21. package/v1/client/admin.js +3 -8
  22. package/v1/client/client.d.ts +20 -5
  23. package/v1/client/client.interface.d.ts +4 -0
  24. package/v1/client/client.js +48 -11
  25. package/{clients/hatchet-client/features/cron-client.d.ts → v1/client/features/crons.d.ts} +13 -19
  26. package/{clients/hatchet-client/features/cron-client.js → v1/client/features/crons.js} +31 -16
  27. package/v1/client/features/filters.d.ts +1 -0
  28. package/v1/client/features/filters.js +6 -0
  29. package/v1/client/features/index.d.ts +2 -0
  30. package/v1/client/features/index.js +2 -0
  31. package/v1/client/features/runs.d.ts +1 -0
  32. package/v1/client/features/runs.js +7 -0
  33. package/{clients/hatchet-client/features/schedule-client.d.ts → v1/client/features/schedules.d.ts} +12 -18
  34. package/{clients/hatchet-client/features/schedule-client.js → v1/client/features/schedules.js} +31 -16
  35. package/v1/client/features/tenant.d.ts +15 -0
  36. package/v1/client/features/tenant.js +31 -0
  37. package/v1/client/features/workflows.d.ts +10 -3
  38. package/v1/client/features/workflows.js +43 -1
  39. package/v1/client/worker/context.js +3 -2
  40. package/v1/client/worker/worker-internal.d.ts +1 -2
  41. package/v1/client/worker/worker-internal.js +11 -5
  42. package/v1/declaration.d.ts +7 -2
  43. package/v1/declaration.js +5 -3
  44. package/v1/examples/cancellations/workflow.js +2 -2
  45. package/v1/examples/on_event/workflow.d.ts +1 -0
  46. package/v1/examples/on_event/workflow.js +27 -1
  47. package/v1/examples/simple/cron.js +1 -1
  48. package/v1/examples/simple/schedule.js +3 -3
  49. package/version.d.ts +1 -1
  50. package/version.js +1 -1
@@ -0,0 +1,15 @@
1
+ import { Tenant } from '../../../clients/rest/generated/data-contracts';
2
+ import { HatchetClient } from '../client';
3
+ /**
4
+ * Client for managing Tenants
5
+ */
6
+ export declare class TenantClient {
7
+ api: HatchetClient['api'];
8
+ tenantId: string;
9
+ constructor(client: HatchetClient);
10
+ /**
11
+ * Retrieves the current tenant.
12
+ * @returns The Tenant object.
13
+ */
14
+ get(): Promise<Tenant>;
15
+ }
@@ -0,0 +1,31 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.TenantClient = void 0;
13
+ /**
14
+ * Client for managing Tenants
15
+ */
16
+ class TenantClient {
17
+ constructor(client) {
18
+ this.api = client.api;
19
+ this.tenantId = client.tenantId;
20
+ }
21
+ /**
22
+ * Retrieves the current tenant.
23
+ * @returns The Tenant object.
24
+ */
25
+ get() {
26
+ return __awaiter(this, void 0, void 0, function* () {
27
+ return (yield this.api.tenantGet(this.tenantId)).data;
28
+ });
29
+ }
30
+ }
31
+ exports.TenantClient = TenantClient;
@@ -1,7 +1,7 @@
1
1
  import { Workflow } from '../../../workflow';
2
- import { BaseWorkflowDeclaration } from '../..';
2
+ import { BaseWorkflowDeclaration, WorkflowDefinition } from '../..';
3
3
  import { HatchetClient } from '../client';
4
- export declare const workflowNameString: (workflow: string | Workflow | BaseWorkflowDeclaration<any, any>) => string;
4
+ export declare const workflowNameString: (workflow: string | Workflow | WorkflowDefinition | BaseWorkflowDeclaration<any, any>) => string;
5
5
  /**
6
6
  * WorkflowsClient is used to list and manage workflows
7
7
  */
@@ -11,7 +11,14 @@ export declare class WorkflowsClient {
11
11
  private workflowCache;
12
12
  private cacheTTL;
13
13
  constructor(client: HatchetClient, cacheTTL?: number);
14
- get(workflow: string | BaseWorkflowDeclaration<any, any> | Workflow): Promise<any>;
14
+ /**
15
+ * Gets the workflow ID from a workflow name, ID, or object.
16
+ * If the input is not a valid UUID, it will look up the workflow by name.
17
+ * @param workflow - The workflow name, ID, or object.
18
+ * @returns The workflow ID as a string.
19
+ */
20
+ getWorkflowIdFromName(workflow: string | Workflow | WorkflowDefinition | BaseWorkflowDeclaration<any, any>): Promise<string>;
21
+ get(workflow: string | BaseWorkflowDeclaration<any, any> | Workflow): Promise<import("../../../clients/rest/generated/data-contracts").Workflow>;
15
22
  list(opts?: Parameters<typeof this.api.workflowList>[1]): Promise<import("../../../clients/rest/generated/data-contracts").WorkflowList>;
16
23
  delete(workflow: string | BaseWorkflowDeclaration<any, any> | Workflow): Promise<void>;
17
24
  }
@@ -10,7 +10,16 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.WorkflowsClient = exports.workflowNameString = void 0;
13
- const workflowNameString = (workflow) => (typeof workflow === 'string' ? workflow : workflow.id);
13
+ const uuid_1 = require("../../../util/uuid");
14
+ const workflowNameString = (workflow) => {
15
+ if (typeof workflow === 'string') {
16
+ return workflow;
17
+ }
18
+ if (typeof workflow === 'object' && 'id' in workflow) {
19
+ return workflow.id;
20
+ }
21
+ return workflow.name;
22
+ };
14
23
  exports.workflowNameString = workflowNameString;
15
24
  /**
16
25
  * WorkflowsClient is used to list and manage workflows
@@ -28,6 +37,39 @@ class WorkflowsClient {
28
37
  this.cacheTTL = cacheTTL;
29
38
  }
30
39
  }
40
+ /**
41
+ * Gets the workflow ID from a workflow name, ID, or object.
42
+ * If the input is not a valid UUID, it will look up the workflow by name.
43
+ * @param workflow - The workflow name, ID, or object.
44
+ * @returns The workflow ID as a string.
45
+ */
46
+ getWorkflowIdFromName(workflow) {
47
+ return __awaiter(this, void 0, void 0, function* () {
48
+ const str = (() => {
49
+ if (typeof workflow === 'string') {
50
+ return workflow;
51
+ }
52
+ if (typeof workflow === 'object' && 'name' in workflow) {
53
+ return workflow.name;
54
+ }
55
+ if (typeof workflow === 'object' && 'id' in workflow) {
56
+ if (!workflow.id) {
57
+ throw new Error('Workflow ID is required');
58
+ }
59
+ return workflow.id;
60
+ }
61
+ throw new Error('Invalid workflow: must be a string, Workflow object, or WorkflowDefinition object');
62
+ })();
63
+ if (!(0, uuid_1.isValidUUID)(str)) {
64
+ const wf = yield this.get(str);
65
+ if (!wf) {
66
+ throw new Error('Invalid workflow ID: must be a valid UUID');
67
+ }
68
+ return wf.metadata.id;
69
+ }
70
+ return str;
71
+ });
72
+ }
31
73
  get(workflow) {
32
74
  return __awaiter(this, void 0, void 0, function* () {
33
75
  // Get workflow name string
@@ -22,6 +22,7 @@ const conditions_1 = require("../../conditions");
22
22
  const transformer_1 = require("../../conditions/transformer");
23
23
  const condition_1 = require("../../../protoc/v1/shared/condition");
24
24
  const step_1 = require("../../../step");
25
+ const apply_namespace_1 = require("../../../util/apply-namespace");
25
26
  class Context {
26
27
  constructor(action, v1, worker) {
27
28
  // @deprecated use ctx.abortController instead
@@ -458,7 +459,7 @@ class Context {
458
459
  else {
459
460
  workflowName = workflow.id;
460
461
  }
461
- const name = this.v1.config.namespace + workflowName;
462
+ const name = (0, apply_namespace_1.applyNamespace)(workflowName, this.v1.config.namespace);
462
463
  const opts = options || {};
463
464
  const { sticky } = opts;
464
465
  if (sticky && !this.worker.hasWorkflow(name)) {
@@ -515,7 +516,7 @@ class Context {
515
516
  else {
516
517
  workflowName = workflow.id;
517
518
  }
518
- const name = this.v1.config.namespace + workflowName;
519
+ const name = (0, apply_namespace_1.applyNamespace)(workflowName, this.v1.config.namespace);
519
520
  const opts = options || {};
520
521
  const { sticky } = opts;
521
522
  if (sticky && !this.worker.hasWorkflow(name)) {
@@ -4,8 +4,7 @@ import HatchetPromise from '../../../util/hatchet-promise/hatchet-promise';
4
4
  import { Workflow } from '../../../workflow';
5
5
  import { Logger } from '../../../util/logger';
6
6
  import { WebhookWorkerCreateRequest } from '../../../clients/rest/generated/data-contracts';
7
- import { BaseWorkflowDeclaration, WorkflowDefinition } from '../../declaration';
8
- import { HatchetClient } from '../..';
7
+ import { BaseWorkflowDeclaration, WorkflowDefinition, HatchetClient } from '../..';
9
8
  import { WorkerLabels } from '../../../clients/dispatcher/dispatcher-client';
10
9
  import { StepRunFunction } from '../../../step';
11
10
  import { Context } from './context';
@@ -29,6 +29,7 @@ const workflows_1 = require("../../../protoc/workflows");
29
29
  const task_1 = require("../../task");
30
30
  const transformer_1 = require("../../conditions/transformer");
31
31
  const step_1 = require("../../../step");
32
+ const apply_namespace_1 = require("../../../util/apply-namespace");
32
33
  const context_1 = require("./context");
33
34
  class V1Worker {
34
35
  constructor(client, options) {
@@ -38,7 +39,7 @@ class V1Worker {
38
39
  this.registeredWorkflowPromises = [];
39
40
  this.labels = {};
40
41
  this.client = client;
41
- this.name = this.client.config.namespace + options.name;
42
+ this.name = (0, apply_namespace_1.applyNamespace)(options.name, this.client.config.namespace);
42
43
  this.action_registry = {};
43
44
  this.maxRuns = options.maxRuns;
44
45
  this.labels = options.labels || {};
@@ -115,9 +116,9 @@ class V1Worker {
115
116
  }
116
117
  registerWorkflowV1(initWorkflow_1) {
117
118
  return __awaiter(this, arguments, void 0, function* (initWorkflow, durable = false) {
118
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v;
119
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x;
119
120
  // patch the namespace
120
- const workflow = Object.assign(Object.assign({}, initWorkflow.definition), { name: (this.client.config.namespace + initWorkflow.definition.name).toLowerCase() });
121
+ const workflow = Object.assign(Object.assign({}, initWorkflow.definition), { name: (0, apply_namespace_1.applyNamespace)(initWorkflow.definition.name, this.client.config.namespace).toLowerCase() });
121
122
  try {
122
123
  const { concurrency } = workflow;
123
124
  let onFailureTask;
@@ -239,6 +240,11 @@ class V1Worker {
239
240
  });
240
241
  }),
241
242
  concurrency: concurrencySolo,
243
+ defaultFilters: (_x = (_w = workflow.defaultFilters) === null || _w === void 0 ? void 0 : _w.map((f) => ({
244
+ scope: f.scope,
245
+ expression: f.expression,
246
+ payload: f.payload ? JSON.stringify(f.payload) : undefined,
247
+ }))) !== null && _x !== void 0 ? _x : [],
242
248
  });
243
249
  this.registeredWorkflowPromises.push(registeredWorkflow);
244
250
  yield registeredWorkflow;
@@ -253,7 +259,7 @@ class V1Worker {
253
259
  registerWorkflow(initWorkflow) {
254
260
  return __awaiter(this, void 0, void 0, function* () {
255
261
  var _a, _b, _c;
256
- const workflow = Object.assign(Object.assign({}, initWorkflow), { id: (this.client.config.namespace + initWorkflow.id).toLowerCase() });
262
+ const workflow = Object.assign(Object.assign({}, initWorkflow), { id: (0, apply_namespace_1.applyNamespace)(initWorkflow.id, this.client.config.namespace).toLowerCase() });
257
263
  try {
258
264
  if (((_a = workflow.concurrency) === null || _a === void 0 ? void 0 : _a.key) && workflow.concurrency.expression) {
259
265
  throw new hatchet_error_1.default('Cannot have both key function and expression in workflow concurrency configuration');
@@ -292,7 +298,7 @@ class V1Worker {
292
298
  description: workflow.description,
293
299
  version: workflow.version || '',
294
300
  eventTriggers: workflow.on && workflow.on.event
295
- ? [this.client.config.namespace + workflow.on.event]
301
+ ? [(0, apply_namespace_1.applyNamespace)(workflow.on.event, this.client.config.namespace)]
296
302
  : [],
297
303
  cronTriggers: workflow.on && workflow.on.cron ? [workflow.on.cron] : [],
298
304
  scheduledTriggers: [],
@@ -1,5 +1,5 @@
1
1
  import WorkflowRunRef from '../util/workflow-run-ref';
2
- import { CronWorkflows, ScheduledWorkflows } from '../clients/rest/generated/data-contracts';
2
+ import { CronWorkflows, ScheduledWorkflows, V1CreateFilterRequest } from '../clients/rest/generated/data-contracts';
3
3
  import { Workflow as WorkflowV0 } from '../workflow';
4
4
  import { IHatchetClient } from './client/client.interface';
5
5
  import { CreateWorkflowTaskOpts, CreateOnFailureTaskOpts, TaskFn, CreateWorkflowDurableTaskOpts, CreateBaseTaskOpts, CreateOnSuccessTaskOpts, Concurrency, DurableTaskFn } from './task';
@@ -39,6 +39,7 @@ export type TaskOutput<O, Key extends string, Fallback> = O extends Record<Key,
39
39
  * Extracts a property from an object type based on task name, or falls back to inferred type
40
40
  */
41
41
  export type TaskOutputType<O, TaskName extends string, InferredType extends OutputType> = TaskName extends keyof O ? O[TaskName] extends OutputType ? O[TaskName] : InferredType : InferredType;
42
+ type DefaultFilter = Omit<V1CreateFilterRequest, 'workflowId'>;
42
43
  export type CreateBaseWorkflowOpts = {
43
44
  /**
44
45
  * The name of the workflow.
@@ -78,6 +79,7 @@ export type CreateBaseWorkflowOpts = {
78
79
  * values: Priority.LOW, Priority.MEDIUM, Priority.HIGH (1, 2, or 3 )
79
80
  */
80
81
  defaultPriority?: Priority;
82
+ defaultFilters?: DefaultFilter[];
81
83
  };
82
84
  export type CreateTaskWorkflowOpts<I extends InputType = UnknownInputType, O extends OutputType = void> = CreateBaseWorkflowOpts & CreateBaseTaskOpts<I, O, TaskFn<I, O>>;
83
85
  export type CreateDurableTaskWorkflowOpts<I extends InputType = UnknownInputType, O extends OutputType = void> = CreateBaseWorkflowOpts & CreateBaseTaskOpts<I, O, DurableTaskFn<I, O>>;
@@ -260,7 +262,10 @@ export declare class BaseWorkflowDeclaration<I extends InputType = UnknownInputT
260
262
  * @returns A promise that resolves with the workflow state.
261
263
  * @throws Error if the workflow is not bound to a Hatchet client.
262
264
  */
263
- get(): Promise<any>;
265
+ get(): Promise<import("../clients/rest/generated/data-contracts").Workflow>;
266
+ /**
267
+ * @deprecated use definition.name instead
268
+ */
264
269
  get id(): string;
265
270
  /**
266
271
  * Get the friendly name of the workflow.
package/v1/declaration.js CHANGED
@@ -111,7 +111,7 @@ class BaseWorkflowDeclaration {
111
111
  if (!this.client) {
112
112
  throw UNBOUND_ERR;
113
113
  }
114
- const scheduled = this.client._v0.schedule.create(this.definition.name, Object.assign({ triggerAt: enqueueAt, input: input }, options));
114
+ const scheduled = this.client.scheduled.create(this.definition.name, Object.assign({ triggerAt: enqueueAt, input: input }, options));
115
115
  return scheduled;
116
116
  });
117
117
  }
@@ -144,7 +144,7 @@ class BaseWorkflowDeclaration {
144
144
  if (!this.client) {
145
145
  throw UNBOUND_ERR;
146
146
  }
147
- const cronDef = this.client._v0.cron.create(this.definition.name, Object.assign(Object.assign({ expression, input: input }, options), { additionalMetadata: options === null || options === void 0 ? void 0 : options.additionalMetadata, name }));
147
+ const cronDef = this.client.crons.create(this.definition.name, Object.assign(Object.assign({ expression, input: input }, options), { additionalMetadata: options === null || options === void 0 ? void 0 : options.additionalMetadata, name }));
148
148
  return cronDef;
149
149
  });
150
150
  }
@@ -204,7 +204,9 @@ class BaseWorkflowDeclaration {
204
204
  // }
205
205
  // return this.client.workflows.unpause(this);
206
206
  // }
207
- // @deprecated use definition.name instead
207
+ /**
208
+ * @deprecated use definition.name instead
209
+ */
208
210
  get id() {
209
211
  return this.definition.name;
210
212
  }
@@ -19,9 +19,9 @@ const hatchet_client_1 = require("../hatchet-client");
19
19
  // > Declaring a Task
20
20
  exports.cancellation = hatchet_client_1.hatchet.task({
21
21
  name: 'cancellation',
22
- fn: (_1, _a) => __awaiter(void 0, [_1, _a], void 0, function* (_, { cancelled }) {
22
+ fn: (_, ctx) => __awaiter(void 0, void 0, void 0, function* () {
23
23
  yield (0, sleep_1.default)(10 * 1000);
24
- if (cancelled) {
24
+ if (ctx.cancelled) {
25
25
  throw new Error('Task was cancelled');
26
26
  }
27
27
  return {
@@ -9,6 +9,7 @@ type LowerOutput = {
9
9
  };
10
10
  };
11
11
  export declare const lower: import("../..").WorkflowDeclaration<Input, LowerOutput>;
12
+ export declare const lowerWithFilter: import("../..").WorkflowDeclaration<Input, LowerOutput>;
12
13
  type UpperOutput = {
13
14
  upper: {
14
15
  TransformedMessage: string;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.upper = exports.lower = exports.SIMPLE_EVENT = void 0;
3
+ exports.upper = exports.lowerWithFilter = exports.lower = exports.SIMPLE_EVENT = void 0;
4
4
  const hatchet_client_1 = require("../hatchet-client");
5
5
  exports.SIMPLE_EVENT = 'simple-event:create';
6
6
  // > Run workflow on event
@@ -10,6 +10,24 @@ exports.lower = hatchet_client_1.hatchet.workflow({
10
10
  onEvents: ['simple-event:create'],
11
11
  });
12
12
  // !!
13
+ // > Workflow with filter
14
+ exports.lowerWithFilter = hatchet_client_1.hatchet.workflow({
15
+ name: 'lower',
16
+ // 👀 Declare the event that will trigger the workflow
17
+ onEvents: ['simple-event:create'],
18
+ defaultFilters: [
19
+ {
20
+ expression: 'true',
21
+ scope: 'example-scope',
22
+ payload: {
23
+ mainCharacter: 'Anna',
24
+ supportingCharacter: 'Stiva',
25
+ location: 'Moscow',
26
+ },
27
+ },
28
+ ],
29
+ });
30
+ // !!
13
31
  exports.lower.task({
14
32
  name: 'lower',
15
33
  fn: (input) => {
@@ -32,3 +50,11 @@ exports.upper.task({
32
50
  };
33
51
  },
34
52
  });
53
+ // > Accessing the filter payload
54
+ exports.lowerWithFilter.task({
55
+ name: 'lowerWithFilter',
56
+ fn: (input, ctx) => {
57
+ console.log(ctx.filterPayload());
58
+ },
59
+ });
60
+ // !!
@@ -27,7 +27,7 @@ function main() {
27
27
  // !!
28
28
  // > List
29
29
  const crons = yield hatchet_client_1.hatchet.crons.list({
30
- workflowId: workflow_1.simple.id,
30
+ workflow: workflow_1.simple,
31
31
  });
32
32
  // !!
33
33
  // eslint-disable-next-line no-console
@@ -26,11 +26,11 @@ function main() {
26
26
  console.log(scheduledRunId);
27
27
  // !!
28
28
  // > Delete a Scheduled Run
29
- yield hatchet_client_1.hatchet.schedules.delete(scheduled);
29
+ yield hatchet_client_1.hatchet.scheduled.delete(scheduled);
30
30
  // !!
31
31
  // > List Scheduled Runs
32
- const scheduledRuns = yield hatchet_client_1.hatchet.schedules.list({
33
- workflowId: workflow_1.simple.id,
32
+ const scheduledRuns = yield hatchet_client_1.hatchet.scheduled.list({
33
+ workflow: workflow_1.simple,
34
34
  });
35
35
  console.log(scheduledRuns);
36
36
  // !!
package/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const HATCHET_VERSION = "1.6.2";
1
+ export declare const HATCHET_VERSION = "1.7.1";
package/version.js CHANGED
@@ -1,4 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.HATCHET_VERSION = void 0;
4
- exports.HATCHET_VERSION = '1.6.2';
4
+ exports.HATCHET_VERSION = '1.7.1';