@upstash/workflow 0.1.5 → 0.2.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/svelte.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  serve
3
- } from "./chunk-OJZEI7LO.mjs";
3
+ } from "./chunk-5R2BFC3N.mjs";
4
4
 
5
5
  // platforms/svelte.ts
6
6
  var serve2 = (routeFunction, options) => {
@@ -79,7 +79,7 @@ declare class AutoExecutor {
79
79
  *
80
80
  * If a function is already executing (this.executingStep), this
81
81
  * means that there is a nested step which is not allowed. In this
82
- * case, addStep throws QStashWorkflowError.
82
+ * case, addStep throws WorkflowError.
83
83
  *
84
84
  * @param stepInfo step plan to add
85
85
  * @returns result of the step function
@@ -263,10 +263,6 @@ declare class WorkflowContext<TInitialPayload = unknown> {
263
263
  * headers of the initial request
264
264
  */
265
265
  readonly headers: Headers;
266
- /**
267
- * initial payload as a raw string
268
- */
269
- readonly rawInitialPayload: string;
270
266
  /**
271
267
  * Map of environment variables and their values.
272
268
  *
@@ -292,7 +288,7 @@ declare class WorkflowContext<TInitialPayload = unknown> {
292
288
  * Number of retries
293
289
  */
294
290
  readonly retries: number;
295
- constructor({ qstashClient, workflowRunId, headers, steps, url, failureUrl, debug, initialPayload, rawInitialPayload, env, retries, }: {
291
+ constructor({ qstashClient, workflowRunId, headers, steps, url, failureUrl, debug, initialPayload, env, retries, }: {
296
292
  qstashClient: WorkflowClient;
297
293
  workflowRunId: string;
298
294
  headers: Headers;
@@ -301,7 +297,6 @@ declare class WorkflowContext<TInitialPayload = unknown> {
301
297
  failureUrl?: string;
302
298
  debug?: WorkflowLogger;
303
299
  initialPayload: TInitialPayload;
304
- rawInitialPayload?: string;
305
300
  env?: Record<string, string | undefined>;
306
301
  retries?: number;
307
302
  });
@@ -321,7 +316,7 @@ declare class WorkflowContext<TInitialPayload = unknown> {
321
316
  * const [result1, result2] = await Promise.all([
322
317
  * context.run("step 1", () => {
323
318
  * return "result1"
324
- * })
319
+ * }),
325
320
  * context.run("step 2", async () => {
326
321
  * return await fetchResults()
327
322
  * })
@@ -336,6 +331,10 @@ declare class WorkflowContext<TInitialPayload = unknown> {
336
331
  /**
337
332
  * Stops the execution for the duration provided.
338
333
  *
334
+ * ```typescript
335
+ * await context.sleep('sleep1', 3) // wait for three seconds
336
+ * ```
337
+ *
339
338
  * @param stepName
340
339
  * @param duration sleep duration in seconds
341
340
  * @returns undefined
@@ -344,6 +343,10 @@ declare class WorkflowContext<TInitialPayload = unknown> {
344
343
  /**
345
344
  * Stops the execution until the date time provided.
346
345
  *
346
+ * ```typescript
347
+ * await context.sleepUntil('sleep1', Date.now() / 1000 + 3) // wait for three seconds
348
+ * ```
349
+ *
347
350
  * @param stepName
348
351
  * @param datetime time to sleep until. Can be provided as a number (in unix seconds),
349
352
  * as a Date object or a string (passed to `new Date(datetimeString)`)
@@ -358,7 +361,7 @@ declare class WorkflowContext<TInitialPayload = unknown> {
358
361
  * const { status, body } = await context.call<string>(
359
362
  * "post call step",
360
363
  * {
361
- * url: `https://www.some-endpoint.com/api`,
364
+ * url: "https://www.some-endpoint.com/api",
362
365
  * method: "POST",
363
366
  * body: "my-payload"
364
367
  * }
@@ -389,39 +392,69 @@ declare class WorkflowContext<TInitialPayload = unknown> {
389
392
  retries?: number;
390
393
  }): Promise<CallResponse<TResult>>;
391
394
  /**
392
- * Makes the workflow run wait until a notify request is sent or until the
393
- * timeout ends
395
+ * Pauses workflow execution until a specific event occurs or a timeout is reached.
394
396
  *
395
- * ```ts
396
- * const { eventData, timeout } = await context.waitForEvent(
397
- * "wait for event step",
398
- * "my-event-id",
399
- * 100 // timeout after 100 seconds
400
- * );
401
- * ```
397
+ *```ts
398
+ * const result = await workflow.waitForEvent("payment-confirmed", {
399
+ * timeout: "5m"
400
+ * });
401
+ *```
402
402
  *
403
- * To notify a waiting workflow run, you can use the notify method:
403
+ * To notify a waiting workflow:
404
404
  *
405
405
  * ```ts
406
406
  * import { Client } from "@upstash/workflow";
407
407
  *
408
- * const client = new Client({ token: });
408
+ * const client = new Client({ token: "<QSTASH_TOKEN>" });
409
409
  *
410
410
  * await client.notify({
411
- * eventId: "my-event-id",
412
- * eventData: "eventData"
411
+ * eventId: "payment.confirmed",
412
+ * data: {
413
+ * amount: 99.99,
414
+ * currency: "USD"
415
+ * }
413
416
  * })
414
417
  * ```
415
418
  *
419
+ * Alternatively, you can use the `context.notify` method.
420
+ *
421
+ * @param stepName
422
+ * @param eventId - Unique identifier for the event to wait for
423
+ * @param options - Configuration options.
424
+ * @returns `{ timeout: boolean, eventData: unknown }`.
425
+ * The `timeout` property specifies if the workflow has timed out. The `eventData`
426
+ * is the data passed when notifying this workflow of an event.
427
+ */
428
+ waitForEvent(stepName: string, eventId: string, options?: WaitEventOptions): Promise<WaitStepResponse>;
429
+ /**
430
+ * Notify workflow runs waiting for an event
431
+ *
432
+ * ```ts
433
+ * const { eventId, eventData, notifyResponse } = await context.notify(
434
+ * "notify step", "event-id", "event-data"
435
+ * );
436
+ * ```
437
+ *
438
+ * Upon `context.notify`, the workflow runs waiting for the given eventId (context.waitForEvent)
439
+ * will receive the given event data and resume execution.
440
+ *
441
+ * The response includes the same eventId and eventData. Additionally, there is
442
+ * a notifyResponse field which contains a list of `Waiter` objects, each corresponding
443
+ * to a notified workflow run.
444
+ *
416
445
  * @param stepName
417
- * @param eventId event id to wake up the waiting workflow run
418
- * @param timeout timeout duration in seconds
419
- * @returns wait response as `{ timeout: boolean, eventData: unknown }`.
420
- * timeout is true if the wait times out, if notified it is false. eventData
421
- * is the value passed to `client.notify`.
446
+ * @param eventId event id to notify
447
+ * @param eventData event data to notify with
448
+ * @returns notify response which has event id, event data and list of waiters which were notified
422
449
  */
423
- waitForEvent(stepName: string, eventId: string, timeout: number | Duration): Promise<WaitStepResponse>;
424
450
  notify(stepName: string, eventId: string, eventData: unknown): Promise<NotifyStepResponse>;
451
+ /**
452
+ * Cancel the current workflow run
453
+ *
454
+ * Will throw WorkflowAbort to stop workflow execution.
455
+ * Shouldn't be inside try/catch.
456
+ */
457
+ cancel(): Promise<void>;
425
458
  /**
426
459
  * Adds steps to the executor. Needed so that it can be overwritten in
427
460
  * DisabledWorkflowContext.
@@ -584,7 +617,12 @@ type WorkflowServeOptions<TResponse extends Response = Response, TInitialPayload
584
617
  * @param failResponse error message
585
618
  * @returns void
586
619
  */
587
- failureFunction?: (context: Omit<WorkflowContext, "run" | "sleepUntil" | "sleep" | "call">, failStatus: number, failResponse: string, failHeader: Record<string, string[]>) => Promise<void> | void;
620
+ failureFunction?: (failureData: {
621
+ context: Omit<WorkflowContext<TInitialPayload>, "run" | "sleepUntil" | "sleep" | "call" | "waitForEvent" | "notify">;
622
+ failStatus: number;
623
+ failResponse: string;
624
+ failHeaders: Record<string, string[]>;
625
+ }) => Promise<void> | void;
588
626
  /**
589
627
  * Base Url of the workflow endpoint
590
628
  *
@@ -680,6 +718,22 @@ type CallResponse<TResult = unknown> = {
680
718
  body: TResult;
681
719
  header: Record<string, string[]>;
682
720
  };
683
- type Duration = `${bigint}s` | `${bigint}m` | `${bigint}h` | `${bigint}d`;
721
+ /**
722
+ * Valid duration string formats
723
+ * @example "30s" // 30 seconds
724
+ * @example "5m" // 5 minutes
725
+ * @example "2h" // 2 hours
726
+ * @example "1d" // 1 day
727
+ */
728
+ type Duration = `${bigint}${"s" | "m" | "h" | "d"}`;
729
+ interface WaitEventOptions {
730
+ /**
731
+ * Duration in seconds to wait for an event before timing out the workflow.
732
+ * @example 300 // 5 minutes in seconds
733
+ * @example "5m" // 5 minutes as duration string
734
+ * @default "7d"
735
+ */
736
+ timeout?: number | Duration;
737
+ }
684
738
 
685
- export { type AsyncStepFunction as A, type CallResponse as C, type Duration as D, type FinishCondition as F, type LogLevel as L, type NotifyResponse as N, type ParallelCallState as P, type RouteFunction as R, type Step as S, type WorkflowServeOptions as W, type Waiter as a, WorkflowContext as b, type WorkflowClient as c, type WorkflowReceiver as d, StepTypes as e, type StepType as f, type RawStep as g, type SyncStepFunction as h, type StepFunction as i, type FailureFunctionPayload as j, type RequiredExceptFields as k, type WaitRequest as l, type WaitStepResponse as m, type NotifyStepResponse as n, type WorkflowLoggerOptions as o, WorkflowLogger as p };
739
+ export { type AsyncStepFunction as A, type CallResponse as C, type Duration as D, type FinishCondition as F, type LogLevel as L, type NotifyResponse as N, type ParallelCallState as P, type RouteFunction as R, type Step as S, type WorkflowServeOptions as W, type Waiter as a, WorkflowContext as b, type WorkflowClient as c, type WorkflowReceiver as d, StepTypes as e, type StepType as f, type RawStep as g, type SyncStepFunction as h, type StepFunction as i, type FailureFunctionPayload as j, type RequiredExceptFields as k, type WaitRequest as l, type WaitStepResponse as m, type NotifyStepResponse as n, type WaitEventOptions as o, type WorkflowLoggerOptions as p, WorkflowLogger as q };
@@ -79,7 +79,7 @@ declare class AutoExecutor {
79
79
  *
80
80
  * If a function is already executing (this.executingStep), this
81
81
  * means that there is a nested step which is not allowed. In this
82
- * case, addStep throws QStashWorkflowError.
82
+ * case, addStep throws WorkflowError.
83
83
  *
84
84
  * @param stepInfo step plan to add
85
85
  * @returns result of the step function
@@ -263,10 +263,6 @@ declare class WorkflowContext<TInitialPayload = unknown> {
263
263
  * headers of the initial request
264
264
  */
265
265
  readonly headers: Headers;
266
- /**
267
- * initial payload as a raw string
268
- */
269
- readonly rawInitialPayload: string;
270
266
  /**
271
267
  * Map of environment variables and their values.
272
268
  *
@@ -292,7 +288,7 @@ declare class WorkflowContext<TInitialPayload = unknown> {
292
288
  * Number of retries
293
289
  */
294
290
  readonly retries: number;
295
- constructor({ qstashClient, workflowRunId, headers, steps, url, failureUrl, debug, initialPayload, rawInitialPayload, env, retries, }: {
291
+ constructor({ qstashClient, workflowRunId, headers, steps, url, failureUrl, debug, initialPayload, env, retries, }: {
296
292
  qstashClient: WorkflowClient;
297
293
  workflowRunId: string;
298
294
  headers: Headers;
@@ -301,7 +297,6 @@ declare class WorkflowContext<TInitialPayload = unknown> {
301
297
  failureUrl?: string;
302
298
  debug?: WorkflowLogger;
303
299
  initialPayload: TInitialPayload;
304
- rawInitialPayload?: string;
305
300
  env?: Record<string, string | undefined>;
306
301
  retries?: number;
307
302
  });
@@ -321,7 +316,7 @@ declare class WorkflowContext<TInitialPayload = unknown> {
321
316
  * const [result1, result2] = await Promise.all([
322
317
  * context.run("step 1", () => {
323
318
  * return "result1"
324
- * })
319
+ * }),
325
320
  * context.run("step 2", async () => {
326
321
  * return await fetchResults()
327
322
  * })
@@ -336,6 +331,10 @@ declare class WorkflowContext<TInitialPayload = unknown> {
336
331
  /**
337
332
  * Stops the execution for the duration provided.
338
333
  *
334
+ * ```typescript
335
+ * await context.sleep('sleep1', 3) // wait for three seconds
336
+ * ```
337
+ *
339
338
  * @param stepName
340
339
  * @param duration sleep duration in seconds
341
340
  * @returns undefined
@@ -344,6 +343,10 @@ declare class WorkflowContext<TInitialPayload = unknown> {
344
343
  /**
345
344
  * Stops the execution until the date time provided.
346
345
  *
346
+ * ```typescript
347
+ * await context.sleepUntil('sleep1', Date.now() / 1000 + 3) // wait for three seconds
348
+ * ```
349
+ *
347
350
  * @param stepName
348
351
  * @param datetime time to sleep until. Can be provided as a number (in unix seconds),
349
352
  * as a Date object or a string (passed to `new Date(datetimeString)`)
@@ -358,7 +361,7 @@ declare class WorkflowContext<TInitialPayload = unknown> {
358
361
  * const { status, body } = await context.call<string>(
359
362
  * "post call step",
360
363
  * {
361
- * url: `https://www.some-endpoint.com/api`,
364
+ * url: "https://www.some-endpoint.com/api",
362
365
  * method: "POST",
363
366
  * body: "my-payload"
364
367
  * }
@@ -389,39 +392,69 @@ declare class WorkflowContext<TInitialPayload = unknown> {
389
392
  retries?: number;
390
393
  }): Promise<CallResponse<TResult>>;
391
394
  /**
392
- * Makes the workflow run wait until a notify request is sent or until the
393
- * timeout ends
395
+ * Pauses workflow execution until a specific event occurs or a timeout is reached.
394
396
  *
395
- * ```ts
396
- * const { eventData, timeout } = await context.waitForEvent(
397
- * "wait for event step",
398
- * "my-event-id",
399
- * 100 // timeout after 100 seconds
400
- * );
401
- * ```
397
+ *```ts
398
+ * const result = await workflow.waitForEvent("payment-confirmed", {
399
+ * timeout: "5m"
400
+ * });
401
+ *```
402
402
  *
403
- * To notify a waiting workflow run, you can use the notify method:
403
+ * To notify a waiting workflow:
404
404
  *
405
405
  * ```ts
406
406
  * import { Client } from "@upstash/workflow";
407
407
  *
408
- * const client = new Client({ token: });
408
+ * const client = new Client({ token: "<QSTASH_TOKEN>" });
409
409
  *
410
410
  * await client.notify({
411
- * eventId: "my-event-id",
412
- * eventData: "eventData"
411
+ * eventId: "payment.confirmed",
412
+ * data: {
413
+ * amount: 99.99,
414
+ * currency: "USD"
415
+ * }
413
416
  * })
414
417
  * ```
415
418
  *
419
+ * Alternatively, you can use the `context.notify` method.
420
+ *
421
+ * @param stepName
422
+ * @param eventId - Unique identifier for the event to wait for
423
+ * @param options - Configuration options.
424
+ * @returns `{ timeout: boolean, eventData: unknown }`.
425
+ * The `timeout` property specifies if the workflow has timed out. The `eventData`
426
+ * is the data passed when notifying this workflow of an event.
427
+ */
428
+ waitForEvent(stepName: string, eventId: string, options?: WaitEventOptions): Promise<WaitStepResponse>;
429
+ /**
430
+ * Notify workflow runs waiting for an event
431
+ *
432
+ * ```ts
433
+ * const { eventId, eventData, notifyResponse } = await context.notify(
434
+ * "notify step", "event-id", "event-data"
435
+ * );
436
+ * ```
437
+ *
438
+ * Upon `context.notify`, the workflow runs waiting for the given eventId (context.waitForEvent)
439
+ * will receive the given event data and resume execution.
440
+ *
441
+ * The response includes the same eventId and eventData. Additionally, there is
442
+ * a notifyResponse field which contains a list of `Waiter` objects, each corresponding
443
+ * to a notified workflow run.
444
+ *
416
445
  * @param stepName
417
- * @param eventId event id to wake up the waiting workflow run
418
- * @param timeout timeout duration in seconds
419
- * @returns wait response as `{ timeout: boolean, eventData: unknown }`.
420
- * timeout is true if the wait times out, if notified it is false. eventData
421
- * is the value passed to `client.notify`.
446
+ * @param eventId event id to notify
447
+ * @param eventData event data to notify with
448
+ * @returns notify response which has event id, event data and list of waiters which were notified
422
449
  */
423
- waitForEvent(stepName: string, eventId: string, timeout: number | Duration): Promise<WaitStepResponse>;
424
450
  notify(stepName: string, eventId: string, eventData: unknown): Promise<NotifyStepResponse>;
451
+ /**
452
+ * Cancel the current workflow run
453
+ *
454
+ * Will throw WorkflowAbort to stop workflow execution.
455
+ * Shouldn't be inside try/catch.
456
+ */
457
+ cancel(): Promise<void>;
425
458
  /**
426
459
  * Adds steps to the executor. Needed so that it can be overwritten in
427
460
  * DisabledWorkflowContext.
@@ -584,7 +617,12 @@ type WorkflowServeOptions<TResponse extends Response = Response, TInitialPayload
584
617
  * @param failResponse error message
585
618
  * @returns void
586
619
  */
587
- failureFunction?: (context: Omit<WorkflowContext, "run" | "sleepUntil" | "sleep" | "call">, failStatus: number, failResponse: string, failHeader: Record<string, string[]>) => Promise<void> | void;
620
+ failureFunction?: (failureData: {
621
+ context: Omit<WorkflowContext<TInitialPayload>, "run" | "sleepUntil" | "sleep" | "call" | "waitForEvent" | "notify">;
622
+ failStatus: number;
623
+ failResponse: string;
624
+ failHeaders: Record<string, string[]>;
625
+ }) => Promise<void> | void;
588
626
  /**
589
627
  * Base Url of the workflow endpoint
590
628
  *
@@ -680,6 +718,22 @@ type CallResponse<TResult = unknown> = {
680
718
  body: TResult;
681
719
  header: Record<string, string[]>;
682
720
  };
683
- type Duration = `${bigint}s` | `${bigint}m` | `${bigint}h` | `${bigint}d`;
721
+ /**
722
+ * Valid duration string formats
723
+ * @example "30s" // 30 seconds
724
+ * @example "5m" // 5 minutes
725
+ * @example "2h" // 2 hours
726
+ * @example "1d" // 1 day
727
+ */
728
+ type Duration = `${bigint}${"s" | "m" | "h" | "d"}`;
729
+ interface WaitEventOptions {
730
+ /**
731
+ * Duration in seconds to wait for an event before timing out the workflow.
732
+ * @example 300 // 5 minutes in seconds
733
+ * @example "5m" // 5 minutes as duration string
734
+ * @default "7d"
735
+ */
736
+ timeout?: number | Duration;
737
+ }
684
738
 
685
- export { type AsyncStepFunction as A, type CallResponse as C, type Duration as D, type FinishCondition as F, type LogLevel as L, type NotifyResponse as N, type ParallelCallState as P, type RouteFunction as R, type Step as S, type WorkflowServeOptions as W, type Waiter as a, WorkflowContext as b, type WorkflowClient as c, type WorkflowReceiver as d, StepTypes as e, type StepType as f, type RawStep as g, type SyncStepFunction as h, type StepFunction as i, type FailureFunctionPayload as j, type RequiredExceptFields as k, type WaitRequest as l, type WaitStepResponse as m, type NotifyStepResponse as n, type WorkflowLoggerOptions as o, WorkflowLogger as p };
739
+ export { type AsyncStepFunction as A, type CallResponse as C, type Duration as D, type FinishCondition as F, type LogLevel as L, type NotifyResponse as N, type ParallelCallState as P, type RouteFunction as R, type Step as S, type WorkflowServeOptions as W, type Waiter as a, WorkflowContext as b, type WorkflowClient as c, type WorkflowReceiver as d, StepTypes as e, type StepType as f, type RawStep as g, type SyncStepFunction as h, type StepFunction as i, type FailureFunctionPayload as j, type RequiredExceptFields as k, type WaitRequest as l, type WaitStepResponse as m, type NotifyStepResponse as n, type WaitEventOptions as o, type WorkflowLoggerOptions as p, WorkflowLogger as q };