@hatchet-dev/typescript-sdk 0.20.2 → 1.0.0-alpha0

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 (95) hide show
  1. package/clients/hatchet-client/features/cron-client.d.ts +0 -1
  2. package/clients/hatchet-client/features/cron-client.js +6 -3
  3. package/clients/hatchet-client/hatchet-client.d.ts +8 -7
  4. package/clients/hatchet-client/hatchet-client.js +9 -8
  5. package/clients/hatchet-client/hatchet-logger.js +2 -2
  6. package/clients/listener/listener-client.js +1 -0
  7. package/clients/rest/generated/Api.d.ts +201 -1
  8. package/clients/rest/generated/Api.js +108 -0
  9. package/clients/rest/generated/data-contracts.d.ts +270 -8
  10. package/clients/rest/generated/data-contracts.js +20 -1
  11. package/clients/worker/handler.d.ts +2 -2
  12. package/clients/worker/worker.d.ts +4 -4
  13. package/clients/worker/worker.js +3 -3
  14. package/examples/api.js +1 -1
  15. package/examples/example-event-with-results.js +1 -1
  16. package/examples/stream-by-additional-meta.js +1 -1
  17. package/index.d.ts +3 -1
  18. package/index.js +5 -3
  19. package/package.json +2 -2
  20. package/protoc/dispatcher/dispatcher.d.ts +2 -0
  21. package/protoc/dispatcher/dispatcher.js +17 -1
  22. package/protoc/workflows/v1-admin.d.ts +79 -0
  23. package/protoc/workflows/v1-admin.js +529 -0
  24. package/sdk.d.ts +1 -1
  25. package/sdk.js +2 -2
  26. package/step.d.ts +75 -9
  27. package/step.js +65 -2
  28. package/util/hatchet-promise/hatchet-promise.js +1 -0
  29. package/v1/client/client.d.ts +81 -0
  30. package/v1/client/client.interface.d.ts +4 -0
  31. package/v1/client/client.interface.js +2 -0
  32. package/v1/client/client.js +134 -0
  33. package/v1/client/worker.d.ts +82 -0
  34. package/v1/client/worker.js +125 -0
  35. package/v1/examples/child_workflows/run.d.ts +1 -0
  36. package/v1/examples/child_workflows/run.js +30 -0
  37. package/v1/examples/child_workflows/worker.d.ts +1 -0
  38. package/v1/examples/child_workflows/worker.js +24 -0
  39. package/v1/examples/child_workflows/workflow.d.ts +19 -0
  40. package/v1/examples/child_workflows/workflow.js +43 -0
  41. package/v1/examples/client.d.ts +2 -0
  42. package/v1/examples/client.js +5 -0
  43. package/v1/examples/concurrency-rr/load.d.ts +1 -0
  44. package/v1/examples/concurrency-rr/load.js +54 -0
  45. package/v1/examples/concurrency-rr/run.d.ts +1 -0
  46. package/v1/examples/concurrency-rr/run.js +39 -0
  47. package/v1/examples/concurrency-rr/worker.d.ts +1 -0
  48. package/v1/examples/concurrency-rr/worker.js +24 -0
  49. package/v1/examples/concurrency-rr/workflow.d.ts +11 -0
  50. package/v1/examples/concurrency-rr/workflow.js +35 -0
  51. package/v1/examples/dag/run.d.ts +1 -0
  52. package/v1/examples/dag/run.js +24 -0
  53. package/v1/examples/dag/worker.d.ts +1 -0
  54. package/v1/examples/dag/worker.js +24 -0
  55. package/v1/examples/dag/workflow.d.ts +11 -0
  56. package/v1/examples/dag/workflow.js +35 -0
  57. package/v1/examples/deep/run.d.ts +1 -0
  58. package/v1/examples/deep/run.js +28 -0
  59. package/v1/examples/deep/worker.d.ts +1 -0
  60. package/v1/examples/deep/worker.js +25 -0
  61. package/v1/examples/deep/workflow.d.ts +18 -0
  62. package/v1/examples/deep/workflow.js +110 -0
  63. package/v1/examples/legacy/run.d.ts +1 -0
  64. package/v1/examples/legacy/run.js +25 -0
  65. package/v1/examples/legacy/worker.d.ts +1 -0
  66. package/v1/examples/legacy/worker.js +24 -0
  67. package/v1/examples/legacy/workflow.d.ts +2 -0
  68. package/v1/examples/legacy/workflow.js +36 -0
  69. package/v1/examples/on_event/event.d.ts +1 -0
  70. package/v1/examples/on_event/event.js +25 -0
  71. package/v1/examples/on_event/worker.d.ts +1 -0
  72. package/v1/examples/on_event/worker.js +24 -0
  73. package/v1/examples/on_event/workflow.d.ts +17 -0
  74. package/v1/examples/on_event/workflow.js +33 -0
  75. package/v1/examples/simple/cron.d.ts +1 -0
  76. package/v1/examples/simple/cron.js +26 -0
  77. package/v1/examples/simple/delay.d.ts +1 -0
  78. package/v1/examples/simple/delay.js +27 -0
  79. package/v1/examples/simple/run.d.ts +1 -0
  80. package/v1/examples/simple/run.js +24 -0
  81. package/v1/examples/simple/schedule.d.ts +1 -0
  82. package/v1/examples/simple/schedule.js +27 -0
  83. package/v1/examples/simple/worker.d.ts +1 -0
  84. package/v1/examples/simple/worker.js +24 -0
  85. package/v1/examples/simple/workflow.d.ts +5 -0
  86. package/v1/examples/simple/workflow.js +15 -0
  87. package/v1/index.d.ts +0 -0
  88. package/v1/index.js +1 -0
  89. package/v1/task.d.ts +51 -0
  90. package/v1/task.js +2 -0
  91. package/v1/workflow.d.ts +152 -0
  92. package/v1/workflow.js +145 -0
  93. package/version.d.ts +1 -1
  94. package/version.js +1 -1
  95. package/workflow.d.ts +3 -0
@@ -0,0 +1,152 @@
1
+ import WorkflowRunRef from '../util/workflow-run-ref';
2
+ import { Context } from '../step';
3
+ import { CronWorkflows, ScheduledWorkflows } from '../clients/rest/generated/data-contracts';
4
+ import { Workflow as WorkflowV0 } from '../workflow';
5
+ import { IHatchetClient } from './client/client.interface';
6
+ import { CreateTaskOpts } from './task';
7
+ /**
8
+ * Additional metadata that can be attached to a workflow run.
9
+ */
10
+ type AdditionalMetadata = Record<string, string>;
11
+ /**
12
+ * Options for running a workflow.
13
+ */
14
+ export type RunOpts = {
15
+ /**
16
+ * Additional metadata to attach to the workflow run.
17
+ */
18
+ additionalMetadata?: AdditionalMetadata;
19
+ };
20
+ /**
21
+ * Extracts a property from an object type based on task name, or falls back to inferred type
22
+ */
23
+ type TaskOutputType<K, TaskName extends string, InferredType> = TaskName extends keyof K ? K[TaskName] : InferredType;
24
+ /**
25
+ * Options for creating a new workflow.
26
+ */
27
+ export type CreateWorkflowOpts = {
28
+ /**
29
+ * The name of the workflow.
30
+ */
31
+ name: WorkflowV0['id'];
32
+ /**
33
+ * Optional description of the workflow.
34
+ */
35
+ description?: WorkflowV0['description'];
36
+ /**
37
+ * Optional version of the workflow.
38
+ */
39
+ version?: WorkflowV0['version'];
40
+ /**
41
+ * Optional sticky strategy for the workflow.
42
+ */
43
+ sticky?: WorkflowV0['sticky'];
44
+ /**
45
+ * Optional schedule timeout for the workflow.
46
+ */
47
+ scheduleTimeout?: WorkflowV0['scheduleTimeout'];
48
+ /**
49
+ * Optional on config for the workflow.
50
+ */
51
+ on?: WorkflowV0['on'];
52
+ concurrency?: WorkflowV0['concurrency'];
53
+ };
54
+ /**
55
+ * Internal definition of a workflow and its tasks.
56
+ */
57
+ type WorkflowDefinition = CreateWorkflowOpts & {
58
+ /**
59
+ * The tasks that make up this workflow.
60
+ */
61
+ tasks: CreateTaskOpts<any, any>[];
62
+ };
63
+ /**
64
+ * Represents a workflow that can be executed by Hatchet.
65
+ * @template T The input type for the workflow.
66
+ * @template K The return type of the workflow.
67
+ */
68
+ export declare class Workflow<T extends Record<string, any>, K> {
69
+ /**
70
+ * The Hatchet client instance used to execute the workflow.
71
+ */
72
+ client: IHatchetClient | undefined;
73
+ /**
74
+ * The internal workflow definition.
75
+ */
76
+ definition: WorkflowDefinition;
77
+ /**
78
+ * Creates a new workflow instance.
79
+ * @param options The options for creating the workflow.
80
+ * @param client Optional Hatchet client instance.
81
+ */
82
+ constructor(options: CreateWorkflowOpts, client?: IHatchetClient);
83
+ /**
84
+ * Triggers a workflow run without waiting for completion.
85
+ * @param input The input data for the workflow.
86
+ * @param options Optional configuration for this workflow run.
87
+ * @returns A WorkflowRunRef containing the run ID and methods to get results and interact with the run.
88
+ * @throws Error if the workflow is not bound to a Hatchet client.
89
+ */
90
+ enqueue(input: T, options?: RunOpts): WorkflowRunRef<K>;
91
+ /**
92
+ * Executes the workflow with the given input and awaits the results.
93
+ * @param input The input data for the workflow.
94
+ * @param options Optional configuration for this workflow run.
95
+ * @returns A promise that resolves with the workflow result.
96
+ * @throws Error if the workflow is not bound to a Hatchet client.
97
+ */
98
+ run(input: T, options?: RunOpts): Promise<K>;
99
+ run(input: T[], options?: RunOpts): Promise<K[]>;
100
+ /**
101
+ * Schedules a workflow to run at a specific date and time in the future.
102
+ * @param enqueueAt The date when the workflow should be triggered.
103
+ * @param input The input data for the workflow.
104
+ * @param options Optional configuration for this workflow run.
105
+ * @returns A promise that resolves with the scheduled workflow details.
106
+ * @throws Error if the workflow is not bound to a Hatchet client.
107
+ */
108
+ schedule(enqueueAt: Date, input: T, options?: RunOpts): Promise<ScheduledWorkflows>;
109
+ /**
110
+ * Schedules a workflow to run after a specified delay.
111
+ * @param duration The delay in seconds before the workflow should run.
112
+ * @param input The input data for the workflow.
113
+ * @param options Optional configuration for this workflow run.
114
+ * @returns A promise that resolves with the scheduled workflow details.
115
+ * @throws Error if the workflow is not bound to a Hatchet client.
116
+ */
117
+ delay(duration: number, input: T, options?: RunOpts): Promise<ScheduledWorkflows>;
118
+ /**
119
+ * Creates a cron schedule for the workflow.
120
+ * @param name The name of the cron schedule.
121
+ * @param expression The cron expression defining the schedule.
122
+ * @param input The input data for the workflow.
123
+ * @param options Optional configuration for this workflow run.
124
+ * @returns A promise that resolves with the cron workflow details.
125
+ * @throws Error if the workflow is not bound to a Hatchet client.
126
+ */
127
+ cron(name: string, expression: string, input: T, options?: RunOpts): Promise<CronWorkflows>;
128
+ /**
129
+ * Adds a task to the workflow.
130
+ * The return type will be either the property on K that corresponds to the task name,
131
+ * or if there is no matching property, the inferred return type of the function.
132
+ * @template Name The literal string name of the task.
133
+ * @template L The inferred return type of the task function.
134
+ * @param options The task configuration options.
135
+ * @returns The task options that were added.
136
+ */
137
+ task<Name extends string, L>(options: Omit<CreateTaskOpts<T, TaskOutputType<K, Name, L>>, 'fn'> & {
138
+ name: Name;
139
+ fn: (input: T, ctx: Context<T>) => TaskOutputType<K, Name, L> | Promise<TaskOutputType<K, Name, L>>;
140
+ }): CreateTaskOpts<T, TaskOutputType<K, Name, L>>;
141
+ get id(): string;
142
+ }
143
+ /**
144
+ * Creates a new workflow instance.
145
+ * @template T The input type for the workflow.
146
+ * @template K The return type of the workflow.
147
+ * @param options The options for creating the workflow.
148
+ * @param client Optional Hatchet client instance.
149
+ * @returns A new Workflow instance.
150
+ */
151
+ export declare function CreateWorkflow<T extends Record<string, any> = any, K = any>(options: CreateWorkflowOpts, client?: IHatchetClient): Workflow<T, K>;
152
+ export {};
package/v1/workflow.js ADDED
@@ -0,0 +1,145 @@
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.Workflow = void 0;
13
+ exports.CreateWorkflow = CreateWorkflow;
14
+ const UNBOUND_ERR = new Error('workflow unbound to hatchet client, hint: use client.run instead');
15
+ /**
16
+ * Represents a workflow that can be executed by Hatchet.
17
+ * @template T The input type for the workflow.
18
+ * @template K The return type of the workflow.
19
+ */
20
+ class Workflow {
21
+ /**
22
+ * Creates a new workflow instance.
23
+ * @param options The options for creating the workflow.
24
+ * @param client Optional Hatchet client instance.
25
+ */
26
+ constructor(options, client) {
27
+ this.definition = Object.assign(Object.assign({}, options), { tasks: [] });
28
+ this.client = client;
29
+ }
30
+ /**
31
+ * Triggers a workflow run without waiting for completion.
32
+ * @param input The input data for the workflow.
33
+ * @param options Optional configuration for this workflow run.
34
+ * @returns A WorkflowRunRef containing the run ID and methods to get results and interact with the run.
35
+ * @throws Error if the workflow is not bound to a Hatchet client.
36
+ */
37
+ enqueue(input, options) {
38
+ if (!this.client) {
39
+ throw UNBOUND_ERR;
40
+ }
41
+ return this.client.v0.admin.runWorkflow(this.definition.name, input, options);
42
+ }
43
+ run(input, options) {
44
+ return __awaiter(this, void 0, void 0, function* () {
45
+ if (!this.client) {
46
+ throw UNBOUND_ERR;
47
+ }
48
+ if (Array.isArray(input)) {
49
+ // FIXME use bulk endpoint?
50
+ return Promise.all(input.map((i) => this.run(i, options)));
51
+ }
52
+ const res = this.client.v0.admin.runWorkflow(this.definition.name, input, options);
53
+ return res.result();
54
+ });
55
+ }
56
+ /**
57
+ * Schedules a workflow to run at a specific date and time in the future.
58
+ * @param enqueueAt The date when the workflow should be triggered.
59
+ * @param input The input data for the workflow.
60
+ * @param options Optional configuration for this workflow run.
61
+ * @returns A promise that resolves with the scheduled workflow details.
62
+ * @throws Error if the workflow is not bound to a Hatchet client.
63
+ */
64
+ schedule(enqueueAt, input, options) {
65
+ return __awaiter(this, void 0, void 0, function* () {
66
+ if (!this.client) {
67
+ throw UNBOUND_ERR;
68
+ }
69
+ const scheduled = this.client.v0.schedule.create(this.definition.name, {
70
+ triggerAt: enqueueAt,
71
+ input,
72
+ additionalMetadata: options === null || options === void 0 ? void 0 : options.additionalMetadata,
73
+ });
74
+ return scheduled;
75
+ });
76
+ }
77
+ /**
78
+ * Schedules a workflow to run after a specified delay.
79
+ * @param duration The delay in seconds before the workflow should run.
80
+ * @param input The input data for the workflow.
81
+ * @param options Optional configuration for this workflow run.
82
+ * @returns A promise that resolves with the scheduled workflow details.
83
+ * @throws Error if the workflow is not bound to a Hatchet client.
84
+ */
85
+ delay(duration, input, options) {
86
+ return __awaiter(this, void 0, void 0, function* () {
87
+ const now = Date.now();
88
+ const triggerAt = new Date(now + duration * 1000);
89
+ return this.schedule(triggerAt, input, options);
90
+ });
91
+ }
92
+ /**
93
+ * Creates a cron schedule for the workflow.
94
+ * @param name The name of the cron schedule.
95
+ * @param expression The cron expression defining the schedule.
96
+ * @param input The input data for the workflow.
97
+ * @param options Optional configuration for this workflow run.
98
+ * @returns A promise that resolves with the cron workflow details.
99
+ * @throws Error if the workflow is not bound to a Hatchet client.
100
+ */
101
+ cron(name, expression, input, options) {
102
+ return __awaiter(this, void 0, void 0, function* () {
103
+ if (!this.client) {
104
+ throw UNBOUND_ERR;
105
+ }
106
+ const cronDef = this.client.v0.cron.create(this.definition.name, {
107
+ expression,
108
+ input,
109
+ additionalMetadata: options === null || options === void 0 ? void 0 : options.additionalMetadata,
110
+ name,
111
+ });
112
+ return cronDef;
113
+ });
114
+ }
115
+ /**
116
+ * Adds a task to the workflow.
117
+ * The return type will be either the property on K that corresponds to the task name,
118
+ * or if there is no matching property, the inferred return type of the function.
119
+ * @template Name The literal string name of the task.
120
+ * @template L The inferred return type of the task function.
121
+ * @param options The task configuration options.
122
+ * @returns The task options that were added.
123
+ */
124
+ task(options) {
125
+ const typedOptions = options;
126
+ this.definition.tasks.push(typedOptions);
127
+ return typedOptions;
128
+ }
129
+ // @deprecated use definition.name instead
130
+ get id() {
131
+ return this.definition.name;
132
+ }
133
+ }
134
+ exports.Workflow = Workflow;
135
+ /**
136
+ * Creates a new workflow instance.
137
+ * @template T The input type for the workflow.
138
+ * @template K The return type of the workflow.
139
+ * @param options The options for creating the workflow.
140
+ * @param client Optional Hatchet client instance.
141
+ * @returns A new Workflow instance.
142
+ */
143
+ function CreateWorkflow(options, client) {
144
+ return new Workflow(options, client);
145
+ }
package/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const HATCHET_VERSION = "0.20.2";
1
+ export declare const HATCHET_VERSION = "1.0.0-alpha0";
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 = '0.20.2';
4
+ exports.HATCHET_VERSION = '1.0.0-alpha0';
package/workflow.d.ts CHANGED
@@ -478,6 +478,9 @@ export declare const CreateWorkflowSchema: z.ZodObject<{
478
478
  } | undefined;
479
479
  } | undefined;
480
480
  }>;
481
+ /**
482
+ * @deprecated Use client.workflow instead (TODO link to migration doc)
483
+ */
481
484
  export interface Workflow extends z.infer<typeof CreateWorkflowSchema> {
482
485
  concurrency?: z.infer<typeof WorkflowConcurrency> & {
483
486
  key?: (ctx: any) => string;