@upstash/workflow 0.2.0 → 0.2.2

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/index.mjs CHANGED
@@ -1,12 +1,198 @@
1
1
  import {
2
- Client,
2
+ DEFAULT_RETRIES,
3
3
  StepTypes,
4
4
  WorkflowAbort,
5
5
  WorkflowContext,
6
6
  WorkflowError,
7
7
  WorkflowLogger,
8
- serve
9
- } from "./chunk-5R2BFC3N.mjs";
8
+ getWorkflowRunId,
9
+ makeGetWaitersRequest,
10
+ makeNotifyRequest,
11
+ serve,
12
+ triggerFirstInvocation
13
+ } from "./chunk-Z7WS5XIR.mjs";
14
+
15
+ // src/client/index.ts
16
+ import { Client as QStashClient } from "@upstash/qstash";
17
+ var Client = class {
18
+ client;
19
+ constructor(clientConfig) {
20
+ if (!clientConfig.token) {
21
+ console.error(
22
+ "QStash token is required for Upstash Workflow!\n\nTo fix this:\n1. Get your token from the Upstash Console (https://console.upstash.com/qstash)\n2. Initialize the workflow client with:\n\n const client = new Client({\n token: '<YOUR_QSTASH_TOKEN>'\n });"
23
+ );
24
+ }
25
+ this.client = new QStashClient(clientConfig);
26
+ }
27
+ /**
28
+ * Cancel an ongoing workflow
29
+ *
30
+ * Returns true if workflow is canceled succesfully. Otherwise, throws error.
31
+ *
32
+ * There are multiple ways you can cancel workflows:
33
+ * - pass one or more workflow run ids to cancel them
34
+ * - pass a workflow url to cancel all runs starting with this url
35
+ * - cancel all pending or active workflow runs
36
+ *
37
+ * ### Cancel a set of workflow runs
38
+ *
39
+ * ```ts
40
+ * // cancel a single workflow
41
+ * await client.cancel({ ids: "<WORKFLOW_RUN_ID>" })
42
+ *
43
+ * // cancel a set of workflow runs
44
+ * await client.cancel({ ids: [
45
+ * "<WORKFLOW_RUN_ID_1>",
46
+ * "<WORKFLOW_RUN_ID_2>",
47
+ * ]})
48
+ * ```
49
+ *
50
+ * ### Cancel workflows starting with a url
51
+ *
52
+ * If you have an endpoint called `https://your-endpoint.com` and you
53
+ * want to cancel all workflow runs on it, you can use `urlStartingWith`.
54
+ *
55
+ * Note that this will cancel workflows in all endpoints under
56
+ * `https://your-endpoint.com`.
57
+ *
58
+ * ```ts
59
+ * await client.cancel({ urlStartingWith: "https://your-endpoint.com" })
60
+ * ```
61
+ *
62
+ * ### Cancel *all* workflows
63
+ *
64
+ * To cancel all pending and currently running workflows, you can
65
+ * do it like this:
66
+ *
67
+ * ```ts
68
+ * await client.cancel({ all: true })
69
+ * ```
70
+ *
71
+ * @param ids run id of the workflow to delete
72
+ * @param urlStartingWith cancel workflows starting with this url. Will be ignored
73
+ * if `ids` parameter is set.
74
+ * @param all set to true in order to cancel all workflows. Will be ignored
75
+ * if `ids` or `urlStartingWith` parameters are set.
76
+ * @returns true if workflow is succesfully deleted. Otherwise throws QStashError
77
+ */
78
+ async cancel({
79
+ ids,
80
+ urlStartingWith,
81
+ all
82
+ }) {
83
+ let body;
84
+ if (ids) {
85
+ const runIdArray = typeof ids === "string" ? [ids] : ids;
86
+ body = JSON.stringify({ workflowRunIds: runIdArray });
87
+ } else if (urlStartingWith) {
88
+ body = JSON.stringify({ workflowUrl: urlStartingWith });
89
+ } else if (all) {
90
+ body = "{}";
91
+ } else {
92
+ throw new TypeError("The `cancel` method cannot be called without any options.");
93
+ }
94
+ const result = await this.client.http.request({
95
+ path: ["v2", "workflows", "runs"],
96
+ method: "DELETE",
97
+ body,
98
+ headers: {
99
+ "Content-Type": "application/json"
100
+ }
101
+ });
102
+ return result;
103
+ }
104
+ /**
105
+ * Notify a workflow run waiting for an event
106
+ *
107
+ * ```ts
108
+ * import { Client } from "@upstash/workflow";
109
+ *
110
+ * const client = new Client({ token: "<QSTASH_TOKEN>" })
111
+ * await client.notify({
112
+ * eventId: "my-event-id",
113
+ * eventData: "my-data" // data passed to the workflow run
114
+ * });
115
+ * ```
116
+ *
117
+ * @param eventId event id to notify
118
+ * @param eventData data to provide to the workflow
119
+ */
120
+ async notify({
121
+ eventId,
122
+ eventData
123
+ }) {
124
+ return await makeNotifyRequest(this.client.http, eventId, eventData);
125
+ }
126
+ /**
127
+ * Check waiters of an event
128
+ *
129
+ * ```ts
130
+ * import { Client } from "@upstash/workflow";
131
+ *
132
+ * const client = new Client({ token: "<QSTASH_TOKEN>" })
133
+ * const result = await client.getWaiters({
134
+ * eventId: "my-event-id"
135
+ * })
136
+ * ```
137
+ *
138
+ * @param eventId event id to check
139
+ */
140
+ async getWaiters({ eventId }) {
141
+ return await makeGetWaitersRequest(this.client.http, eventId);
142
+ }
143
+ /**
144
+ * Trigger new workflow run and returns the workflow run id
145
+ *
146
+ * ```ts
147
+ * const { workflowRunId } = await client.trigger({
148
+ * url: "https://workflow-endpoint.com",
149
+ * body: "hello there!", // Optional body
150
+ * headers: { ... }, // Optional headers
151
+ * workflowRunId: "my-workflow", // Optional workflow run ID
152
+ * retries: 3 // Optional retries for the initial request
153
+ * });
154
+ *
155
+ * console.log(workflowRunId)
156
+ * // wfr_my-workflow
157
+ * ```
158
+ *
159
+ * @param url URL of the workflow
160
+ * @param body body to start the workflow with
161
+ * @param headers headers to use in the request
162
+ * @param workflowRunId optional workflow run id to use. mind that
163
+ * you should pass different workflow run ids for different runs.
164
+ * The final workflowRunId will be `wfr_${workflowRunId}`, in
165
+ * other words: the workflow run id you pass will be prefixed
166
+ * with `wfr_`.
167
+ * @param retries retry to use in the initial request. in the rest of
168
+ * the workflow, `retries` option of the `serve` will be used.
169
+ * @returns workflow run id
170
+ */
171
+ async trigger({
172
+ url,
173
+ body,
174
+ headers,
175
+ workflowRunId,
176
+ retries
177
+ }) {
178
+ const finalWorkflowRunId = getWorkflowRunId(workflowRunId);
179
+ const context = new WorkflowContext({
180
+ qstashClient: this.client,
181
+ // @ts-expect-error headers type mismatch
182
+ headers: new Headers(headers ?? {}),
183
+ initialPayload: body,
184
+ steps: [],
185
+ url,
186
+ workflowRunId: finalWorkflowRunId
187
+ });
188
+ const result = await triggerFirstInvocation(context, retries ?? DEFAULT_RETRIES);
189
+ if (result.isOk()) {
190
+ return { workflowRunId: finalWorkflowRunId };
191
+ } else {
192
+ throw result.error;
193
+ }
194
+ }
195
+ };
10
196
  export {
11
197
  Client,
12
198
  StepTypes,
package/nextjs.d.mts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { NextApiHandler } from 'next';
2
- import { R as RouteFunction, W as WorkflowServeOptions } from './types-Cki_MHrh.mjs';
2
+ import { R as RouteFunction, j as PublicServeOptions } from './types-APRap-aV.mjs';
3
3
  import '@upstash/qstash';
4
4
 
5
5
  /**
@@ -11,10 +11,10 @@ import '@upstash/qstash';
11
11
  * @param options workflow options
12
12
  * @returns
13
13
  */
14
- declare const serve: <TInitialPayload = unknown>(routeFunction: RouteFunction<TInitialPayload>, options?: Omit<WorkflowServeOptions<Response, TInitialPayload>, "onStepFinish">) => {
14
+ declare const serve: <TInitialPayload = unknown>(routeFunction: RouteFunction<TInitialPayload>, options?: PublicServeOptions<TInitialPayload>) => {
15
15
  POST: (request: Request) => Promise<Response>;
16
16
  };
17
- declare const servePagesRouter: <TInitialPayload = unknown>(routeFunction: RouteFunction<TInitialPayload>, options?: Omit<WorkflowServeOptions<Response, TInitialPayload>, "onStepFinish">) => {
17
+ declare const servePagesRouter: <TInitialPayload = unknown>(routeFunction: RouteFunction<TInitialPayload>, options?: PublicServeOptions<TInitialPayload>) => {
18
18
  handler: NextApiHandler;
19
19
  };
20
20
 
package/nextjs.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { NextApiHandler } from 'next';
2
- import { R as RouteFunction, W as WorkflowServeOptions } from './types-Cki_MHrh.js';
2
+ import { R as RouteFunction, j as PublicServeOptions } from './types-APRap-aV.js';
3
3
  import '@upstash/qstash';
4
4
 
5
5
  /**
@@ -11,10 +11,10 @@ import '@upstash/qstash';
11
11
  * @param options workflow options
12
12
  * @returns
13
13
  */
14
- declare const serve: <TInitialPayload = unknown>(routeFunction: RouteFunction<TInitialPayload>, options?: Omit<WorkflowServeOptions<Response, TInitialPayload>, "onStepFinish">) => {
14
+ declare const serve: <TInitialPayload = unknown>(routeFunction: RouteFunction<TInitialPayload>, options?: PublicServeOptions<TInitialPayload>) => {
15
15
  POST: (request: Request) => Promise<Response>;
16
16
  };
17
- declare const servePagesRouter: <TInitialPayload = unknown>(routeFunction: RouteFunction<TInitialPayload>, options?: Omit<WorkflowServeOptions<Response, TInitialPayload>, "onStepFinish">) => {
17
+ declare const servePagesRouter: <TInitialPayload = unknown>(routeFunction: RouteFunction<TInitialPayload>, options?: PublicServeOptions<TInitialPayload>) => {
18
18
  handler: NextApiHandler;
19
19
  };
20
20