@hatchet-dev/typescript-sdk 1.0.1 → 1.0.3

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 (35) hide show
  1. package/package.json +1 -1
  2. package/v1/client/client.d.ts +3 -3
  3. package/v1/client/features/runs.d.ts +1 -1
  4. package/v1/client/features/runs.js +0 -1
  5. package/v1/declaration.d.ts +40 -18
  6. package/v1/declaration.js +49 -3
  7. package/v1/examples/cancellations/workflow.d.ts +2 -2
  8. package/v1/examples/concurrency-rr/workflow.js +3 -1
  9. package/v1/examples/durable-sleep/workflow.d.ts +1 -1
  10. package/v1/examples/durable-sleep/workflow.js +1 -1
  11. package/v1/examples/on_failure/run.js +1 -1
  12. package/v1/examples/on_failure/worker.js +1 -1
  13. package/v1/examples/on_failure/workflow.d.ts +1 -1
  14. package/v1/examples/on_failure/workflow.js +12 -8
  15. package/v1/examples/on_success/run.js +0 -2
  16. package/v1/examples/on_success/worker.js +1 -1
  17. package/v1/examples/on_success/workflow.d.ts +1 -4
  18. package/v1/examples/on_success/workflow.js +10 -25
  19. package/v1/examples/rate_limit/run.d.ts +1 -0
  20. package/v1/examples/rate_limit/run.js +29 -0
  21. package/v1/examples/rate_limit/worker.d.ts +1 -0
  22. package/v1/examples/rate_limit/worker.js +24 -0
  23. package/v1/examples/rate_limit/workflow.d.ts +5 -0
  24. package/v1/examples/rate_limit/workflow.js +40 -0
  25. package/v1/examples/retries/workflow.d.ts +3 -3
  26. package/v1/examples/retries/workflow.js +7 -4
  27. package/v1/examples/simple/client-run.js +2 -2
  28. package/v1/examples/simple/run.js +2 -4
  29. package/v1/examples/sticky/workflow.d.ts +1 -1
  30. package/v1/examples/timeouts/workflow.d.ts +2 -2
  31. package/v1/index.d.ts +1 -0
  32. package/v1/index.js +1 -0
  33. package/v1/task.d.ts +2 -2
  34. package/version.d.ts +1 -1
  35. package/version.js +1 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hatchet-dev/typescript-sdk",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Background task orchestration & visibility for developers",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [
@@ -51,7 +51,7 @@ export declare class HatchetClient implements IHatchetClient {
51
51
  * @returns A new Workflow instance
52
52
  * @note It is possible to create an orphaned workflow if no client is available using @hatchet/client CreateWorkflow
53
53
  */
54
- workflow<T extends JsonObject = any, K extends JsonObject = any>(options: CreateWorkflowOpts): WorkflowDeclaration<T, K>;
54
+ workflow<T extends JsonObject | unknown = unknown, K extends JsonObject | unknown = unknown>(options: CreateWorkflowOpts): WorkflowDeclaration<T, K>;
55
55
  /**
56
56
  * Creates a new task workflow.
57
57
  * Types can be explicitly specified as generics or inferred from the function signature.
@@ -60,14 +60,14 @@ export declare class HatchetClient implements IHatchetClient {
60
60
  * @param options Task configuration options
61
61
  * @returns A TaskWorkflowDeclaration instance
62
62
  */
63
- task<T extends JsonObject, K extends JsonObject>(options: CreateTaskWorkflowOpts<T, K>): TaskWorkflowDeclaration<T, K>;
63
+ task<T extends JsonObject | unknown, K extends JsonObject | unknown>(options: CreateTaskWorkflowOpts<T, K>): TaskWorkflowDeclaration<T, K>;
64
64
  /**
65
65
  * Creates a new task workflow with types inferred from the function parameter.
66
66
  * @template Fn The type of the task function with input and output extending JsonObject
67
67
  * @param options Task configuration options with function that defines types
68
68
  * @returns A TaskWorkflowDeclaration instance with inferred types
69
69
  */
70
- task<Fn extends (input: I, ctx?: any) => O | Promise<O>, I extends JsonObject = Parameters<Fn>[0], O extends JsonObject = ReturnType<Fn> extends Promise<infer P> ? P extends JsonObject ? P : never : ReturnType<Fn> extends JsonObject ? ReturnType<Fn> : never>(options: {
70
+ task<Fn extends (input: I, ctx?: any) => O | Promise<O>, I extends JsonObject | unknown = Parameters<Fn>[0], O extends JsonObject | unknown = ReturnType<Fn> extends Promise<infer P> ? P extends JsonObject ? P : never : ReturnType<Fn> extends JsonObject ? ReturnType<Fn> : never>(options: {
71
71
  fn: Fn;
72
72
  } & Omit<CreateTaskWorkflowOpts<I, O>, 'fn'>): TaskWorkflowDeclaration<I, O>;
73
73
  /**
@@ -1,7 +1,7 @@
1
1
  import WorkflowRunRef from '../../../util/workflow-run-ref';
2
2
  import { V1TaskStatus } from '../../../clients/rest/generated/data-contracts';
3
- import { HatchetClient } from '../client';
4
3
  import { WorkflowsClient } from './workflows';
4
+ import { HatchetClient } from '../client';
5
5
  export type RunFilter = {
6
6
  since: Date;
7
7
  until?: Date;
@@ -19,7 +19,6 @@ class RunsClient {
19
19
  this.tenantId = client.tenantId;
20
20
  this.workflows = client.workflows;
21
21
  }
22
- // TODO expose streaming methods?
23
22
  get(run) {
24
23
  return __awaiter(this, void 0, void 0, function* () {
25
24
  const runId = typeof run === 'string' ? run : yield run.getWorkflowRunId();
@@ -54,20 +54,8 @@ export type CreateBaseWorkflowOpts = {
54
54
  */
55
55
  onEvents?: string[];
56
56
  concurrency?: TaskConcurrency;
57
- /**
58
- * (optional) onFailure handler for the workflow.
59
- * Invoked when any task in the workflow fails.
60
- * @param ctx The context of the workflow.
61
- */
62
- onFailure?: TaskFn<any, any> | CreateOnFailureTaskOpts<any, any>;
63
- /**
64
- * (optional) onSuccess handler for the workflow.
65
- * Invoked when all tasks in the workflow complete successfully.
66
- * @param ctx The context of the workflow.
67
- */
68
- onSuccess?: TaskFn<any, any> | CreateOnSuccessTaskOpts<any, any>;
69
57
  };
70
- export type CreateTaskWorkflowOpts<T extends JsonObject = any, K extends JsonObject = any> = CreateBaseWorkflowOpts & CreateBaseTaskOpts<T, K, TaskFn<T, K>>;
58
+ export type CreateTaskWorkflowOpts<T extends JsonObject | unknown = unknown, K extends JsonObject | unknown = unknown> = CreateBaseWorkflowOpts & CreateBaseTaskOpts<T, K, TaskFn<T, K>>;
71
59
  /**
72
60
  * Options for creating a new workflow.
73
61
  */
@@ -138,13 +126,25 @@ export type WorkflowDefinition = CreateWorkflowOpts & {
138
126
  * The durable tasks that make up this workflow.
139
127
  */
140
128
  _durableTasks: CreateWorkflowDurableTaskOpts<any, any>[];
129
+ /**
130
+ * (optional) onFailure handler for the workflow.
131
+ * Invoked when any task in the workflow fails.
132
+ * @param ctx The context of the workflow.
133
+ */
134
+ onFailure?: TaskFn<any, any> | CreateOnFailureTaskOpts<any, any>;
135
+ /**
136
+ * (optional) onSuccess handler for the workflow.
137
+ * Invoked when all tasks in the workflow complete successfully.
138
+ * @param ctx The context of the workflow.
139
+ */
140
+ onSuccess?: TaskFn<any, any> | CreateOnSuccessTaskOpts<any, any>;
141
141
  };
142
142
  /**
143
143
  * Represents a workflow that can be executed by Hatchet.
144
144
  * @template T The input type for the workflow.
145
145
  * @template K The return type of the workflow.
146
146
  */
147
- export declare class BaseWorkflowDeclaration<T extends JsonObject, K extends JsonObject> {
147
+ export declare class BaseWorkflowDeclaration<T extends JsonObject | unknown, K extends JsonObject | unknown> {
148
148
  /**
149
149
  * The Hatchet client instance used to execute the workflow.
150
150
  */
@@ -242,7 +242,7 @@ export declare class BaseWorkflowDeclaration<T extends JsonObject, K extends Jso
242
242
  */
243
243
  get name(): string;
244
244
  }
245
- export declare class WorkflowDeclaration<T extends JsonObject, K extends JsonObject> extends BaseWorkflowDeclaration<T, K> {
245
+ export declare class WorkflowDeclaration<T extends JsonObject | unknown = unknown, K extends JsonObject | unknown = unknown> extends BaseWorkflowDeclaration<T, K> {
246
246
  /**
247
247
  * Adds a task to the workflow.
248
248
  * The return type will be either the property on K that corresponds to the task name,
@@ -256,6 +256,28 @@ export declare class WorkflowDeclaration<T extends JsonObject, K extends JsonObj
256
256
  name: Name;
257
257
  fn: (input: T, ctx: Context<T>) => TaskOutputType<K, Name, L> | Promise<TaskOutputType<K, Name, L>>;
258
258
  }) | TaskWorkflowDeclaration<any, any>): CreateWorkflowTaskOpts<T, TaskOutputType<K, Name, L>>;
259
+ /**
260
+ * Adds an onFailure task to the workflow.
261
+ * This will only run if any task in the workflow fails.
262
+ * @template Name The literal string name of the task.
263
+ * @template L The inferred return type of the task function.
264
+ * @param options The task configuration options.
265
+ * @returns The task options that were added.
266
+ */
267
+ onFailure<Name extends string, L>(options: (Omit<CreateOnFailureTaskOpts<T, TaskOutputType<K, Name, L>>, 'fn'> & {
268
+ fn: (input: T, ctx: Context<T>) => TaskOutputType<K, Name, L> | Promise<TaskOutputType<K, Name, L>>;
269
+ }) | TaskWorkflowDeclaration<any, any>): CreateWorkflowTaskOpts<T, TaskOutputType<K, Name, L>>;
270
+ /**
271
+ * Adds an onSuccess task to the workflow.
272
+ * This will only run if all tasks in the workflow complete successfully.
273
+ * @template Name The literal string name of the task.
274
+ * @template L The inferred return type of the task function.
275
+ * @param options The task configuration options.
276
+ * @returns The task options that were added.
277
+ */
278
+ onSuccess<Name extends string, L>(options: (Omit<CreateOnSuccessTaskOpts<T, TaskOutputType<K, Name, L>>, 'fn'> & {
279
+ fn: (input: T, ctx: Context<T>) => TaskOutputType<K, Name, L> | Promise<TaskOutputType<K, Name, L>>;
280
+ }) | TaskWorkflowDeclaration<any, any>): CreateWorkflowTaskOpts<T, TaskOutputType<K, Name, L>>;
259
281
  /**
260
282
  * Adds a durable task to the workflow.
261
283
  * The return type will be either the property on K that corresponds to the task name,
@@ -270,7 +292,7 @@ export declare class WorkflowDeclaration<T extends JsonObject, K extends JsonObj
270
292
  fn: (input: T, ctx: DurableContext<T>) => TaskOutputType<K, Name, L> | Promise<TaskOutputType<K, Name, L>>;
271
293
  }): CreateWorkflowDurableTaskOpts<T, TaskOutputType<K, Name, L>>;
272
294
  }
273
- export declare class TaskWorkflowDeclaration<T extends JsonObject, K extends JsonObject> extends BaseWorkflowDeclaration<T, K> {
295
+ export declare class TaskWorkflowDeclaration<T extends JsonObject | unknown = unknown, K extends JsonObject | unknown = unknown> extends BaseWorkflowDeclaration<T, K> {
274
296
  private _standalone_task_name;
275
297
  constructor(options: CreateTaskWorkflowOpts<T, K>, client?: IHatchetClient);
276
298
  run(input: T, options?: RunOpts): Promise<K>;
@@ -284,7 +306,7 @@ export declare class TaskWorkflowDeclaration<T extends JsonObject, K extends Jso
284
306
  * @param client Optional Hatchet client instance.
285
307
  * @returns A new TaskWorkflowDeclaration with inferred types.
286
308
  */
287
- export declare function CreateTaskWorkflow<Fn extends (input: I, ctx?: any) => O | Promise<O>, I extends JsonObject = Parameters<Fn>[0], O extends JsonObject = ReturnType<Fn> extends Promise<infer P> ? P extends JsonObject ? P : never : ReturnType<Fn> extends JsonObject ? ReturnType<Fn> : never>(options: {
309
+ export declare function CreateTaskWorkflow<Fn extends (input: I, ctx?: any) => O | Promise<O>, I extends JsonObject | unknown = Parameters<Fn>[0], O extends JsonObject | unknown = ReturnType<Fn> extends Promise<infer P> ? P extends JsonObject ? P : never : ReturnType<Fn> extends JsonObject ? ReturnType<Fn> : never>(options: {
288
310
  fn: Fn;
289
311
  } & Omit<CreateTaskWorkflowOpts<I, O>, 'fn'>, client?: IHatchetClient): TaskWorkflowDeclaration<I, O>;
290
312
  /**
@@ -295,7 +317,7 @@ export declare function CreateTaskWorkflow<Fn extends (input: I, ctx?: any) => O
295
317
  * @param client Optional Hatchet client instance.
296
318
  * @returns A new Workflow instance.
297
319
  */
298
- export declare function CreateWorkflow<T extends JsonObject = any, K extends JsonObject = any>(options: CreateWorkflowOpts, client?: IHatchetClient): WorkflowDeclaration<T, K>;
320
+ export declare function CreateWorkflow<T extends JsonObject | unknown = unknown, K extends JsonObject | unknown = unknown>(options: CreateWorkflowOpts, client?: IHatchetClient): WorkflowDeclaration<T, K>;
299
321
  /**
300
322
  * Creates a new durable task workflow declaration with types inferred from the function parameter.
301
323
  * @template Fn The type of the durable task function
package/v1/declaration.js CHANGED
@@ -81,7 +81,7 @@ class BaseWorkflowDeclaration {
81
81
  }
82
82
  const scheduled = this.client._v0.schedule.create(this.definition.name, {
83
83
  triggerAt: enqueueAt,
84
- input,
84
+ input: input,
85
85
  additionalMetadata: options === null || options === void 0 ? void 0 : options.additionalMetadata,
86
86
  });
87
87
  return scheduled;
@@ -118,7 +118,7 @@ class BaseWorkflowDeclaration {
118
118
  }
119
119
  const cronDef = this.client._v0.cron.create(this.definition.name, {
120
120
  expression,
121
- input,
121
+ input: input,
122
122
  additionalMetadata: options === null || options === void 0 ? void 0 : options.additionalMetadata,
123
123
  name,
124
124
  });
@@ -215,6 +215,52 @@ class WorkflowDeclaration extends BaseWorkflowDeclaration {
215
215
  this.definition._tasks.push(typedOptions);
216
216
  return typedOptions;
217
217
  }
218
+ /**
219
+ * Adds an onFailure task to the workflow.
220
+ * This will only run if any task in the workflow fails.
221
+ * @template Name The literal string name of the task.
222
+ * @template L The inferred return type of the task function.
223
+ * @param options The task configuration options.
224
+ * @returns The task options that were added.
225
+ */
226
+ onFailure(options) {
227
+ var _a;
228
+ let typedOptions;
229
+ if (options instanceof TaskWorkflowDeclaration) {
230
+ typedOptions = options.taskDef;
231
+ }
232
+ else {
233
+ typedOptions = options;
234
+ }
235
+ if (this.definition.onFailure) {
236
+ (_a = this.client) === null || _a === void 0 ? void 0 : _a._v0.logger.warn(`onFailure task will override existing onFailure task`);
237
+ }
238
+ this.definition.onFailure = typedOptions;
239
+ return typedOptions;
240
+ }
241
+ /**
242
+ * Adds an onSuccess task to the workflow.
243
+ * This will only run if all tasks in the workflow complete successfully.
244
+ * @template Name The literal string name of the task.
245
+ * @template L The inferred return type of the task function.
246
+ * @param options The task configuration options.
247
+ * @returns The task options that were added.
248
+ */
249
+ onSuccess(options) {
250
+ var _a;
251
+ let typedOptions;
252
+ if (options instanceof TaskWorkflowDeclaration) {
253
+ typedOptions = options.taskDef;
254
+ }
255
+ else {
256
+ typedOptions = options;
257
+ }
258
+ if (this.definition.onSuccess) {
259
+ (_a = this.client) === null || _a === void 0 ? void 0 : _a._v0.logger.warn(`onSuccess task will override existing onSuccess task`);
260
+ }
261
+ this.definition.onSuccess = typedOptions;
262
+ return typedOptions;
263
+ }
218
264
  /**
219
265
  * Adds a durable task to the workflow.
220
266
  * The return type will be either the property on K that corresponds to the task name,
@@ -242,7 +288,7 @@ class TaskWorkflowDeclaration extends BaseWorkflowDeclaration {
242
288
  run: { get: () => super.run }
243
289
  });
244
290
  return __awaiter(this, void 0, void 0, function* () {
245
- const res = yield _super.run.call(this, input, options);
291
+ const res = (yield _super.run.call(this, input, options));
246
292
  if (Array.isArray(res)) {
247
293
  return res.map((r) => r[this._standalone_task_name]);
248
294
  }
@@ -1,3 +1,3 @@
1
- export declare const cancellation: import("../..").TaskWorkflowDeclaration<import("../../..").JsonObject, {
2
- Completed: true;
1
+ export declare const cancellation: import("../..").TaskWorkflowDeclaration<unknown, {
2
+ Completed: boolean;
3
3
  }>;
@@ -15,14 +15,16 @@ const hatchet_client_1 = require("../hatchet-client");
15
15
  const sleep = (ms) => new Promise((resolve) => {
16
16
  setTimeout(resolve, ms);
17
17
  });
18
+ // ❓ Concurrency Strategy With Key
18
19
  exports.simpleConcurrency = hatchet_client_1.hatchet.workflow({
19
20
  name: 'simple-concurrency',
20
21
  concurrency: {
21
- maxRuns: 100,
22
+ maxRuns: 1,
22
23
  limitStrategy: workflow_1.ConcurrencyLimitStrategy.GROUP_ROUND_ROBIN,
23
24
  expression: 'input.GroupKey',
24
25
  },
25
26
  });
27
+ // !!
26
28
  exports.simpleConcurrency.task({
27
29
  name: 'to-lower',
28
30
  fn: (input) => __awaiter(void 0, void 0, void 0, function* () {
@@ -1 +1 @@
1
- export declare const durableSleep: import("../..").WorkflowDeclaration<any, any>;
1
+ export declare const durableSleep: import("../..").WorkflowDeclaration<unknown, unknown>;
@@ -23,7 +23,7 @@ exports.durableSleep.durableTask({
23
23
  console.log('sleeping for 5s');
24
24
  const sleepRes = yield ctx.sleepFor('5s');
25
25
  console.log('done sleeping for 5s', sleepRes);
26
- // wait for an event or a sleep
26
+ // wait for either an event or a sleep
27
27
  const res = yield ctx.waitFor((0, conditions_1.Or)({
28
28
  eventKey: 'user:event',
29
29
  }, {
@@ -14,7 +14,7 @@ const workflow_1 = require("./workflow");
14
14
  function main() {
15
15
  return __awaiter(this, void 0, void 0, function* () {
16
16
  try {
17
- const res = yield workflow_1.alwaysFail.run({});
17
+ const res = yield workflow_1.failureWorkflow.run({});
18
18
  console.log(res);
19
19
  }
20
20
  catch (e) {
@@ -14,7 +14,7 @@ const workflow_1 = require("./workflow");
14
14
  function main() {
15
15
  return __awaiter(this, void 0, void 0, function* () {
16
16
  const worker = yield hatchet_client_1.hatchet.worker('always-fail-worker', {
17
- workflows: [workflow_1.alwaysFail],
17
+ workflows: [workflow_1.failureWorkflow],
18
18
  });
19
19
  yield worker.start();
20
20
  });
@@ -1 +1 @@
1
- export declare const alwaysFail: import("../..").TaskWorkflowDeclaration<import("../../..").JsonObject, never>;
1
+ export declare const failureWorkflow: import("../..").WorkflowDeclaration<unknown, unknown>;
@@ -9,22 +9,26 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.alwaysFail = void 0;
12
+ exports.failureWorkflow = void 0;
13
13
  /* eslint-disable no-console */
14
14
  const hatchet_client_1 = require("../hatchet-client");
15
15
  // ❓ On Failure Task
16
- exports.alwaysFail = hatchet_client_1.hatchet.task({
16
+ exports.failureWorkflow = hatchet_client_1.hatchet.workflow({
17
17
  name: 'always-fail',
18
- // the onFailure function is called when the task fails
19
- // note: not guaranteed to be called on the same worker
20
- onFailure: (input, ctx) => {
18
+ });
19
+ exports.failureWorkflow.task({
20
+ name: 'always-fail',
21
+ fn: () => __awaiter(void 0, void 0, void 0, function* () {
22
+ throw new Error('intentional failure');
23
+ }),
24
+ });
25
+ exports.failureWorkflow.onFailure({
26
+ name: 'on-failure',
27
+ fn: (input, ctx) => __awaiter(void 0, void 0, void 0, function* () {
21
28
  console.log('onFailure for run:', ctx.workflowRunId());
22
29
  return {
23
30
  'on-failure': 'success',
24
31
  };
25
- },
26
- fn: () => __awaiter(void 0, void 0, void 0, function* () {
27
- throw new Error('intentional failure');
28
32
  }),
29
33
  });
30
34
  // !!
@@ -14,8 +14,6 @@ const workflow_1 = require("./workflow");
14
14
  function main() {
15
15
  return __awaiter(this, void 0, void 0, function* () {
16
16
  try {
17
- const res = yield workflow_1.onSuccess.run({});
18
- console.log(res);
19
17
  const res2 = yield workflow_1.onSuccessDag.run({});
20
18
  console.log(res2);
21
19
  }
@@ -14,7 +14,7 @@ const workflow_1 = require("./workflow");
14
14
  function main() {
15
15
  return __awaiter(this, void 0, void 0, function* () {
16
16
  const worker = yield hatchet_client_1.hatchet.worker('always-succeed-worker', {
17
- workflows: [workflow_1.onSuccessDag, workflow_1.onSuccess],
17
+ workflows: [workflow_1.onSuccessDag],
18
18
  });
19
19
  yield worker.start();
20
20
  });
@@ -1,4 +1 @@
1
- export declare const onSuccess: import("../..").TaskWorkflowDeclaration<import("../../..").JsonObject, {
2
- 'always-succeed': string;
3
- }>;
4
- export declare const onSuccessDag: import("../..").WorkflowDeclaration<any, any>;
1
+ export declare const onSuccessDag: import("../..").WorkflowDeclaration<unknown, unknown>;
@@ -9,36 +9,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.onSuccessDag = exports.onSuccess = void 0;
12
+ exports.onSuccessDag = void 0;
13
13
  /* eslint-disable no-console */
14
14
  const hatchet_client_1 = require("../hatchet-client");
15
- // ❓ On Success Task
16
- exports.onSuccess = hatchet_client_1.hatchet.task({
17
- name: 'run-on-success',
18
- // 👀 onSuccess handler will run if the fn task succeeds
19
- onSuccess: (_, ctx) => {
20
- console.log('onSuccess for run:', ctx.workflowRunId());
21
- return {
22
- 'on-success': 'success',
23
- };
24
- },
25
- fn: () => __awaiter(void 0, void 0, void 0, function* () {
26
- return {
27
- 'always-succeed': 'success',
28
- };
29
- }),
30
- });
31
- // !!
32
15
  // ❓ On Success DAG
33
16
  exports.onSuccessDag = hatchet_client_1.hatchet.workflow({
34
17
  name: 'on-success-dag',
35
- // 👀 onSuccess handler will run if all tasks in the workflow succeed
36
- onSuccess: (_, ctx) => {
37
- console.log('onSuccess for run:', ctx.workflowRunId());
38
- return {
39
- 'on-success': 'success',
40
- };
41
- },
42
18
  });
43
19
  exports.onSuccessDag.task({
44
20
  name: 'always-succeed',
@@ -56,4 +32,13 @@ exports.onSuccessDag.task({
56
32
  };
57
33
  }),
58
34
  });
35
+ // 👀 onSuccess handler will run if all tasks in the workflow succeed
36
+ exports.onSuccessDag.onSuccess({
37
+ fn: (_, ctx) => {
38
+ console.log('onSuccess for run:', ctx.workflowRunId());
39
+ return {
40
+ 'on-success': 'success',
41
+ };
42
+ },
43
+ });
59
44
  // !!
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,29 @@
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
+ /* eslint-disable no-console */
13
+ const workflow_1 = require("./workflow");
14
+ function main() {
15
+ return __awaiter(this, void 0, void 0, function* () {
16
+ try {
17
+ const res = yield workflow_1.rateLimitWorkflow.run({ userId: 'abc' });
18
+ console.log(res);
19
+ }
20
+ catch (e) {
21
+ console.log('error', e);
22
+ }
23
+ });
24
+ }
25
+ if (require.main === module) {
26
+ main()
27
+ .catch(console.error)
28
+ .finally(() => process.exit(0));
29
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,24 @@
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
+ const hatchet_client_1 = require("../hatchet-client");
13
+ const workflow_1 = require("./workflow");
14
+ function main() {
15
+ return __awaiter(this, void 0, void 0, function* () {
16
+ const worker = yield hatchet_client_1.hatchet.worker('rate-limit-worker', {
17
+ workflows: [workflow_1.rateLimitWorkflow],
18
+ });
19
+ yield worker.start();
20
+ });
21
+ }
22
+ if (require.main === module) {
23
+ main();
24
+ }
@@ -0,0 +1,5 @@
1
+ type RateLimitInput = {
2
+ userId: string;
3
+ };
4
+ export declare const rateLimitWorkflow: import("../..").WorkflowDeclaration<RateLimitInput, unknown>;
5
+ export {};
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.rateLimitWorkflow = void 0;
4
+ const workflows_1 = require("../../../protoc/v1/workflows");
5
+ const hatchet_client_1 = require("../hatchet-client");
6
+ exports.rateLimitWorkflow = hatchet_client_1.hatchet.workflow({
7
+ name: 'RateLimitWorkflow',
8
+ });
9
+ // !!
10
+ // ❓ Static
11
+ const RATE_LIMIT_KEY = 'test-limit';
12
+ const task1 = exports.rateLimitWorkflow.task({
13
+ name: 'task1',
14
+ fn: (input) => {
15
+ console.log('executed task1');
16
+ },
17
+ rateLimits: [
18
+ {
19
+ staticKey: RATE_LIMIT_KEY,
20
+ units: 1,
21
+ },
22
+ ],
23
+ });
24
+ // !!
25
+ // ❓ Dynamic
26
+ const task2 = exports.rateLimitWorkflow.task({
27
+ name: 'task2',
28
+ fn: (input) => {
29
+ console.log('executed task2 for user: ', input.userId);
30
+ },
31
+ rateLimits: [
32
+ {
33
+ dynamicKey: 'input.userId',
34
+ units: 1,
35
+ limit: 10,
36
+ duration: workflows_1.RateLimitDuration.MINUTE,
37
+ },
38
+ ],
39
+ });
40
+ // !!
@@ -1,5 +1,5 @@
1
- export declare const retries: import("../..").TaskWorkflowDeclaration<import("../../..").JsonObject, never>;
2
- export declare const retriesWithCount: import("../..").TaskWorkflowDeclaration<import("../../..").JsonObject, {
1
+ export declare const retries: import("../..").TaskWorkflowDeclaration<unknown, never>;
2
+ export declare const retriesWithCount: import("../..").TaskWorkflowDeclaration<unknown, {
3
3
  message: string;
4
4
  }>;
5
- export declare const withBackoff: import("../..").TaskWorkflowDeclaration<import("../../..").JsonObject, never>;
5
+ export declare const withBackoff: import("../..").TaskWorkflowDeclaration<unknown, never>;
@@ -21,7 +21,7 @@ exports.retries = hatchet_client_1.hatchet.task({
21
21
  }),
22
22
  });
23
23
  // !!
24
- // ❓ Step Retries with Count
24
+ // ❓ Retries with Count
25
25
  exports.retriesWithCount = hatchet_client_1.hatchet.task({
26
26
  name: 'retriesWithCount',
27
27
  retries: 3,
@@ -38,13 +38,16 @@ exports.retriesWithCount = hatchet_client_1.hatchet.task({
38
38
  }),
39
39
  });
40
40
  // !!
41
- // ❓ Step Retries with Backoff
41
+ // ❓ Retries with Backoff
42
42
  exports.withBackoff = hatchet_client_1.hatchet.task({
43
43
  name: 'withBackoff',
44
- retries: 3,
44
+ retries: 10,
45
45
  backoff: {
46
- factor: 2,
46
+ // 👀 Maximum number of seconds to wait between retries
47
47
  maxSeconds: 10,
48
+ // 👀 Factor to increase the wait time between retries.
49
+ // This sequence will be 2s, 4s, 8s, 10s, 10s, 10s... due to the maxSeconds limit
50
+ factor: 2,
48
51
  },
49
52
  fn: () => __awaiter(void 0, void 0, void 0, function* () {
50
53
  throw new Error('intentional failure');
@@ -4,11 +4,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
4
4
  const hatchet_client_1 = require("../hatchet-client");
5
5
  hatchet_client_1.hatchet.run('simple', { Message: 'Hello, World!' });
6
6
  hatchet_client_1.hatchet.runNoWait('simple', { Message: 'Hello, World!' }, {});
7
- hatchet_client_1.hatchet.schedule.create('simple', {
7
+ hatchet_client_1.hatchet.schedules.create('simple', {
8
8
  triggerAt: new Date(Date.now() + 1000 * 60 * 60 * 24),
9
9
  input: { Message: 'Hello, World!' },
10
10
  });
11
- hatchet_client_1.hatchet.cron.create('simple', {
11
+ hatchet_client_1.hatchet.crons.create('simple', {
12
12
  name: 'my-cron',
13
13
  expression: '0 0 * * *',
14
14
  input: { Message: 'Hello, World!' },
@@ -10,16 +10,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  /* eslint-disable no-console */
13
- // ❓ Running a Task with Results
14
13
  const workflow_1 = require("./workflow");
15
- // ...
16
14
  function main() {
17
15
  return __awaiter(this, void 0, void 0, function* () {
18
- // 👀 Run the workflow with results
16
+ // Running a Task
19
17
  const res = yield workflow_1.simple.run({
20
18
  Message: 'HeLlO WoRlD',
21
19
  });
22
- // 👀 Access the results of the workflow
20
+ // 👀 Access the results of the Task
23
21
  console.log(res.TransformedMessage);
24
22
  // !!
25
23
  });
@@ -1,4 +1,4 @@
1
- export declare const sticky: import("../..").TaskWorkflowDeclaration<import("../../..").JsonObject, {
1
+ export declare const sticky: import("../..").TaskWorkflowDeclaration<unknown, {
2
2
  result: {
3
3
  Value: number;
4
4
  };
@@ -1,3 +1,3 @@
1
- export declare const cancellation: import("../..").TaskWorkflowDeclaration<import("../../..").JsonObject, {
2
- Completed: true;
1
+ export declare const cancellation: import("../..").TaskWorkflowDeclaration<unknown, {
2
+ Completed: boolean;
3
3
  }>;
package/v1/index.d.ts CHANGED
@@ -3,3 +3,4 @@ export * from './client/features';
3
3
  export * from './client/worker';
4
4
  export * from './declaration';
5
5
  export * from './conditions';
6
+ export * from './client/duration';
package/v1/index.js CHANGED
@@ -19,3 +19,4 @@ __exportStar(require("./client/features"), exports);
19
19
  __exportStar(require("./client/worker"), exports);
20
20
  __exportStar(require("./declaration"), exports);
21
21
  __exportStar(require("./conditions"), exports);
22
+ __exportStar(require("./client/duration"), exports);
package/v1/task.d.ts CHANGED
@@ -172,10 +172,10 @@ export type CreateStandaloneDurableTaskOpts<T, K> = CreateBaseTaskOpts<T, K, Dur
172
172
  * @template T The input type for the task function.
173
173
  * @template K The return type of the task function (can be inferred from the return value of fn).
174
174
  */
175
- export type CreateOnSuccessTaskOpts<T, K> = CreateBaseTaskOpts<T, K, TaskFn<T, K>>;
175
+ export type CreateOnSuccessTaskOpts<T, K> = Omit<CreateBaseTaskOpts<T, K, TaskFn<T, K>>, 'name'>;
176
176
  /**
177
177
  * Options for configuring the onFailure task that is invoked when a task fails.
178
178
  * @template T The input type for the task function.
179
179
  * @template K The return type of the task function (can be inferred from the return value of fn).
180
180
  */
181
- export type CreateOnFailureTaskOpts<T, K> = CreateBaseTaskOpts<T, K, TaskFn<T, K>>;
181
+ export type CreateOnFailureTaskOpts<T, K> = Omit<CreateBaseTaskOpts<T, K, TaskFn<T, K>>, 'name'>;
package/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const HATCHET_VERSION = "1.0.1";
1
+ export declare const HATCHET_VERSION = "1.0.3";
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.0.1';
4
+ exports.HATCHET_VERSION = '1.0.3';