@upstash/workflow 1.4.0-rc → 1.5.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/README.md +9 -0
- package/astro.d.mts +18 -2
- package/astro.d.ts +18 -2
- package/astro.js +129 -29
- package/astro.mjs +1 -1
- package/{chunk-UH6O5IJY.mjs → chunk-J3PXPHSB.mjs} +129 -29
- package/cloudflare.d.mts +16 -2
- package/cloudflare.d.ts +16 -2
- package/cloudflare.js +129 -29
- package/cloudflare.mjs +1 -1
- package/docs/agents/examples.mdx +8 -0
- package/docs/agents/features.mdx +374 -0
- package/docs/agents/getting-started.mdx +139 -0
- package/docs/agents/overview.mdx +40 -0
- package/docs/agents/patterns/evaluator-optimizer.mdx +72 -0
- package/docs/agents/patterns/orchestrator-workers.mdx +95 -0
- package/docs/agents/patterns/parallelization.mdx +77 -0
- package/docs/agents/patterns/prompt-chaining.mdx +84 -0
- package/docs/basics/best-practices/at-least-one-step.mdx +57 -0
- package/docs/basics/best-practices/business-logic-in-steps.mdx +52 -0
- package/docs/basics/best-practices/deterministic-code.mdx +132 -0
- package/docs/basics/best-practices/idempotency.mdx +38 -0
- package/docs/basics/best-practices/nested-context-methods.mdx +38 -0
- package/docs/basics/best-practices/overview.mdx +35 -0
- package/docs/basics/best-practices/promise-any.mdx +9 -0
- package/docs/basics/best-practices/return-step-results.mdx +80 -0
- package/docs/basics/best-practices/workflow-limits.mdx +62 -0
- package/docs/basics/client/cancel.mdx +137 -0
- package/docs/basics/client/dlq/callback.mdx +21 -0
- package/docs/basics/client/dlq/delete.mdx +120 -0
- package/docs/basics/client/dlq/list.mdx +96 -0
- package/docs/basics/client/dlq/restart.mdx +164 -0
- package/docs/basics/client/dlq/resume.mdx +167 -0
- package/docs/basics/client/logs.mdx +139 -0
- package/docs/basics/client/notify.mdx +79 -0
- package/docs/basics/client/trigger.mdx +139 -0
- package/docs/basics/client/waiters.mdx +31 -0
- package/docs/basics/client.mdx +40 -0
- package/docs/basics/how.mdx +65 -0
- package/docs/basics/serve/advanced.mdx +435 -0
- package/docs/basics/serve.mdx +142 -0
- package/docs/changelog.mdx +174 -0
- package/docs/examples/allInOne.mdx +377 -0
- package/docs/examples/authWebhook.mdx +567 -0
- package/docs/examples/customRetry.mdx +309 -0
- package/docs/examples/customerOnboarding.mdx +230 -0
- package/docs/examples/dynamicWorkflow.mdx +203 -0
- package/docs/examples/eCommerceOrderFulfillment.mdx +281 -0
- package/docs/examples/imageProcessing.mdx +339 -0
- package/docs/examples/paymentRetry.mdx +438 -0
- package/docs/examples/waitForEvent.mdx +201 -0
- package/docs/features/dlq/callback.mdx +42 -0
- package/docs/features/dlq/delete.mdx +29 -0
- package/docs/features/dlq/restart.mdx +33 -0
- package/docs/features/dlq/resume.mdx +37 -0
- package/docs/features/dlq.mdx +39 -0
- package/docs/features/failure-callback.mdx +102 -0
- package/docs/features/failureFunction/advanced.mdx +36 -0
- package/docs/features/failureFunction/reliability.mdx +33 -0
- package/docs/features/flow-control/monitor.mdx +22 -0
- package/docs/features/flow-control/parallelism.mdx +45 -0
- package/docs/features/flow-control/rate-period.mdx +46 -0
- package/docs/features/flow-control.mdx +164 -0
- package/docs/features/invoke/serveMany.mdx +109 -0
- package/docs/features/invoke.mdx +39 -0
- package/docs/features/notify.mdx +127 -0
- package/docs/features/parallel-steps.mdx +41 -0
- package/docs/features/retries/prevent-retries.mdx +140 -0
- package/docs/features/retries.mdx +93 -0
- package/docs/features/wait-for-event.mdx +104 -0
- package/docs/features/wait.mdx +44 -0
- package/docs/features/webhooks.mdx +116 -0
- package/docs/getstarted.mdx +200 -0
- package/docs/howto/cancel.mdx +50 -0
- package/docs/howto/changes.mdx +33 -0
- package/docs/howto/configure.mdx +42 -0
- package/docs/howto/failures.mdx +185 -0
- package/docs/howto/get-token.mdx +18 -0
- package/docs/howto/local-development/development-server.mdx +130 -0
- package/docs/howto/local-development/local-tunnel.mdx +73 -0
- package/docs/howto/middlewares.mdx +263 -0
- package/docs/howto/migrations.mdx +335 -0
- package/docs/howto/multi-region.mdx +107 -0
- package/docs/howto/parallel-runs.mdx +52 -0
- package/docs/howto/readonly-token.mdx +19 -0
- package/docs/howto/realtime/basic.mdx +315 -0
- package/docs/howto/realtime/human-in-the-loop.mdx +454 -0
- package/docs/howto/redact-fields.mdx +61 -0
- package/docs/howto/retry-now.mdx +41 -0
- package/docs/howto/schedule.mdx +104 -0
- package/docs/howto/security.mdx +143 -0
- package/docs/howto/singleton.mdx +137 -0
- package/docs/howto/start.mdx +69 -0
- package/docs/howto/use-webhooks.mdx +256 -0
- package/docs/integrations/aisdk.mdx +316 -0
- package/docs/integrations/anthropic.mdx +51 -0
- package/docs/integrations/datadog.mdx +69 -0
- package/docs/integrations/openai.mdx +87 -0
- package/docs/integrations/prometheus.mdx +65 -0
- package/docs/integrations/resend.mdx +82 -0
- package/docs/llms-txt.mdx +4 -0
- package/docs/openapi.yaml +2944 -0
- package/docs/pricing.mdx +22 -0
- package/docs/quickstarts/astro.mdx +185 -0
- package/docs/quickstarts/cloudflare-workers.mdx +190 -0
- package/docs/quickstarts/express.mdx +190 -0
- package/docs/quickstarts/fastapi.mdx +282 -0
- package/docs/quickstarts/flask.mdx +282 -0
- package/docs/quickstarts/hono.mdx +221 -0
- package/docs/quickstarts/nextjs-fastapi.mdx +147 -0
- package/docs/quickstarts/nextjs-flask.mdx +147 -0
- package/docs/quickstarts/nuxt.mdx +169 -0
- package/docs/quickstarts/platforms.mdx +78 -0
- package/docs/quickstarts/solidjs.mdx +161 -0
- package/docs/quickstarts/svelte.mdx +170 -0
- package/docs/quickstarts/tanstack-start.mdx +205 -0
- package/docs/quickstarts/vercel-nextjs.mdx +233 -0
- package/docs/roadmap.mdx +8 -0
- package/docs/sdk/workflow-js.mdx +4 -0
- package/docs/sdk/workflow-py.mdx +4 -0
- package/docs/steps/api.mdx +64 -0
- package/docs/steps/call.mdx +183 -0
- package/docs/steps/cancel.mdx +25 -0
- package/docs/steps/createWebhook.mdx +40 -0
- package/docs/steps/invoke.mdx +100 -0
- package/docs/steps/notify.mdx +105 -0
- package/docs/steps/run.mdx +150 -0
- package/docs/steps/sleep.mdx +97 -0
- package/docs/steps/sleepUntil.mdx +88 -0
- package/docs/steps/waitForEvent.mdx +60 -0
- package/docs/steps/waitForWebhook.mdx +78 -0
- package/docs/steps.mdx +108 -0
- package/docs/troubleshooting/general.mdx +331 -0
- package/docs/troubleshooting/vercel.mdx +82 -0
- package/express.d.mts +18 -2
- package/express.d.ts +18 -2
- package/express.js +129 -29
- package/express.mjs +1 -1
- package/h3.d.mts +18 -2
- package/h3.d.ts +18 -2
- package/h3.js +129 -29
- package/h3.mjs +1 -1
- package/hono.d.mts +16 -2
- package/hono.d.ts +16 -2
- package/hono.js +129 -29
- package/hono.mjs +1 -1
- package/index.d.mts +49 -21
- package/index.d.ts +49 -21
- package/index.js +145 -41
- package/index.mjs +17 -13
- package/nextjs.d.mts +39 -2
- package/nextjs.d.ts +39 -2
- package/nextjs.js +129 -29
- package/nextjs.mjs +1 -1
- package/package.json +1 -1
- package/react-router.d.mts +15 -2
- package/react-router.d.ts +15 -2
- package/react-router.js +129 -29
- package/react-router.mjs +1 -1
- package/{serve-many-CbIV7145.d.mts → serve-many-BKukvLNl.d.mts} +1 -1
- package/{serve-many-BCtZg31b.d.ts → serve-many-CrLKQpuZ.d.ts} +1 -1
- package/solidjs.d.mts +9 -1
- package/solidjs.d.ts +9 -1
- package/solidjs.js +129 -29
- package/solidjs.mjs +1 -1
- package/src/platforms/astro.ts +84 -0
- package/src/platforms/cloudflare.ts +127 -0
- package/src/platforms/express.ts +147 -0
- package/src/platforms/h3.ts +105 -0
- package/src/platforms/hono.ts +103 -0
- package/src/platforms/nextjs.ts +193 -0
- package/src/platforms/react-router.ts +92 -0
- package/src/platforms/solidjs.ts +59 -0
- package/src/platforms/svelte.ts +99 -0
- package/src/platforms/tanstack.ts +96 -0
- package/src/src/client/dlq.ts +387 -0
- package/src/src/client/filter-types.ts +166 -0
- package/src/src/client/index.ts +350 -0
- package/src/src/client/types.ts +501 -0
- package/src/src/client/utils.ts +307 -0
- package/src/src/constants.ts +82 -0
- package/src/src/context/api/anthropic.ts +58 -0
- package/src/src/context/api/base.ts +52 -0
- package/src/src/context/api/index.ts +24 -0
- package/src/src/context/api/openai.ts +125 -0
- package/src/src/context/api/resend.ts +57 -0
- package/src/src/context/auto-executor.ts +707 -0
- package/src/src/context/context.ts +681 -0
- package/src/src/context/index.ts +1 -0
- package/src/src/context/provider.ts +37 -0
- package/src/src/context/steps.ts +965 -0
- package/src/src/error.ts +203 -0
- package/src/src/index.ts +13 -0
- package/src/src/middleware/default-callbacks.ts +20 -0
- package/src/src/middleware/index.ts +2 -0
- package/src/src/middleware/logging.ts +39 -0
- package/src/src/middleware/manager.ts +148 -0
- package/src/src/middleware/middleware.ts +46 -0
- package/src/src/middleware/types.ts +72 -0
- package/src/src/qstash/headers.ts +397 -0
- package/src/src/qstash/step-config.ts +268 -0
- package/src/src/qstash/submit-steps.ts +284 -0
- package/src/src/serve/authorization.ts +118 -0
- package/src/src/serve/index.ts +475 -0
- package/src/src/serve/multi-region/handlers.ts +225 -0
- package/src/src/serve/multi-region/utils.ts +88 -0
- package/src/src/serve/options.ts +241 -0
- package/src/src/serve/serve-many.ts +117 -0
- package/src/src/serve/utils.ts +11 -0
- package/src/src/types.ts +590 -0
- package/src/src/utils.ts +141 -0
- package/src/src/workflow-parser.ts +472 -0
- package/src/src/workflow-requests.ts +575 -0
- package/svelte.d.mts +16 -2
- package/svelte.d.ts +16 -2
- package/svelte.js +129 -29
- package/svelte.mjs +1 -1
- package/tanstack.d.mts +14 -2
- package/tanstack.d.ts +14 -2
- package/tanstack.js +129 -29
- package/tanstack.mjs +1 -1
- package/{types-ffLPVG5_.d.ts → types-CrgK5P6p.d.mts} +124 -24
- package/{types-ffLPVG5_.d.mts → types-CrgK5P6p.d.ts} +124 -24
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
// ── Filter Utility Types ──────────────────────────────────────
|
|
2
|
+
|
|
3
|
+
import type { FailureFunctionState, WorkflowRunLog } from "./types";
|
|
4
|
+
|
|
5
|
+
// Intersecting with T keeps every field visible to autocomplete/inference;
|
|
6
|
+
// the single-property union alone only enforces the "at least one" constraint.
|
|
7
|
+
type RequireAtLeastOne<T> = T & { [K in keyof T]-?: Required<Pick<T, K>> }[keyof T];
|
|
8
|
+
|
|
9
|
+
// ── Filter Field Groups ───────────────────────────────────────
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Shared filter fields accepted by every qstash & workflow endpoint.
|
|
13
|
+
*
|
|
14
|
+
* Most fields support multi-value filtering: pass an array to match a record
|
|
15
|
+
* whose value equals any of the given values (OR semantics). Separate filters
|
|
16
|
+
* are combined with AND semantics.
|
|
17
|
+
*/
|
|
18
|
+
type UniversalFilterFields = {
|
|
19
|
+
fromDate?: Date | number;
|
|
20
|
+
toDate?: Date | number;
|
|
21
|
+
/** Filter by the IP address of the publisher. Supports multiple values. */
|
|
22
|
+
callerIp?: string | string[];
|
|
23
|
+
/**
|
|
24
|
+
* Filter by label.
|
|
25
|
+
*
|
|
26
|
+
* Pass an array to match runs that have any of the given labels (OR semantics).
|
|
27
|
+
* For example, with runs labelled `[label_1, label_2]` and `[label_2, label_3]`,
|
|
28
|
+
* filtering by `[label_1, label_2]` returns both.
|
|
29
|
+
*/
|
|
30
|
+
label?: string | string[];
|
|
31
|
+
/** Filter by Flow Control Key. Supports multiple values. */
|
|
32
|
+
flowControlKey?: string | string[];
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/** Workflow-specific filter fields for DLQ and bulk endpoints. */
|
|
36
|
+
type WorkflowFilterFields = {
|
|
37
|
+
/** Filter by workflow URL. Supports multiple values. */
|
|
38
|
+
workflowUrl?: string | string[];
|
|
39
|
+
workflowRunId?: string;
|
|
40
|
+
workflowCreatedAt?: number;
|
|
41
|
+
/**
|
|
42
|
+
* Filter by the state of the failure function call. Supports multiple values:
|
|
43
|
+
* pass an array to match runs in any of the given states (OR semantics).
|
|
44
|
+
*/
|
|
45
|
+
failureFunctionState?: FailureFunctionState | FailureFunctionState[];
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
type WorkflowLogsFilterFields = {
|
|
49
|
+
state?: WorkflowRunLog["workflowState"];
|
|
50
|
+
messageId?: string;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Filter by the host/path of the workflow URL.
|
|
55
|
+
*
|
|
56
|
+
* Supported by the cancel and logs endpoints (the DLQ endpoint rejects these).
|
|
57
|
+
* Each supports multiple values: pass an array to match any value.
|
|
58
|
+
*/
|
|
59
|
+
type HostPathFilterFields = {
|
|
60
|
+
/** Filter by the host of the workflow URL. Supports multiple values. */
|
|
61
|
+
host?: string | string[];
|
|
62
|
+
/** Filter by the path of the workflow URL. Supports multiple values. */
|
|
63
|
+
path?: string | string[];
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
// ── Composed Filter Field Types ───────────────────────────────
|
|
67
|
+
|
|
68
|
+
type DLQActionFilterFields = UniversalFilterFields & WorkflowFilterFields;
|
|
69
|
+
|
|
70
|
+
/** Cancel filter: exact URL match. Cannot combine with `workflowUrlStartingWith`. */
|
|
71
|
+
type CancelFilterWithExactUrl = UniversalFilterFields &
|
|
72
|
+
HostPathFilterFields & {
|
|
73
|
+
/** Filter by exact workflow URL. Supports multiple values. */
|
|
74
|
+
workflowUrl: string | string[];
|
|
75
|
+
workflowUrlStartingWith?: never;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/** Cancel filter: URL prefix match. Cannot combine with `workflowUrl`. */
|
|
79
|
+
type CancelFilterWithPrefixUrl = UniversalFilterFields &
|
|
80
|
+
HostPathFilterFields & {
|
|
81
|
+
/** Filter by workflow URL prefix. Supports multiple values. */
|
|
82
|
+
workflowUrlStartingWith: string | string[];
|
|
83
|
+
workflowUrl?: never;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/** Cancel filter: no URL. Requires at least one other filter field. */
|
|
87
|
+
type CancelFilterWithoutUrl = RequireAtLeastOne<UniversalFilterFields & HostPathFilterFields> & {
|
|
88
|
+
workflowUrl?: never;
|
|
89
|
+
workflowUrlStartingWith?: never;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
type CancelFilter = CancelFilterWithExactUrl | CancelFilterWithPrefixUrl | CancelFilterWithoutUrl;
|
|
93
|
+
|
|
94
|
+
// ── Composed Endpoint Filter Types ────────────────────────────
|
|
95
|
+
|
|
96
|
+
type WorkflowDLQBulkCount = {
|
|
97
|
+
cursor?: string;
|
|
98
|
+
/**
|
|
99
|
+
* Maximum number of messages to process per call.
|
|
100
|
+
*
|
|
101
|
+
* @default 100
|
|
102
|
+
*/
|
|
103
|
+
count?: number;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* DLQ bulk actions (resume, restart, delete) support three modes:
|
|
108
|
+
* - By dlqIds (no cursor)
|
|
109
|
+
* - By filter fields (with optional cursor and count)
|
|
110
|
+
* - All (with optional cursor and count)
|
|
111
|
+
*/
|
|
112
|
+
export type WorkflowDLQActionFilters =
|
|
113
|
+
| { dlqIds: string | string[]; filter?: never; all?: never; count?: never; cursor?: never }
|
|
114
|
+
| ({
|
|
115
|
+
filter: RequireAtLeastOne<DLQActionFilterFields>;
|
|
116
|
+
dlqIds?: never;
|
|
117
|
+
all?: never;
|
|
118
|
+
} & WorkflowDLQBulkCount)
|
|
119
|
+
| ({
|
|
120
|
+
all: true;
|
|
121
|
+
dlqIds?: never;
|
|
122
|
+
filter?: never;
|
|
123
|
+
} & WorkflowDLQBulkCount);
|
|
124
|
+
|
|
125
|
+
export type WorkflowDLQListFilters = UniversalFilterFields &
|
|
126
|
+
WorkflowFilterFields & {
|
|
127
|
+
/** @deprecated Use `workflowUrl` instead. */
|
|
128
|
+
url?: string;
|
|
129
|
+
/** @deprecated No longer supported in the new API. */
|
|
130
|
+
responseStatus?: number;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
type WorkflowCancelCount = {
|
|
134
|
+
/**
|
|
135
|
+
* Maximum number of workflow runs to cancel per call.
|
|
136
|
+
*
|
|
137
|
+
* @default 100
|
|
138
|
+
*/
|
|
139
|
+
count?: number;
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* We don't accept a single workflowRunId or workflowCreatedAt because there
|
|
144
|
+
* could only be one running workflow with a single wfrid at the same time.
|
|
145
|
+
* So using these do not make sense.
|
|
146
|
+
*
|
|
147
|
+
* Also failureFunctionState is not available.
|
|
148
|
+
* Cancel does not support cursor.
|
|
149
|
+
*/
|
|
150
|
+
export type WorkflowRunCancelFilters =
|
|
151
|
+
| { workflowRunIds: string[]; filter?: never; all?: never; count?: never }
|
|
152
|
+
| ({
|
|
153
|
+
filter: CancelFilter;
|
|
154
|
+
workflowRunIds?: never;
|
|
155
|
+
all?: never;
|
|
156
|
+
} & WorkflowCancelCount)
|
|
157
|
+
| ({
|
|
158
|
+
all: true;
|
|
159
|
+
workflowRunIds?: never;
|
|
160
|
+
filter?: never;
|
|
161
|
+
} & WorkflowCancelCount);
|
|
162
|
+
|
|
163
|
+
export type WorkflowLogsListFilters = UniversalFilterFields &
|
|
164
|
+
Pick<WorkflowFilterFields, "workflowUrl" | "workflowRunId" | "workflowCreatedAt"> &
|
|
165
|
+
HostPathFilterFields &
|
|
166
|
+
WorkflowLogsFilterFields;
|
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
import { NotifyResponse, Waiter } from "../types";
|
|
2
|
+
import { Client as QStashClient } from "@upstash/qstash";
|
|
3
|
+
import {
|
|
4
|
+
buildBulkActionQueryParameters,
|
|
5
|
+
makeGetWaitersRequest,
|
|
6
|
+
makeNotifyRequest,
|
|
7
|
+
toNonEmptyIdArray,
|
|
8
|
+
} from "./utils";
|
|
9
|
+
import { getWorkflowRunId, serializeLabel, validateFlowControl, validateLabel } from "../utils";
|
|
10
|
+
import { triggerFirstInvocation } from "../workflow-requests";
|
|
11
|
+
import { WorkflowContext } from "../context";
|
|
12
|
+
import { DLQ } from "./dlq";
|
|
13
|
+
import { TriggerOptions, WorkflowRunLogs } from "./types";
|
|
14
|
+
import { SDK_TELEMETRY, WORKFLOW_LABEL_HEADER } from "../constants";
|
|
15
|
+
import { WorkflowLogsListFilters, WorkflowRunCancelFilters } from "./filter-types";
|
|
16
|
+
|
|
17
|
+
type ClientConfig = ConstructorParameters<typeof QStashClient>[0];
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Workflow client for canceling & notifying workflows and getting waiters of an
|
|
21
|
+
* event.
|
|
22
|
+
*
|
|
23
|
+
* ```ts
|
|
24
|
+
* import { Client } from "@upstash/workflow";
|
|
25
|
+
* const client = new Client({ token: "<QSTASH_TOKEN>" })
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @see node_modules/@upstash/workflow/docs/basics/client.mdx
|
|
29
|
+
*/
|
|
30
|
+
export class Client {
|
|
31
|
+
private client: QStashClient;
|
|
32
|
+
|
|
33
|
+
constructor(clientConfig: ClientConfig) {
|
|
34
|
+
this.client = new QStashClient(clientConfig);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Cancel an ongoing workflow.
|
|
39
|
+
*
|
|
40
|
+
* Can be called with:
|
|
41
|
+
* - A single workflow run id: `cancel("wfr_123")`
|
|
42
|
+
* - An array of workflow run ids: `cancel(["wfr_123", "wfr_456"])`
|
|
43
|
+
* - By exact URL: `cancel({ filter: { workflowUrl: "https://..." } })`
|
|
44
|
+
* - By URL prefix: `cancel({ filter: { workflowUrlStartingWith: "https://..." } })`
|
|
45
|
+
* - With other filters: `cancel({ filter: { label: "my-label" } })`
|
|
46
|
+
* - To target all: `cancel({ all: true })`
|
|
47
|
+
*
|
|
48
|
+
* Filters support multiple values: pass an array to match a run whose value
|
|
49
|
+
* equals any of the given values (OR logic). Separate filters are combined with
|
|
50
|
+
* AND logic. For example:
|
|
51
|
+
* `cancel({ filter: { workflowUrl: ["https://a.com", "https://b.com"] } })`
|
|
52
|
+
*
|
|
53
|
+
* Cancels up to `count` workflow runs per call (defaults to 100).
|
|
54
|
+
*
|
|
55
|
+
* ```ts
|
|
56
|
+
* const result = await client.cancel({ all: true, count: 50 });
|
|
57
|
+
* ```
|
|
58
|
+
*
|
|
59
|
+
* @see node_modules/@upstash/workflow/docs/basics/client/cancel.mdx
|
|
60
|
+
*/
|
|
61
|
+
public async cancel(
|
|
62
|
+
request: string | string[] | WorkflowRunCancelFilters
|
|
63
|
+
): Promise<{ cancelled: number }>;
|
|
64
|
+
/** @deprecated Use `cancel(id)`, `cancel([id1, id2])`, `cancel({ filter: { workflowUrlStartingWith } })` instead. */
|
|
65
|
+
public async cancel(request: {
|
|
66
|
+
ids?: string | string[];
|
|
67
|
+
urlStartingWith?: string;
|
|
68
|
+
all?: true;
|
|
69
|
+
}): Promise<{ cancelled: number }>;
|
|
70
|
+
public async cancel(
|
|
71
|
+
request:
|
|
72
|
+
| string
|
|
73
|
+
| string[]
|
|
74
|
+
| WorkflowRunCancelFilters
|
|
75
|
+
| { ids?: string | string[]; urlStartingWith?: string; all?: true }
|
|
76
|
+
): Promise<{ cancelled: number }> {
|
|
77
|
+
// Legacy format: { ids?, urlStartingWith?, all? }
|
|
78
|
+
if (
|
|
79
|
+
typeof request === "object" &&
|
|
80
|
+
!Array.isArray(request) &&
|
|
81
|
+
("ids" in request || "urlStartingWith" in request)
|
|
82
|
+
) {
|
|
83
|
+
const legacy = request as { ids?: string | string[]; urlStartingWith?: string; all?: true };
|
|
84
|
+
if (legacy.ids) {
|
|
85
|
+
const ids = typeof legacy.ids === "string" ? [legacy.ids] : legacy.ids;
|
|
86
|
+
return this.cancel(ids);
|
|
87
|
+
}
|
|
88
|
+
if (legacy.urlStartingWith) {
|
|
89
|
+
return this.cancel({ filter: { workflowUrlStartingWith: legacy.urlStartingWith } });
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (typeof request === "string" || Array.isArray(request)) {
|
|
94
|
+
const ids = toNonEmptyIdArray(request, "Workflow run id");
|
|
95
|
+
if (ids.length === 0) return { cancelled: 0 };
|
|
96
|
+
request = ids;
|
|
97
|
+
}
|
|
98
|
+
const filters: WorkflowRunCancelFilters = Array.isArray(request)
|
|
99
|
+
? { workflowRunIds: request }
|
|
100
|
+
: (request as WorkflowRunCancelFilters);
|
|
101
|
+
|
|
102
|
+
return await this.client.http.request<{ cancelled: number }>({
|
|
103
|
+
path: ["v2", "workflows", "runs"],
|
|
104
|
+
method: "DELETE",
|
|
105
|
+
query: buildBulkActionQueryParameters(filters, { translateWorkflowUrl: true }),
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Notify a workflow run waiting for an event
|
|
111
|
+
*
|
|
112
|
+
* ```ts
|
|
113
|
+
* import { Client } from "@upstash/workflow";
|
|
114
|
+
*
|
|
115
|
+
* const client = new Client({ token: "<QSTASH_TOKEN>" })
|
|
116
|
+
* await client.notify({
|
|
117
|
+
* eventId: "my-event-id",
|
|
118
|
+
* eventData: "my-data" // data passed to the workflow run
|
|
119
|
+
* });
|
|
120
|
+
* ```
|
|
121
|
+
*
|
|
122
|
+
* @param eventId event id to notify. Only letters, digits, "-", "_" and "." are allowed;
|
|
123
|
+
* QStash rejects other characters, such as ":".
|
|
124
|
+
* @param eventData data to provide to the workflow
|
|
125
|
+
* @param workflowRunId optional workflow run id for lookback: the event is kept for the run
|
|
126
|
+
* even if it has not reached `waitForEvent` yet
|
|
127
|
+
* @returns the notified waiters. With `workflowRunId`, QStash returns a single object
|
|
128
|
+
* instead of an array, despite the type.
|
|
129
|
+
* @see node_modules/@upstash/workflow/docs/basics/client/notify.mdx
|
|
130
|
+
*/
|
|
131
|
+
public async notify({
|
|
132
|
+
eventId,
|
|
133
|
+
eventData,
|
|
134
|
+
workflowRunId,
|
|
135
|
+
}: {
|
|
136
|
+
eventId: string;
|
|
137
|
+
eventData?: unknown;
|
|
138
|
+
workflowRunId?: string;
|
|
139
|
+
}): Promise<NotifyResponse[]> {
|
|
140
|
+
return await makeNotifyRequest(this.client.http, eventId, eventData, workflowRunId);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Check waiters of an event
|
|
145
|
+
*
|
|
146
|
+
* ```ts
|
|
147
|
+
* import { Client } from "@upstash/workflow";
|
|
148
|
+
*
|
|
149
|
+
* const client = new Client({ token: "<QSTASH_TOKEN>" })
|
|
150
|
+
* const result = await client.getWaiters({
|
|
151
|
+
* eventId: "my-event-id"
|
|
152
|
+
* })
|
|
153
|
+
* ```
|
|
154
|
+
*
|
|
155
|
+
* @param eventId event id to check
|
|
156
|
+
* @see node_modules/@upstash/workflow/docs/basics/client/waiters.mdx
|
|
157
|
+
*/
|
|
158
|
+
public async getWaiters({ eventId }: { eventId: string }): Promise<Required<Waiter>[]> {
|
|
159
|
+
return await makeGetWaitersRequest(this.client.http, eventId);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Trigger new workflow run and returns the workflow run id or an array of workflow run ids
|
|
164
|
+
*
|
|
165
|
+
* trigger a single workflow run:
|
|
166
|
+
* ```ts
|
|
167
|
+
* const { workflowRunId } = await client.trigger({
|
|
168
|
+
* url: "https://workflow-endpoint.com",
|
|
169
|
+
* body: "hello there!", // Optional body
|
|
170
|
+
* headers: { ... }, // Optional headers
|
|
171
|
+
* workflowRunId: "my-workflow", // Optional workflow run ID
|
|
172
|
+
* retries: 3, // Optional retries for each step
|
|
173
|
+
* retryDelay: "1000" // Optional retry delay for the delay between retries
|
|
174
|
+
* });
|
|
175
|
+
*
|
|
176
|
+
* console.log(workflowRunId)
|
|
177
|
+
* // wfr_my-workflow
|
|
178
|
+
* ```
|
|
179
|
+
* trigger multiple workflow runs:
|
|
180
|
+
* ```ts
|
|
181
|
+
* const result = await client.trigger([
|
|
182
|
+
* {
|
|
183
|
+
* url: "https://workflow-endpoint.com",
|
|
184
|
+
* body: "hello there!", // Optional body
|
|
185
|
+
* headers: { ... }, // Optional headers
|
|
186
|
+
* workflowRunId: "my-workflow", // Optional workflow run ID
|
|
187
|
+
* retries: 3, // Optional retries for each step
|
|
188
|
+
* retryDelay: "1000" // Optional retry delay for the delay between retries
|
|
189
|
+
* },
|
|
190
|
+
* {
|
|
191
|
+
* url: "https://workflow-endpoint-2.com",
|
|
192
|
+
* body: "hello world!", // Optional body
|
|
193
|
+
* headers: { ... }, // Optional headers
|
|
194
|
+
* workflowRunId: "my-workflow-2", // Optional workflow run ID
|
|
195
|
+
* retries: 5, // Optional retries for each step
|
|
196
|
+
* retryDelay: "1000" // Optional retry delay for the delay between retries
|
|
197
|
+
* },
|
|
198
|
+
* ]);
|
|
199
|
+
*
|
|
200
|
+
* console.log(result)
|
|
201
|
+
* // [
|
|
202
|
+
* // { workflowRunId: "wfr_my-workflow" },
|
|
203
|
+
* // { workflowRunId: "wfr_my-workflow-2" },
|
|
204
|
+
* // ]
|
|
205
|
+
* ```
|
|
206
|
+
*
|
|
207
|
+
* @param url URL of the workflow
|
|
208
|
+
* @param body body to start the workflow with
|
|
209
|
+
* @param headers headers to use in the request
|
|
210
|
+
* @param workflowRunId optional workflow run id to use. mind that
|
|
211
|
+
* you should pass different workflow run ids for different runs.
|
|
212
|
+
* The final workflowRunId will be `wfr_${workflowRunId}`, in
|
|
213
|
+
* other words: the workflow run id you pass will be prefixed
|
|
214
|
+
* with `wfr_` (twice if it already starts with `wfr_`). Use the
|
|
215
|
+
* returned workflowRunId for notify, cancel and logs.
|
|
216
|
+
* @param retries number of times each failed step of the run is
|
|
217
|
+
* retried. 3 by default.
|
|
218
|
+
* @param retryDelay delay between retries.
|
|
219
|
+
* @param flowControl Settings for controlling the number of active requests
|
|
220
|
+
* and number of requests per second with the same key.
|
|
221
|
+
* @param delay Delay for the workflow run. This is used to delay the
|
|
222
|
+
* execution of the workflow run. The delay is in seconds or can be passed
|
|
223
|
+
* as a string with a time unit (e.g. "1h", "30m", "15s").
|
|
224
|
+
* @returns workflow run id or an array of workflow run ids
|
|
225
|
+
* @see node_modules/@upstash/workflow/docs/basics/client/trigger.mdx
|
|
226
|
+
*/
|
|
227
|
+
|
|
228
|
+
public async trigger(params: TriggerOptions): Promise<{ workflowRunId: string }>;
|
|
229
|
+
public async trigger(params: TriggerOptions[]): Promise<{ workflowRunId: string }[]>;
|
|
230
|
+
|
|
231
|
+
public async trigger(
|
|
232
|
+
params: TriggerOptions | TriggerOptions[]
|
|
233
|
+
): Promise<{ workflowRunId: string } | { workflowRunId: string }[]> {
|
|
234
|
+
const isBatchInput = Array.isArray(params);
|
|
235
|
+
const options = isBatchInput ? params : [params];
|
|
236
|
+
|
|
237
|
+
const invocations = options.map((option) => {
|
|
238
|
+
validateLabel(option.label);
|
|
239
|
+
validateFlowControl(option.flowControl);
|
|
240
|
+
|
|
241
|
+
const failureUrl = option.failureUrl ?? option.url;
|
|
242
|
+
const finalWorkflowRunId = getWorkflowRunId(option.workflowRunId);
|
|
243
|
+
|
|
244
|
+
const context = new WorkflowContext({
|
|
245
|
+
qstashClient: this.client,
|
|
246
|
+
headers: new Headers({
|
|
247
|
+
...(option.headers ?? {}),
|
|
248
|
+
...(option.label ? { [WORKFLOW_LABEL_HEADER]: serializeLabel(option.label) } : {}),
|
|
249
|
+
}) as Headers,
|
|
250
|
+
initialPayload: option.body,
|
|
251
|
+
steps: [],
|
|
252
|
+
url: option.url,
|
|
253
|
+
workflowRunId: finalWorkflowRunId,
|
|
254
|
+
telemetry: option.disableTelemetry ? undefined : { sdk: SDK_TELEMETRY },
|
|
255
|
+
label: option.label,
|
|
256
|
+
workflowRunCreatedAt: Date.now(), // pass a timestamp (server will override it)
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
return {
|
|
260
|
+
workflowContext: context,
|
|
261
|
+
telemetry: option.disableTelemetry ? undefined : { sdk: SDK_TELEMETRY },
|
|
262
|
+
delay: option.delay,
|
|
263
|
+
notBefore: option.notBefore,
|
|
264
|
+
failureUrl,
|
|
265
|
+
retries: option.retries,
|
|
266
|
+
retryDelay: option.retryDelay,
|
|
267
|
+
flowControl: option.flowControl,
|
|
268
|
+
redact: option.redact,
|
|
269
|
+
};
|
|
270
|
+
});
|
|
271
|
+
const result = await triggerFirstInvocation(invocations);
|
|
272
|
+
|
|
273
|
+
const workflowRunIds: string[] = invocations.map(
|
|
274
|
+
(invocation) => invocation.workflowContext.workflowRunId
|
|
275
|
+
);
|
|
276
|
+
|
|
277
|
+
if (result.isOk()) {
|
|
278
|
+
return isBatchInput
|
|
279
|
+
? workflowRunIds.map((id) => ({ workflowRunId: id }))
|
|
280
|
+
: { workflowRunId: workflowRunIds[0] };
|
|
281
|
+
} else {
|
|
282
|
+
throw result.error;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Fetches logs for workflow runs.
|
|
288
|
+
*
|
|
289
|
+
* @param workflowRunId - The ID of the workflow run to fetch logs for.
|
|
290
|
+
* @param cursor - The cursor for pagination.
|
|
291
|
+
* @param count - Number of runs to fetch. Default value is 10.
|
|
292
|
+
* @param state - The state of the workflow run.
|
|
293
|
+
* @param workflowUrl - The URL of the workflow. Should be an exact match.
|
|
294
|
+
* @param workflowCreatedAt - The creation time of the workflow. If you have two workflow runs with the same URL, you can use this to filter them.
|
|
295
|
+
* @returns A promise that resolves to either a `WorkflowRunLog` or a `WorkflowRunResponse`.
|
|
296
|
+
*
|
|
297
|
+
* @example
|
|
298
|
+
* Fetch logs for a specific workflow run:
|
|
299
|
+
* ```typescript
|
|
300
|
+
* const { runs } = await client.logs({ workflowRunId: '12345' });
|
|
301
|
+
* const steps = runs[0].steps; // access steps
|
|
302
|
+
* ```
|
|
303
|
+
*
|
|
304
|
+
* @example
|
|
305
|
+
* Fetch logs with pagination:
|
|
306
|
+
* ```typescript
|
|
307
|
+
* const { runs, cursor } = await client.logs();
|
|
308
|
+
* const steps = runs[0].steps // access steps
|
|
309
|
+
*
|
|
310
|
+
* const { runs: nextRuns, cursor: nextCursor } = await client.logs({ cursor, count: 2 });
|
|
311
|
+
* ```
|
|
312
|
+
*
|
|
313
|
+
* @see node_modules/@upstash/workflow/docs/basics/client/logs.mdx
|
|
314
|
+
*/
|
|
315
|
+
public async logs(params?: {
|
|
316
|
+
cursor?: string;
|
|
317
|
+
count?: number;
|
|
318
|
+
filter?: WorkflowLogsListFilters;
|
|
319
|
+
/** @deprecated Use `filter.workflowRunId` instead. */
|
|
320
|
+
workflowRunId?: string;
|
|
321
|
+
/** @deprecated Use `filter.state` instead. */
|
|
322
|
+
state?: string;
|
|
323
|
+
/** @deprecated Use `filter.workflowUrl` instead. */
|
|
324
|
+
workflowUrl?: string;
|
|
325
|
+
/** @deprecated Use `filter.label` instead. */
|
|
326
|
+
label?: string;
|
|
327
|
+
/** @deprecated Use `filter.workflowCreatedAt` instead. */
|
|
328
|
+
workflowCreatedAt?: number;
|
|
329
|
+
}): Promise<WorkflowRunLogs> {
|
|
330
|
+
const { cursor, count, filter, ...legacyFilter } = params ?? {};
|
|
331
|
+
|
|
332
|
+
return await this.client.http.request<WorkflowRunLogs>({
|
|
333
|
+
path: ["v2", "workflows", "events"],
|
|
334
|
+
query: {
|
|
335
|
+
groupBy: "workflowRunId",
|
|
336
|
+
...legacyFilter,
|
|
337
|
+
cursor,
|
|
338
|
+
count,
|
|
339
|
+
...filter,
|
|
340
|
+
},
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* @see node_modules/@upstash/workflow/docs/features/dlq.mdx
|
|
346
|
+
*/
|
|
347
|
+
get dlq() {
|
|
348
|
+
return new DLQ(this.client);
|
|
349
|
+
}
|
|
350
|
+
}
|