@upstash/qstash 2.7.9 → 2.7.11-canary
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/{chunk-QK55BUNQ.mjs → chunk-KWSSCN6R.mjs} +30 -0
- package/{chunk-UPFTIDSI.mjs → chunk-XLCNEVA2.mjs} +1 -1
- package/cloudflare.js +2942 -6
- package/cloudflare.mjs +1 -1
- package/h3.js +3333 -5
- package/h3.mjs +2 -2
- package/hono.js +2942 -6
- package/hono.mjs +1 -1
- package/index.js +1623 -27
- package/index.mjs +1 -1
- package/nextjs.js +4598 -34
- package/nextjs.mjs +1631 -7
- package/nuxt.js +828 -7
- package/nuxt.mjs +2 -2
- package/package.json +1 -1
- package/solidjs.js +2946 -11
- package/solidjs.mjs +1 -1
- package/svelte.js +2943 -8
- package/svelte.mjs +1 -1
- package/workflow.js +2939 -3
- package/workflow.mjs +1 -1
- package/LICENSE +0 -21
- package/README.md +0 -167
- package/chunk-R5CZPV7H.js +0 -2952
- package/chunk-VN7YQ2UN.js +0 -1
- package/chunk-YBZBGHDQ.js +0 -403
package/index.js
CHANGED
|
@@ -1,43 +1,1639 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
2
29
|
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var src_exports = {};
|
|
32
|
+
__export(src_exports, {
|
|
33
|
+
Chat: () => Chat,
|
|
34
|
+
Client: () => Client,
|
|
35
|
+
Messages: () => Messages,
|
|
36
|
+
QStashWorkflowAbort: () => QStashWorkflowAbort,
|
|
37
|
+
QStashWorkflowError: () => QStashWorkflowError,
|
|
38
|
+
QstashChatRatelimitError: () => QstashChatRatelimitError,
|
|
39
|
+
QstashDailyRatelimitError: () => QstashDailyRatelimitError,
|
|
40
|
+
QstashError: () => QstashError,
|
|
41
|
+
QstashRatelimitError: () => QstashRatelimitError,
|
|
42
|
+
Receiver: () => Receiver,
|
|
43
|
+
Schedules: () => Schedules,
|
|
44
|
+
SignatureError: () => SignatureError,
|
|
45
|
+
UrlGroups: () => UrlGroups,
|
|
46
|
+
custom: () => custom,
|
|
47
|
+
decodeBase64: () => decodeBase64,
|
|
48
|
+
formatWorkflowError: () => formatWorkflowError,
|
|
49
|
+
openai: () => openai,
|
|
50
|
+
setupAnalytics: () => setupAnalytics,
|
|
51
|
+
upstash: () => upstash
|
|
52
|
+
});
|
|
53
|
+
module.exports = __toCommonJS(src_exports);
|
|
3
54
|
|
|
55
|
+
// src/receiver.ts
|
|
56
|
+
var jose = __toESM(require("jose"));
|
|
57
|
+
var import_crypto_js = __toESM(require("crypto-js"));
|
|
58
|
+
var SignatureError = class extends Error {
|
|
59
|
+
constructor(message) {
|
|
60
|
+
super(message);
|
|
61
|
+
this.name = "SignatureError";
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
var Receiver = class {
|
|
65
|
+
currentSigningKey;
|
|
66
|
+
nextSigningKey;
|
|
67
|
+
constructor(config) {
|
|
68
|
+
this.currentSigningKey = config.currentSigningKey;
|
|
69
|
+
this.nextSigningKey = config.nextSigningKey;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Verify the signature of a request.
|
|
73
|
+
*
|
|
74
|
+
* Tries to verify the signature with the current signing key.
|
|
75
|
+
* If that fails, maybe because you have rotated the keys recently, it will
|
|
76
|
+
* try to verify the signature with the next signing key.
|
|
77
|
+
*
|
|
78
|
+
* If that fails, the signature is invalid and a `SignatureError` is thrown.
|
|
79
|
+
*/
|
|
80
|
+
async verify(request) {
|
|
81
|
+
const isValid = await this.verifyWithKey(this.currentSigningKey, request);
|
|
82
|
+
if (isValid) {
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
return this.verifyWithKey(this.nextSigningKey, request);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Verify signature with a specific signing key
|
|
89
|
+
*/
|
|
90
|
+
async verifyWithKey(key, request) {
|
|
91
|
+
const jwt = await jose.jwtVerify(request.signature, new TextEncoder().encode(key), {
|
|
92
|
+
issuer: "Upstash",
|
|
93
|
+
clockTolerance: request.clockTolerance
|
|
94
|
+
}).catch((error) => {
|
|
95
|
+
throw new SignatureError(error.message);
|
|
96
|
+
});
|
|
97
|
+
const p = jwt.payload;
|
|
98
|
+
if (request.url !== void 0 && p.sub !== request.url) {
|
|
99
|
+
throw new SignatureError(`invalid subject: ${p.sub}, want: ${request.url}`);
|
|
100
|
+
}
|
|
101
|
+
const bodyHash = import_crypto_js.default.SHA256(request.body).toString(import_crypto_js.default.enc.Base64url);
|
|
102
|
+
const padding = new RegExp(/=+$/);
|
|
103
|
+
if (p.body.replace(padding, "") !== bodyHash.replace(padding, "")) {
|
|
104
|
+
throw new SignatureError(`body hash does not match, want: ${p.body}, got: ${bodyHash}`);
|
|
105
|
+
}
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
};
|
|
4
109
|
|
|
110
|
+
// src/client/dlq.ts
|
|
111
|
+
var DLQ = class {
|
|
112
|
+
http;
|
|
113
|
+
constructor(http) {
|
|
114
|
+
this.http = http;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* List messages in the dlq
|
|
118
|
+
*/
|
|
119
|
+
async listMessages(options) {
|
|
120
|
+
const filterPayload = {
|
|
121
|
+
...options?.filter,
|
|
122
|
+
topicName: options?.filter?.urlGroup
|
|
123
|
+
};
|
|
124
|
+
const messagesPayload = await this.http.request({
|
|
125
|
+
method: "GET",
|
|
126
|
+
path: ["v2", "dlq"],
|
|
127
|
+
query: {
|
|
128
|
+
cursor: options?.cursor,
|
|
129
|
+
count: options?.count,
|
|
130
|
+
...filterPayload
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
return {
|
|
134
|
+
messages: messagesPayload.messages.map((message) => {
|
|
135
|
+
return {
|
|
136
|
+
...message,
|
|
137
|
+
urlGroup: message.topicName
|
|
138
|
+
};
|
|
139
|
+
}),
|
|
140
|
+
cursor: messagesPayload.cursor
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Remove a message from the dlq using it's `dlqId`
|
|
145
|
+
*/
|
|
146
|
+
async delete(dlqMessageId) {
|
|
147
|
+
return await this.http.request({
|
|
148
|
+
method: "DELETE",
|
|
149
|
+
path: ["v2", "dlq", dlqMessageId],
|
|
150
|
+
parseResponseAsJson: false
|
|
151
|
+
// there is no response
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Remove multiple messages from the dlq using their `dlqId`s
|
|
156
|
+
*/
|
|
157
|
+
async deleteMany(request) {
|
|
158
|
+
return await this.http.request({
|
|
159
|
+
method: "DELETE",
|
|
160
|
+
path: ["v2", "dlq"],
|
|
161
|
+
headers: { "Content-Type": "application/json" },
|
|
162
|
+
body: JSON.stringify({ dlqIds: request.dlqIds })
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
};
|
|
5
166
|
|
|
167
|
+
// src/client/error.ts
|
|
168
|
+
var QstashError = class extends Error {
|
|
169
|
+
constructor(message) {
|
|
170
|
+
super(message);
|
|
171
|
+
this.name = "QstashError";
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
var QstashRatelimitError = class extends QstashError {
|
|
175
|
+
limit;
|
|
176
|
+
remaining;
|
|
177
|
+
reset;
|
|
178
|
+
constructor(args) {
|
|
179
|
+
super(`Exceeded burst rate limit. ${JSON.stringify(args)} `);
|
|
180
|
+
this.name = "QstashRatelimitError";
|
|
181
|
+
this.limit = args.limit;
|
|
182
|
+
this.remaining = args.remaining;
|
|
183
|
+
this.reset = args.reset;
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
var QstashChatRatelimitError = class extends QstashError {
|
|
187
|
+
limitRequests;
|
|
188
|
+
limitTokens;
|
|
189
|
+
remainingRequests;
|
|
190
|
+
remainingTokens;
|
|
191
|
+
resetRequests;
|
|
192
|
+
resetTokens;
|
|
193
|
+
constructor(args) {
|
|
194
|
+
super(`Exceeded chat rate limit. ${JSON.stringify(args)} `);
|
|
195
|
+
this.limitRequests = args["limit-requests"];
|
|
196
|
+
this.limitTokens = args["limit-tokens"];
|
|
197
|
+
this.remainingRequests = args["remaining-requests"];
|
|
198
|
+
this.remainingTokens = args["remaining-tokens"];
|
|
199
|
+
this.resetRequests = args["reset-requests"];
|
|
200
|
+
this.resetTokens = args["reset-tokens"];
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
var QstashDailyRatelimitError = class extends QstashError {
|
|
204
|
+
limit;
|
|
205
|
+
remaining;
|
|
206
|
+
reset;
|
|
207
|
+
constructor(args) {
|
|
208
|
+
super(`Exceeded daily rate limit. ${JSON.stringify(args)} `);
|
|
209
|
+
this.limit = args.limit;
|
|
210
|
+
this.remaining = args.remaining;
|
|
211
|
+
this.reset = args.reset;
|
|
212
|
+
this.name = "QstashChatRatelimitError";
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
var QStashWorkflowError = class extends QstashError {
|
|
216
|
+
constructor(message) {
|
|
217
|
+
super(message);
|
|
218
|
+
this.name = "QStashWorkflowError";
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
var QStashWorkflowAbort = class extends Error {
|
|
222
|
+
stepInfo;
|
|
223
|
+
stepName;
|
|
224
|
+
constructor(stepName, stepInfo) {
|
|
225
|
+
super(
|
|
226
|
+
`This is an QStash Workflow error thrown after a step executes. It is expected to be raised. Make sure that you await for each step. Also, if you are using try/catch blocks, you should not wrap context.run/sleep/sleepUntil/call methods with try/catch. Aborting workflow after executing step '${stepName}'.`
|
|
227
|
+
);
|
|
228
|
+
this.name = "QStashWorkflowAbort";
|
|
229
|
+
this.stepName = stepName;
|
|
230
|
+
this.stepInfo = stepInfo;
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
var formatWorkflowError = (error) => {
|
|
234
|
+
return error instanceof Error ? {
|
|
235
|
+
error: error.name,
|
|
236
|
+
message: error.message
|
|
237
|
+
} : {
|
|
238
|
+
error: "Error",
|
|
239
|
+
message: "An error occured while executing workflow."
|
|
240
|
+
};
|
|
241
|
+
};
|
|
6
242
|
|
|
243
|
+
// src/client/http.ts
|
|
244
|
+
var HttpClient = class {
|
|
245
|
+
baseUrl;
|
|
246
|
+
authorization;
|
|
247
|
+
options;
|
|
248
|
+
retry;
|
|
249
|
+
constructor(config) {
|
|
250
|
+
this.baseUrl = config.baseUrl.replace(/\/$/, "");
|
|
251
|
+
this.authorization = config.authorization;
|
|
252
|
+
this.retry = // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
253
|
+
typeof config.retry === "boolean" && !config.retry ? {
|
|
254
|
+
attempts: 1,
|
|
255
|
+
backoff: () => 0
|
|
256
|
+
} : {
|
|
257
|
+
attempts: config.retry?.retries ? config.retry.retries + 1 : 5,
|
|
258
|
+
backoff: config.retry?.backoff ?? ((retryCount) => Math.exp(retryCount) * 50)
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
async request(request) {
|
|
262
|
+
const { response } = await this.requestWithBackoff(request);
|
|
263
|
+
if (request.parseResponseAsJson === false) {
|
|
264
|
+
return void 0;
|
|
265
|
+
}
|
|
266
|
+
return await response.json();
|
|
267
|
+
}
|
|
268
|
+
async *requestStream(request) {
|
|
269
|
+
const { response } = await this.requestWithBackoff(request);
|
|
270
|
+
if (!response.body) {
|
|
271
|
+
throw new Error("No response body");
|
|
272
|
+
}
|
|
273
|
+
const body = response.body;
|
|
274
|
+
const reader = body.getReader();
|
|
275
|
+
const decoder = new TextDecoder();
|
|
276
|
+
try {
|
|
277
|
+
while (true) {
|
|
278
|
+
const { done, value } = await reader.read();
|
|
279
|
+
if (done) {
|
|
280
|
+
break;
|
|
281
|
+
}
|
|
282
|
+
const chunkText = decoder.decode(value, { stream: true });
|
|
283
|
+
const chunks = chunkText.split("\n").filter(Boolean);
|
|
284
|
+
for (const chunk of chunks) {
|
|
285
|
+
if (chunk.startsWith("data: ")) {
|
|
286
|
+
const data = chunk.slice(6);
|
|
287
|
+
if (data === "[DONE]") {
|
|
288
|
+
break;
|
|
289
|
+
}
|
|
290
|
+
yield JSON.parse(data);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
} finally {
|
|
295
|
+
await reader.cancel();
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
requestWithBackoff = async (request) => {
|
|
299
|
+
const [url, requestOptions] = this.processRequest(request);
|
|
300
|
+
let response = void 0;
|
|
301
|
+
let error = void 0;
|
|
302
|
+
for (let index = 0; index < this.retry.attempts; index++) {
|
|
303
|
+
try {
|
|
304
|
+
response = await fetch(url.toString(), requestOptions);
|
|
305
|
+
break;
|
|
306
|
+
} catch (error_) {
|
|
307
|
+
error = error_;
|
|
308
|
+
await new Promise((r) => setTimeout(r, this.retry.backoff(index)));
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
if (!response) {
|
|
312
|
+
throw error ?? new Error("Exhausted all retries");
|
|
313
|
+
}
|
|
314
|
+
await this.checkResponse(response);
|
|
315
|
+
return {
|
|
316
|
+
response,
|
|
317
|
+
error
|
|
318
|
+
};
|
|
319
|
+
};
|
|
320
|
+
processRequest = (request) => {
|
|
321
|
+
const headers = new Headers(request.headers);
|
|
322
|
+
if (!headers.has("Authorization")) {
|
|
323
|
+
headers.set("Authorization", this.authorization);
|
|
324
|
+
}
|
|
325
|
+
const requestOptions = {
|
|
326
|
+
method: request.method,
|
|
327
|
+
headers,
|
|
328
|
+
body: request.body,
|
|
329
|
+
keepalive: request.keepalive
|
|
330
|
+
};
|
|
331
|
+
const url = new URL([request.baseUrl ?? this.baseUrl, ...request.path].join("/"));
|
|
332
|
+
if (request.query) {
|
|
333
|
+
for (const [key, value] of Object.entries(request.query)) {
|
|
334
|
+
if (value !== void 0) {
|
|
335
|
+
url.searchParams.set(key, value.toString());
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
return [url.toString(), requestOptions];
|
|
340
|
+
};
|
|
341
|
+
async checkResponse(response) {
|
|
342
|
+
if (response.status === 429) {
|
|
343
|
+
if (response.headers.get("x-ratelimit-limit-requests")) {
|
|
344
|
+
throw new QstashChatRatelimitError({
|
|
345
|
+
"limit-requests": response.headers.get("x-ratelimit-limit-requests"),
|
|
346
|
+
"limit-tokens": response.headers.get("x-ratelimit-limit-tokens"),
|
|
347
|
+
"remaining-requests": response.headers.get("x-ratelimit-remaining-requests"),
|
|
348
|
+
"remaining-tokens": response.headers.get("x-ratelimit-remaining-tokens"),
|
|
349
|
+
"reset-requests": response.headers.get("x-ratelimit-reset-requests"),
|
|
350
|
+
"reset-tokens": response.headers.get("x-ratelimit-reset-tokens")
|
|
351
|
+
});
|
|
352
|
+
} else if (response.headers.get("RateLimit-Limit")) {
|
|
353
|
+
throw new QstashDailyRatelimitError({
|
|
354
|
+
limit: response.headers.get("RateLimit-Limit"),
|
|
355
|
+
remaining: response.headers.get("RateLimit-Remaining"),
|
|
356
|
+
reset: response.headers.get("RateLimit-Reset")
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
throw new QstashRatelimitError({
|
|
360
|
+
limit: response.headers.get("Burst-RateLimit-Limit"),
|
|
361
|
+
remaining: response.headers.get("Burst-RateLimit-Remaining"),
|
|
362
|
+
reset: response.headers.get("Burst-RateLimit-Reset")
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
if (response.status < 200 || response.status >= 300) {
|
|
366
|
+
const body = await response.text();
|
|
367
|
+
throw new QstashError(body.length > 0 ? body : `Error: status=${response.status}`);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
};
|
|
7
371
|
|
|
372
|
+
// src/client/llm/providers.ts
|
|
373
|
+
var setupAnalytics = (analytics, providerApiKey, providerBaseUrl, provider) => {
|
|
374
|
+
if (!analytics)
|
|
375
|
+
return {};
|
|
376
|
+
switch (analytics.name) {
|
|
377
|
+
case "helicone": {
|
|
378
|
+
switch (provider) {
|
|
379
|
+
case "upstash": {
|
|
380
|
+
return {
|
|
381
|
+
baseURL: "https://qstash.helicone.ai/llm/v1/chat/completions",
|
|
382
|
+
defaultHeaders: {
|
|
383
|
+
"Helicone-Auth": `Bearer ${analytics.token}`,
|
|
384
|
+
Authorization: `Bearer ${providerApiKey}`
|
|
385
|
+
}
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
default: {
|
|
389
|
+
return {
|
|
390
|
+
baseURL: "https://gateway.helicone.ai/v1/chat/completions",
|
|
391
|
+
defaultHeaders: {
|
|
392
|
+
"Helicone-Auth": `Bearer ${analytics.token}`,
|
|
393
|
+
"Helicone-Target-Url": providerBaseUrl,
|
|
394
|
+
Authorization: `Bearer ${providerApiKey}`
|
|
395
|
+
}
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
default: {
|
|
401
|
+
throw new Error("Unknown analytics provider");
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
};
|
|
405
|
+
var upstash = () => {
|
|
406
|
+
return {
|
|
407
|
+
owner: "upstash",
|
|
408
|
+
baseUrl: "https://qstash.upstash.io/llm",
|
|
409
|
+
token: ""
|
|
410
|
+
};
|
|
411
|
+
};
|
|
412
|
+
var openai = ({
|
|
413
|
+
token
|
|
414
|
+
}) => {
|
|
415
|
+
return { token, owner: "openai", baseUrl: "https://api.openai.com" };
|
|
416
|
+
};
|
|
417
|
+
var custom = ({
|
|
418
|
+
baseUrl,
|
|
419
|
+
token
|
|
420
|
+
}) => {
|
|
421
|
+
const trimmedBaseUrl = baseUrl.replace(/\/(v1\/)?chat\/completions$/, "");
|
|
422
|
+
return {
|
|
423
|
+
token,
|
|
424
|
+
owner: "custom",
|
|
425
|
+
baseUrl: trimmedBaseUrl
|
|
426
|
+
};
|
|
427
|
+
};
|
|
8
428
|
|
|
429
|
+
// src/client/llm/chat.ts
|
|
430
|
+
var Chat = class _Chat {
|
|
431
|
+
http;
|
|
432
|
+
token;
|
|
433
|
+
constructor(http, token) {
|
|
434
|
+
this.http = http;
|
|
435
|
+
this.token = token;
|
|
436
|
+
}
|
|
437
|
+
static toChatRequest(request) {
|
|
438
|
+
const messages = [];
|
|
439
|
+
messages.push(
|
|
440
|
+
{ role: "system", content: request.system },
|
|
441
|
+
{ role: "user", content: request.user }
|
|
442
|
+
);
|
|
443
|
+
const chatRequest = { ...request, messages };
|
|
444
|
+
return chatRequest;
|
|
445
|
+
}
|
|
446
|
+
/**
|
|
447
|
+
* Calls the Upstash completions api given a ChatRequest.
|
|
448
|
+
*
|
|
449
|
+
* Returns a ChatCompletion or a stream of ChatCompletionChunks
|
|
450
|
+
* if stream is enabled.
|
|
451
|
+
*
|
|
452
|
+
* @param request ChatRequest with messages
|
|
453
|
+
* @returns Chat completion or stream
|
|
454
|
+
*/
|
|
455
|
+
create = async (request) => {
|
|
456
|
+
if (request.provider.owner != "upstash")
|
|
457
|
+
return this.createThirdParty(request);
|
|
458
|
+
const body = JSON.stringify(request);
|
|
459
|
+
let baseUrl = void 0;
|
|
460
|
+
let headers = {
|
|
461
|
+
"Content-Type": "application/json",
|
|
462
|
+
Authorization: `Bearer ${this.token}`,
|
|
463
|
+
..."stream" in request && request.stream ? {
|
|
464
|
+
Connection: "keep-alive",
|
|
465
|
+
Accept: "text/event-stream",
|
|
466
|
+
"Cache-Control": "no-cache"
|
|
467
|
+
} : {}
|
|
468
|
+
};
|
|
469
|
+
if (request.analytics) {
|
|
470
|
+
const { baseURL, defaultHeaders } = setupAnalytics(
|
|
471
|
+
{ name: "helicone", token: request.analytics.token },
|
|
472
|
+
this.getAuthorizationToken(),
|
|
473
|
+
request.provider.baseUrl,
|
|
474
|
+
"upstash"
|
|
475
|
+
);
|
|
476
|
+
headers = { ...headers, ...defaultHeaders };
|
|
477
|
+
baseUrl = baseURL;
|
|
478
|
+
}
|
|
479
|
+
const path = request.analytics ? [] : ["llm", "v1", "chat", "completions"];
|
|
480
|
+
return "stream" in request && request.stream ? this.http.requestStream({
|
|
481
|
+
path,
|
|
482
|
+
method: "POST",
|
|
483
|
+
headers,
|
|
484
|
+
baseUrl,
|
|
485
|
+
body
|
|
486
|
+
}) : this.http.request({
|
|
487
|
+
path,
|
|
488
|
+
method: "POST",
|
|
489
|
+
headers,
|
|
490
|
+
baseUrl,
|
|
491
|
+
body
|
|
492
|
+
});
|
|
493
|
+
};
|
|
494
|
+
/**
|
|
495
|
+
* Calls the Upstash completions api given a ChatRequest.
|
|
496
|
+
*
|
|
497
|
+
* Returns a ChatCompletion or a stream of ChatCompletionChunks
|
|
498
|
+
* if stream is enabled.
|
|
499
|
+
*
|
|
500
|
+
* @param request ChatRequest with messages
|
|
501
|
+
* @returns Chat completion or stream
|
|
502
|
+
*/
|
|
503
|
+
// eslint-disable-next-line @typescript-eslint/require-await
|
|
504
|
+
createThirdParty = async (request) => {
|
|
505
|
+
const { baseUrl, token, owner } = request.provider;
|
|
506
|
+
if (owner === "upstash")
|
|
507
|
+
throw new Error("Upstash is not 3rd party provider!");
|
|
508
|
+
delete request.provider;
|
|
509
|
+
delete request.system;
|
|
510
|
+
const analytics = request.analytics;
|
|
511
|
+
delete request.analytics;
|
|
512
|
+
const body = JSON.stringify(request);
|
|
513
|
+
const isAnalyticsEnabled = analytics?.name && analytics.token;
|
|
514
|
+
const analyticsConfig = analytics?.name && analytics.token ? setupAnalytics({ name: analytics.name, token: analytics.token }, token, baseUrl, owner) : { defaultHeaders: void 0, baseURL: baseUrl };
|
|
515
|
+
const isStream = "stream" in request && request.stream;
|
|
516
|
+
const headers = {
|
|
517
|
+
"Content-Type": "application/json",
|
|
518
|
+
Authorization: `Bearer ${token}`,
|
|
519
|
+
...isStream ? {
|
|
520
|
+
Connection: "keep-alive",
|
|
521
|
+
Accept: "text/event-stream",
|
|
522
|
+
"Cache-Control": "no-cache"
|
|
523
|
+
} : {},
|
|
524
|
+
...analyticsConfig.defaultHeaders
|
|
525
|
+
};
|
|
526
|
+
const response = await this.http[isStream ? "requestStream" : "request"]({
|
|
527
|
+
path: isAnalyticsEnabled ? [] : ["v1", "chat", "completions"],
|
|
528
|
+
method: "POST",
|
|
529
|
+
headers,
|
|
530
|
+
body,
|
|
531
|
+
baseUrl: analyticsConfig.baseURL
|
|
532
|
+
});
|
|
533
|
+
return response;
|
|
534
|
+
};
|
|
535
|
+
// Helper method to get the authorization token
|
|
536
|
+
getAuthorizationToken() {
|
|
537
|
+
const authHeader = String(this.http.authorization);
|
|
538
|
+
const match = /Bearer (.+)/.exec(authHeader);
|
|
539
|
+
if (!match) {
|
|
540
|
+
throw new Error("Invalid authorization header format");
|
|
541
|
+
}
|
|
542
|
+
return match[1];
|
|
543
|
+
}
|
|
544
|
+
/**
|
|
545
|
+
* Calls the Upstash completions api given a PromptRequest.
|
|
546
|
+
*
|
|
547
|
+
* Returns a ChatCompletion or a stream of ChatCompletionChunks
|
|
548
|
+
* if stream is enabled.
|
|
549
|
+
*
|
|
550
|
+
* @param request PromptRequest with system and user messages.
|
|
551
|
+
* Note that system parameter shouldn't be passed in the case of
|
|
552
|
+
* mistralai/Mistral-7B-Instruct-v0.2 model.
|
|
553
|
+
* @returns Chat completion or stream
|
|
554
|
+
*/
|
|
555
|
+
prompt = async (request) => {
|
|
556
|
+
const chatRequest = _Chat.toChatRequest(request);
|
|
557
|
+
return this.create(chatRequest);
|
|
558
|
+
};
|
|
559
|
+
};
|
|
9
560
|
|
|
561
|
+
// src/client/llm/utils.ts
|
|
562
|
+
function appendLLMOptionsIfNeeded(request, headers, http) {
|
|
563
|
+
if (!request.api)
|
|
564
|
+
return;
|
|
565
|
+
const provider = request.api.provider;
|
|
566
|
+
const analytics = request.api.analytics;
|
|
567
|
+
if (provider?.owner === "upstash") {
|
|
568
|
+
handleUpstashProvider(request, headers, http, analytics);
|
|
569
|
+
return;
|
|
570
|
+
}
|
|
571
|
+
if (!("provider" in request.api))
|
|
572
|
+
return;
|
|
573
|
+
const { baseUrl, token } = validateProviderConfig(provider);
|
|
574
|
+
const analyticsConfig = analytics ? setupAnalytics({ name: analytics.name, token: analytics.token }, token, baseUrl, "custom") : void 0;
|
|
575
|
+
if (analyticsConfig) {
|
|
576
|
+
setAnalyticsHeaders(headers, analyticsConfig);
|
|
577
|
+
request.url = analyticsConfig.baseURL;
|
|
578
|
+
} else {
|
|
579
|
+
request.url = `${baseUrl}/v1/chat/completions`;
|
|
580
|
+
headers.set("Authorization", `Bearer ${token}`);
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
function handleUpstashProvider(request, headers, http, analytics) {
|
|
584
|
+
if (analytics) {
|
|
585
|
+
const analyticsConfig = setupAnalytics(
|
|
586
|
+
{ name: analytics.name, token: analytics.token },
|
|
587
|
+
//@ts-expect-error hacky way to get bearer token
|
|
588
|
+
String(http.authorization).split("Bearer ")[1],
|
|
589
|
+
request.api?.provider?.baseUrl,
|
|
590
|
+
"upstash"
|
|
591
|
+
);
|
|
592
|
+
setAnalyticsHeaders(headers, analyticsConfig);
|
|
593
|
+
request.url = analyticsConfig.baseURL;
|
|
594
|
+
} else {
|
|
595
|
+
request.api = { name: "llm" };
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
function validateProviderConfig(provider) {
|
|
599
|
+
if (!provider?.baseUrl)
|
|
600
|
+
throw new Error("baseUrl cannot be empty or undefined!");
|
|
601
|
+
if (!provider.token)
|
|
602
|
+
throw new Error("token cannot be empty or undefined!");
|
|
603
|
+
return { baseUrl: provider.baseUrl, token: provider.token };
|
|
604
|
+
}
|
|
605
|
+
function setAnalyticsHeaders(headers, analyticsConfig) {
|
|
606
|
+
headers.set("Helicone-Auth", analyticsConfig.defaultHeaders?.["Helicone-Auth"] ?? "");
|
|
607
|
+
headers.set("Authorization", analyticsConfig.defaultHeaders?.Authorization ?? "");
|
|
608
|
+
if (analyticsConfig.defaultHeaders?.["Helicone-Target-Url"]) {
|
|
609
|
+
headers.set("Helicone-Target-Url", analyticsConfig.defaultHeaders["Helicone-Target-Url"]);
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
function ensureCallbackPresent(request) {
|
|
613
|
+
if (request.api?.name === "llm" && !request.callback) {
|
|
614
|
+
throw new TypeError("Callback cannot be undefined when using LLM");
|
|
615
|
+
}
|
|
616
|
+
}
|
|
10
617
|
|
|
618
|
+
// src/client/messages.ts
|
|
619
|
+
var Messages = class {
|
|
620
|
+
http;
|
|
621
|
+
constructor(http) {
|
|
622
|
+
this.http = http;
|
|
623
|
+
}
|
|
624
|
+
/**
|
|
625
|
+
* Get a message
|
|
626
|
+
*/
|
|
627
|
+
async get(messageId) {
|
|
628
|
+
const messagePayload = await this.http.request({
|
|
629
|
+
method: "GET",
|
|
630
|
+
path: ["v2", "messages", messageId]
|
|
631
|
+
});
|
|
632
|
+
const message = {
|
|
633
|
+
...messagePayload,
|
|
634
|
+
urlGroup: messagePayload.topicName
|
|
635
|
+
};
|
|
636
|
+
return message;
|
|
637
|
+
}
|
|
638
|
+
/**
|
|
639
|
+
* Cancel a message
|
|
640
|
+
*/
|
|
641
|
+
async delete(messageId) {
|
|
642
|
+
return await this.http.request({
|
|
643
|
+
method: "DELETE",
|
|
644
|
+
path: ["v2", "messages", messageId],
|
|
645
|
+
parseResponseAsJson: false
|
|
646
|
+
});
|
|
647
|
+
}
|
|
648
|
+
async deleteMany(messageIds) {
|
|
649
|
+
const result = await this.http.request({
|
|
650
|
+
method: "DELETE",
|
|
651
|
+
path: ["v2", "messages"],
|
|
652
|
+
headers: { "Content-Type": "application/json" },
|
|
653
|
+
body: JSON.stringify({ messageIds })
|
|
654
|
+
});
|
|
655
|
+
return result.cancelled;
|
|
656
|
+
}
|
|
657
|
+
async deleteAll() {
|
|
658
|
+
const result = await this.http.request({
|
|
659
|
+
method: "DELETE",
|
|
660
|
+
path: ["v2", "messages"]
|
|
661
|
+
});
|
|
662
|
+
return result.cancelled;
|
|
663
|
+
}
|
|
664
|
+
};
|
|
11
665
|
|
|
666
|
+
// src/client/utils.ts
|
|
667
|
+
var isIgnoredHeader = (header) => {
|
|
668
|
+
const lowerCaseHeader = header.toLowerCase();
|
|
669
|
+
return lowerCaseHeader.startsWith("content-type") || lowerCaseHeader.startsWith("upstash-");
|
|
670
|
+
};
|
|
671
|
+
function prefixHeaders(headers) {
|
|
672
|
+
const keysToBePrefixed = [...headers.keys()].filter((key) => !isIgnoredHeader(key));
|
|
673
|
+
for (const key of keysToBePrefixed) {
|
|
674
|
+
const value = headers.get(key);
|
|
675
|
+
if (value !== null) {
|
|
676
|
+
headers.set(`Upstash-Forward-${key}`, value);
|
|
677
|
+
}
|
|
678
|
+
headers.delete(key);
|
|
679
|
+
}
|
|
680
|
+
return headers;
|
|
681
|
+
}
|
|
682
|
+
function processHeaders(request) {
|
|
683
|
+
const headers = prefixHeaders(new Headers(request.headers));
|
|
684
|
+
headers.set("Upstash-Method", request.method ?? "POST");
|
|
685
|
+
if (request.delay !== void 0) {
|
|
686
|
+
if (typeof request.delay === "string") {
|
|
687
|
+
headers.set("Upstash-Delay", request.delay);
|
|
688
|
+
} else {
|
|
689
|
+
headers.set("Upstash-Delay", `${request.delay.toFixed(0)}s`);
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
if (request.notBefore !== void 0) {
|
|
693
|
+
headers.set("Upstash-Not-Before", request.notBefore.toFixed(0));
|
|
694
|
+
}
|
|
695
|
+
if (request.deduplicationId !== void 0) {
|
|
696
|
+
headers.set("Upstash-Deduplication-Id", request.deduplicationId);
|
|
697
|
+
}
|
|
698
|
+
if (request.contentBasedDeduplication !== void 0) {
|
|
699
|
+
headers.set("Upstash-Content-Based-Deduplication", "true");
|
|
700
|
+
}
|
|
701
|
+
if (request.retries !== void 0) {
|
|
702
|
+
headers.set("Upstash-Retries", request.retries.toFixed(0));
|
|
703
|
+
}
|
|
704
|
+
if (request.callback !== void 0) {
|
|
705
|
+
headers.set("Upstash-Callback", request.callback);
|
|
706
|
+
}
|
|
707
|
+
if (request.failureCallback !== void 0) {
|
|
708
|
+
headers.set("Upstash-Failure-Callback", request.failureCallback);
|
|
709
|
+
}
|
|
710
|
+
if (request.timeout !== void 0) {
|
|
711
|
+
if (typeof request.timeout === "string") {
|
|
712
|
+
headers.set("Upstash-Timeout", request.timeout);
|
|
713
|
+
} else {
|
|
714
|
+
headers.set("Upstash-Timeout", `${request.timeout}s`);
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
return headers;
|
|
718
|
+
}
|
|
719
|
+
function getRequestPath(request) {
|
|
720
|
+
return request.url ?? request.urlGroup ?? request.topic ?? `api/${request.api?.name}`;
|
|
721
|
+
}
|
|
722
|
+
function decodeBase64(base64) {
|
|
723
|
+
try {
|
|
724
|
+
const binString = atob(base64);
|
|
725
|
+
const intArray = Uint8Array.from(binString, (m) => m.codePointAt(0));
|
|
726
|
+
return new TextDecoder().decode(intArray);
|
|
727
|
+
} catch (error) {
|
|
728
|
+
console.warn(
|
|
729
|
+
`Upstash Qstash: Failed while decoding base64 "${base64}". Decoding with atob and returning it instead. Error: ${error}`
|
|
730
|
+
);
|
|
731
|
+
return atob(base64);
|
|
732
|
+
}
|
|
733
|
+
}
|
|
12
734
|
|
|
735
|
+
// src/client/queue.ts
|
|
736
|
+
var Queue = class {
|
|
737
|
+
http;
|
|
738
|
+
queueName;
|
|
739
|
+
constructor(http, queueName) {
|
|
740
|
+
this.http = http;
|
|
741
|
+
this.queueName = queueName;
|
|
742
|
+
}
|
|
743
|
+
/**
|
|
744
|
+
* Create or update the queue
|
|
745
|
+
*/
|
|
746
|
+
async upsert(request) {
|
|
747
|
+
if (!this.queueName) {
|
|
748
|
+
throw new Error("Please provide a queue name to the Queue constructor");
|
|
749
|
+
}
|
|
750
|
+
const body = {
|
|
751
|
+
queueName: this.queueName,
|
|
752
|
+
parallelism: request.parallelism ?? 1,
|
|
753
|
+
paused: request.paused ?? false
|
|
754
|
+
};
|
|
755
|
+
await this.http.request({
|
|
756
|
+
method: "POST",
|
|
757
|
+
path: ["v2", "queues"],
|
|
758
|
+
headers: {
|
|
759
|
+
"Content-Type": "application/json"
|
|
760
|
+
},
|
|
761
|
+
body: JSON.stringify(body),
|
|
762
|
+
parseResponseAsJson: false
|
|
763
|
+
});
|
|
764
|
+
}
|
|
765
|
+
/**
|
|
766
|
+
* Get the queue details
|
|
767
|
+
*/
|
|
768
|
+
async get() {
|
|
769
|
+
if (!this.queueName) {
|
|
770
|
+
throw new Error("Please provide a queue name to the Queue constructor");
|
|
771
|
+
}
|
|
772
|
+
return await this.http.request({
|
|
773
|
+
method: "GET",
|
|
774
|
+
path: ["v2", "queues", this.queueName]
|
|
775
|
+
});
|
|
776
|
+
}
|
|
777
|
+
/**
|
|
778
|
+
* List queues
|
|
779
|
+
*/
|
|
780
|
+
async list() {
|
|
781
|
+
return await this.http.request({
|
|
782
|
+
method: "GET",
|
|
783
|
+
path: ["v2", "queues"]
|
|
784
|
+
});
|
|
785
|
+
}
|
|
786
|
+
/**
|
|
787
|
+
* Delete the queue
|
|
788
|
+
*/
|
|
789
|
+
async delete() {
|
|
790
|
+
if (!this.queueName) {
|
|
791
|
+
throw new Error("Please provide a queue name to the Queue constructor");
|
|
792
|
+
}
|
|
793
|
+
await this.http.request({
|
|
794
|
+
method: "DELETE",
|
|
795
|
+
path: ["v2", "queues", this.queueName],
|
|
796
|
+
parseResponseAsJson: false
|
|
797
|
+
});
|
|
798
|
+
}
|
|
799
|
+
/**
|
|
800
|
+
* Enqueue a message to a queue.
|
|
801
|
+
*/
|
|
802
|
+
async enqueue(request) {
|
|
803
|
+
if (!this.queueName) {
|
|
804
|
+
throw new Error("Please provide a queue name to the Queue constructor");
|
|
805
|
+
}
|
|
806
|
+
const headers = processHeaders(request);
|
|
807
|
+
const destination = getRequestPath(request);
|
|
808
|
+
const response = await this.http.request({
|
|
809
|
+
path: ["v2", "enqueue", this.queueName, destination],
|
|
810
|
+
body: request.body,
|
|
811
|
+
headers,
|
|
812
|
+
method: "POST"
|
|
813
|
+
});
|
|
814
|
+
return response;
|
|
815
|
+
}
|
|
816
|
+
/**
|
|
817
|
+
* Enqueue a message to a queue, serializing the body to JSON.
|
|
818
|
+
*/
|
|
819
|
+
async enqueueJSON(request) {
|
|
820
|
+
const headers = prefixHeaders(new Headers(request.headers));
|
|
821
|
+
headers.set("Content-Type", "application/json");
|
|
822
|
+
ensureCallbackPresent(request);
|
|
823
|
+
appendLLMOptionsIfNeeded(request, headers, this.http);
|
|
824
|
+
const response = await this.enqueue({
|
|
825
|
+
...request,
|
|
826
|
+
body: JSON.stringify(request.body),
|
|
827
|
+
headers
|
|
828
|
+
});
|
|
829
|
+
return response;
|
|
830
|
+
}
|
|
831
|
+
/**
|
|
832
|
+
* Pauses the queue.
|
|
833
|
+
*
|
|
834
|
+
* A paused queue will not deliver messages until
|
|
835
|
+
* it is resumed.
|
|
836
|
+
*/
|
|
837
|
+
async pause() {
|
|
838
|
+
if (!this.queueName) {
|
|
839
|
+
throw new Error("Please provide a queue name to the Queue constructor");
|
|
840
|
+
}
|
|
841
|
+
await this.http.request({
|
|
842
|
+
method: "POST",
|
|
843
|
+
path: ["v2", "queues", this.queueName, "pause"],
|
|
844
|
+
parseResponseAsJson: false
|
|
845
|
+
});
|
|
846
|
+
}
|
|
847
|
+
/**
|
|
848
|
+
* Resumes the queue.
|
|
849
|
+
*/
|
|
850
|
+
async resume() {
|
|
851
|
+
if (!this.queueName) {
|
|
852
|
+
throw new Error("Please provide a queue name to the Queue constructor");
|
|
853
|
+
}
|
|
854
|
+
await this.http.request({
|
|
855
|
+
method: "POST",
|
|
856
|
+
path: ["v2", "queues", this.queueName, "resume"],
|
|
857
|
+
parseResponseAsJson: false
|
|
858
|
+
});
|
|
859
|
+
}
|
|
860
|
+
};
|
|
13
861
|
|
|
862
|
+
// src/client/schedules.ts
|
|
863
|
+
var Schedules = class {
|
|
864
|
+
http;
|
|
865
|
+
constructor(http) {
|
|
866
|
+
this.http = http;
|
|
867
|
+
}
|
|
868
|
+
/**
|
|
869
|
+
* Create a schedule
|
|
870
|
+
*/
|
|
871
|
+
async create(request) {
|
|
872
|
+
const headers = prefixHeaders(new Headers(request.headers));
|
|
873
|
+
if (!headers.has("Content-Type")) {
|
|
874
|
+
headers.set("Content-Type", "application/json");
|
|
875
|
+
}
|
|
876
|
+
headers.set("Upstash-Cron", request.cron);
|
|
877
|
+
if (request.method !== void 0) {
|
|
878
|
+
headers.set("Upstash-Method", request.method);
|
|
879
|
+
}
|
|
880
|
+
if (request.delay !== void 0) {
|
|
881
|
+
if (typeof request.delay === "string") {
|
|
882
|
+
headers.set("Upstash-Delay", request.delay);
|
|
883
|
+
} else {
|
|
884
|
+
headers.set("Upstash-Delay", `${request.delay.toFixed(0)}s`);
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
if (request.retries !== void 0) {
|
|
888
|
+
headers.set("Upstash-Retries", request.retries.toFixed(0));
|
|
889
|
+
}
|
|
890
|
+
if (request.callback !== void 0) {
|
|
891
|
+
headers.set("Upstash-Callback", request.callback);
|
|
892
|
+
}
|
|
893
|
+
if (request.failureCallback !== void 0) {
|
|
894
|
+
headers.set("Upstash-Failure-Callback", request.failureCallback);
|
|
895
|
+
}
|
|
896
|
+
if (request.timeout !== void 0) {
|
|
897
|
+
if (typeof request.timeout === "string") {
|
|
898
|
+
headers.set("Upstash-Timeout", request.timeout);
|
|
899
|
+
} else {
|
|
900
|
+
headers.set("Upstash-Timeout", `${request.timeout}s`);
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
if (request.scheduleId !== void 0) {
|
|
904
|
+
headers.set("Upstash-Schedule-Id", request.scheduleId);
|
|
905
|
+
}
|
|
906
|
+
return await this.http.request({
|
|
907
|
+
method: "POST",
|
|
908
|
+
headers,
|
|
909
|
+
path: ["v2", "schedules", request.destination],
|
|
910
|
+
body: request.body
|
|
911
|
+
});
|
|
912
|
+
}
|
|
913
|
+
/**
|
|
914
|
+
* Get a schedule
|
|
915
|
+
*/
|
|
916
|
+
async get(scheduleId) {
|
|
917
|
+
return await this.http.request({
|
|
918
|
+
method: "GET",
|
|
919
|
+
path: ["v2", "schedules", scheduleId]
|
|
920
|
+
});
|
|
921
|
+
}
|
|
922
|
+
/**
|
|
923
|
+
* List your schedules
|
|
924
|
+
*/
|
|
925
|
+
async list() {
|
|
926
|
+
return await this.http.request({
|
|
927
|
+
method: "GET",
|
|
928
|
+
path: ["v2", "schedules"]
|
|
929
|
+
});
|
|
930
|
+
}
|
|
931
|
+
/**
|
|
932
|
+
* Delete a schedule
|
|
933
|
+
*/
|
|
934
|
+
async delete(scheduleId) {
|
|
935
|
+
return await this.http.request({
|
|
936
|
+
method: "DELETE",
|
|
937
|
+
path: ["v2", "schedules", scheduleId],
|
|
938
|
+
parseResponseAsJson: false
|
|
939
|
+
});
|
|
940
|
+
}
|
|
941
|
+
/**
|
|
942
|
+
* Pauses the schedule.
|
|
943
|
+
*
|
|
944
|
+
* A paused schedule will not deliver messages until
|
|
945
|
+
* it is resumed.
|
|
946
|
+
*/
|
|
947
|
+
async pause({ schedule }) {
|
|
948
|
+
await this.http.request({
|
|
949
|
+
method: "PATCH",
|
|
950
|
+
path: ["v2", "schedules", schedule, "pause"],
|
|
951
|
+
parseResponseAsJson: false
|
|
952
|
+
});
|
|
953
|
+
}
|
|
954
|
+
/**
|
|
955
|
+
* Resumes the schedule.
|
|
956
|
+
*/
|
|
957
|
+
async resume({ schedule }) {
|
|
958
|
+
await this.http.request({
|
|
959
|
+
method: "PATCH",
|
|
960
|
+
path: ["v2", "schedules", schedule, "resume"],
|
|
961
|
+
parseResponseAsJson: false
|
|
962
|
+
});
|
|
963
|
+
}
|
|
964
|
+
};
|
|
14
965
|
|
|
966
|
+
// src/client/url-groups.ts
|
|
967
|
+
var UrlGroups = class {
|
|
968
|
+
http;
|
|
969
|
+
constructor(http) {
|
|
970
|
+
this.http = http;
|
|
971
|
+
}
|
|
972
|
+
/**
|
|
973
|
+
* Create a new url group with the given name and endpoints
|
|
974
|
+
*/
|
|
975
|
+
async addEndpoints(request) {
|
|
976
|
+
await this.http.request({
|
|
977
|
+
method: "POST",
|
|
978
|
+
path: ["v2", "topics", request.name, "endpoints"],
|
|
979
|
+
headers: { "Content-Type": "application/json" },
|
|
980
|
+
body: JSON.stringify({ endpoints: request.endpoints }),
|
|
981
|
+
parseResponseAsJson: false
|
|
982
|
+
});
|
|
983
|
+
}
|
|
984
|
+
/**
|
|
985
|
+
* Remove endpoints from a url group.
|
|
986
|
+
*/
|
|
987
|
+
async removeEndpoints(request) {
|
|
988
|
+
await this.http.request({
|
|
989
|
+
method: "DELETE",
|
|
990
|
+
path: ["v2", "topics", request.name, "endpoints"],
|
|
991
|
+
headers: { "Content-Type": "application/json" },
|
|
992
|
+
body: JSON.stringify({ endpoints: request.endpoints }),
|
|
993
|
+
parseResponseAsJson: false
|
|
994
|
+
});
|
|
995
|
+
}
|
|
996
|
+
/**
|
|
997
|
+
* Get a list of all url groups.
|
|
998
|
+
*/
|
|
999
|
+
async list() {
|
|
1000
|
+
return await this.http.request({
|
|
1001
|
+
method: "GET",
|
|
1002
|
+
path: ["v2", "topics"]
|
|
1003
|
+
});
|
|
1004
|
+
}
|
|
1005
|
+
/**
|
|
1006
|
+
* Get a single url group
|
|
1007
|
+
*/
|
|
1008
|
+
async get(name) {
|
|
1009
|
+
return await this.http.request({
|
|
1010
|
+
method: "GET",
|
|
1011
|
+
path: ["v2", "topics", name]
|
|
1012
|
+
});
|
|
1013
|
+
}
|
|
1014
|
+
/**
|
|
1015
|
+
* Delete a url group
|
|
1016
|
+
*/
|
|
1017
|
+
async delete(name) {
|
|
1018
|
+
return await this.http.request({
|
|
1019
|
+
method: "DELETE",
|
|
1020
|
+
path: ["v2", "topics", name],
|
|
1021
|
+
parseResponseAsJson: false
|
|
1022
|
+
});
|
|
1023
|
+
}
|
|
1024
|
+
};
|
|
15
1025
|
|
|
1026
|
+
// node_modules/neverthrow/dist/index.es.js
|
|
1027
|
+
var defaultErrorConfig = {
|
|
1028
|
+
withStackTrace: false
|
|
1029
|
+
};
|
|
1030
|
+
var createNeverThrowError = (message, result, config = defaultErrorConfig) => {
|
|
1031
|
+
const data = result.isOk() ? { type: "Ok", value: result.value } : { type: "Err", value: result.error };
|
|
1032
|
+
const maybeStack = config.withStackTrace ? new Error().stack : void 0;
|
|
1033
|
+
return {
|
|
1034
|
+
data,
|
|
1035
|
+
message,
|
|
1036
|
+
stack: maybeStack
|
|
1037
|
+
};
|
|
1038
|
+
};
|
|
1039
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
1040
|
+
function adopt(value) {
|
|
1041
|
+
return value instanceof P ? value : new P(function(resolve) {
|
|
1042
|
+
resolve(value);
|
|
1043
|
+
});
|
|
1044
|
+
}
|
|
1045
|
+
return new (P || (P = Promise))(function(resolve, reject) {
|
|
1046
|
+
function fulfilled(value) {
|
|
1047
|
+
try {
|
|
1048
|
+
step(generator.next(value));
|
|
1049
|
+
} catch (e) {
|
|
1050
|
+
reject(e);
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
function rejected(value) {
|
|
1054
|
+
try {
|
|
1055
|
+
step(generator["throw"](value));
|
|
1056
|
+
} catch (e) {
|
|
1057
|
+
reject(e);
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
function step(result) {
|
|
1061
|
+
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
|
|
1062
|
+
}
|
|
1063
|
+
step((generator = generator.apply(thisArg, [])).next());
|
|
1064
|
+
});
|
|
1065
|
+
}
|
|
1066
|
+
function __values(o) {
|
|
1067
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
1068
|
+
if (m)
|
|
1069
|
+
return m.call(o);
|
|
1070
|
+
if (o && typeof o.length === "number")
|
|
1071
|
+
return {
|
|
1072
|
+
next: function() {
|
|
1073
|
+
if (o && i >= o.length)
|
|
1074
|
+
o = void 0;
|
|
1075
|
+
return { value: o && o[i++], done: !o };
|
|
1076
|
+
}
|
|
1077
|
+
};
|
|
1078
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
1079
|
+
}
|
|
1080
|
+
function __await(v) {
|
|
1081
|
+
return this instanceof __await ? (this.v = v, this) : new __await(v);
|
|
1082
|
+
}
|
|
1083
|
+
function __asyncGenerator(thisArg, _arguments, generator) {
|
|
1084
|
+
if (!Symbol.asyncIterator)
|
|
1085
|
+
throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
1086
|
+
var g = generator.apply(thisArg, _arguments || []), i, q = [];
|
|
1087
|
+
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function() {
|
|
1088
|
+
return this;
|
|
1089
|
+
}, i;
|
|
1090
|
+
function verb(n) {
|
|
1091
|
+
if (g[n])
|
|
1092
|
+
i[n] = function(v) {
|
|
1093
|
+
return new Promise(function(a, b) {
|
|
1094
|
+
q.push([n, v, a, b]) > 1 || resume(n, v);
|
|
1095
|
+
});
|
|
1096
|
+
};
|
|
1097
|
+
}
|
|
1098
|
+
function resume(n, v) {
|
|
1099
|
+
try {
|
|
1100
|
+
step(g[n](v));
|
|
1101
|
+
} catch (e) {
|
|
1102
|
+
settle(q[0][3], e);
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
function step(r) {
|
|
1106
|
+
r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r);
|
|
1107
|
+
}
|
|
1108
|
+
function fulfill(value) {
|
|
1109
|
+
resume("next", value);
|
|
1110
|
+
}
|
|
1111
|
+
function reject(value) {
|
|
1112
|
+
resume("throw", value);
|
|
1113
|
+
}
|
|
1114
|
+
function settle(f, v) {
|
|
1115
|
+
if (f(v), q.shift(), q.length)
|
|
1116
|
+
resume(q[0][0], q[0][1]);
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
function __asyncDelegator(o) {
|
|
1120
|
+
var i, p;
|
|
1121
|
+
return i = {}, verb("next"), verb("throw", function(e) {
|
|
1122
|
+
throw e;
|
|
1123
|
+
}), verb("return"), i[Symbol.iterator] = function() {
|
|
1124
|
+
return this;
|
|
1125
|
+
}, i;
|
|
1126
|
+
function verb(n, f) {
|
|
1127
|
+
i[n] = o[n] ? function(v) {
|
|
1128
|
+
return (p = !p) ? { value: __await(o[n](v)), done: n === "return" } : f ? f(v) : v;
|
|
1129
|
+
} : f;
|
|
1130
|
+
}
|
|
1131
|
+
}
|
|
1132
|
+
function __asyncValues(o) {
|
|
1133
|
+
if (!Symbol.asyncIterator)
|
|
1134
|
+
throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
1135
|
+
var m = o[Symbol.asyncIterator], i;
|
|
1136
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function() {
|
|
1137
|
+
return this;
|
|
1138
|
+
}, i);
|
|
1139
|
+
function verb(n) {
|
|
1140
|
+
i[n] = o[n] && function(v) {
|
|
1141
|
+
return new Promise(function(resolve, reject) {
|
|
1142
|
+
v = o[n](v), settle(resolve, reject, v.done, v.value);
|
|
1143
|
+
});
|
|
1144
|
+
};
|
|
1145
|
+
}
|
|
1146
|
+
function settle(resolve, reject, d, v) {
|
|
1147
|
+
Promise.resolve(v).then(function(v2) {
|
|
1148
|
+
resolve({ value: v2, done: d });
|
|
1149
|
+
}, reject);
|
|
1150
|
+
}
|
|
1151
|
+
}
|
|
1152
|
+
var ResultAsync = class _ResultAsync {
|
|
1153
|
+
constructor(res) {
|
|
1154
|
+
this._promise = res;
|
|
1155
|
+
}
|
|
1156
|
+
static fromSafePromise(promise) {
|
|
1157
|
+
const newPromise = promise.then((value) => new Ok(value));
|
|
1158
|
+
return new _ResultAsync(newPromise);
|
|
1159
|
+
}
|
|
1160
|
+
static fromPromise(promise, errorFn) {
|
|
1161
|
+
const newPromise = promise.then((value) => new Ok(value)).catch((e) => new Err(errorFn(e)));
|
|
1162
|
+
return new _ResultAsync(newPromise);
|
|
1163
|
+
}
|
|
1164
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1165
|
+
static fromThrowable(fn, errorFn) {
|
|
1166
|
+
return (...args) => {
|
|
1167
|
+
return new _ResultAsync((() => __awaiter(this, void 0, void 0, function* () {
|
|
1168
|
+
try {
|
|
1169
|
+
return new Ok(yield fn(...args));
|
|
1170
|
+
} catch (error) {
|
|
1171
|
+
return new Err(errorFn ? errorFn(error) : error);
|
|
1172
|
+
}
|
|
1173
|
+
}))());
|
|
1174
|
+
};
|
|
1175
|
+
}
|
|
1176
|
+
static combine(asyncResultList) {
|
|
1177
|
+
return combineResultAsyncList(asyncResultList);
|
|
1178
|
+
}
|
|
1179
|
+
static combineWithAllErrors(asyncResultList) {
|
|
1180
|
+
return combineResultAsyncListWithAllErrors(asyncResultList);
|
|
1181
|
+
}
|
|
1182
|
+
map(f) {
|
|
1183
|
+
return new _ResultAsync(this._promise.then((res) => __awaiter(this, void 0, void 0, function* () {
|
|
1184
|
+
if (res.isErr()) {
|
|
1185
|
+
return new Err(res.error);
|
|
1186
|
+
}
|
|
1187
|
+
return new Ok(yield f(res.value));
|
|
1188
|
+
})));
|
|
1189
|
+
}
|
|
1190
|
+
mapErr(f) {
|
|
1191
|
+
return new _ResultAsync(this._promise.then((res) => __awaiter(this, void 0, void 0, function* () {
|
|
1192
|
+
if (res.isOk()) {
|
|
1193
|
+
return new Ok(res.value);
|
|
1194
|
+
}
|
|
1195
|
+
return new Err(yield f(res.error));
|
|
1196
|
+
})));
|
|
1197
|
+
}
|
|
1198
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
|
|
1199
|
+
andThen(f) {
|
|
1200
|
+
return new _ResultAsync(this._promise.then((res) => {
|
|
1201
|
+
if (res.isErr()) {
|
|
1202
|
+
return new Err(res.error);
|
|
1203
|
+
}
|
|
1204
|
+
const newValue = f(res.value);
|
|
1205
|
+
return newValue instanceof _ResultAsync ? newValue._promise : newValue;
|
|
1206
|
+
}));
|
|
1207
|
+
}
|
|
1208
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
|
|
1209
|
+
orElse(f) {
|
|
1210
|
+
return new _ResultAsync(this._promise.then((res) => __awaiter(this, void 0, void 0, function* () {
|
|
1211
|
+
if (res.isErr()) {
|
|
1212
|
+
return f(res.error);
|
|
1213
|
+
}
|
|
1214
|
+
return new Ok(res.value);
|
|
1215
|
+
})));
|
|
1216
|
+
}
|
|
1217
|
+
match(ok2, _err) {
|
|
1218
|
+
return this._promise.then((res) => res.match(ok2, _err));
|
|
1219
|
+
}
|
|
1220
|
+
unwrapOr(t) {
|
|
1221
|
+
return this._promise.then((res) => res.unwrapOr(t));
|
|
1222
|
+
}
|
|
1223
|
+
/**
|
|
1224
|
+
* Emulates Rust's `?` operator in `safeTry`'s body. See also `safeTry`.
|
|
1225
|
+
*/
|
|
1226
|
+
safeUnwrap() {
|
|
1227
|
+
return __asyncGenerator(this, arguments, function* safeUnwrap_1() {
|
|
1228
|
+
return yield __await(yield __await(yield* __asyncDelegator(__asyncValues(yield __await(this._promise.then((res) => res.safeUnwrap()))))));
|
|
1229
|
+
});
|
|
1230
|
+
}
|
|
1231
|
+
// Makes ResultAsync implement PromiseLike<Result>
|
|
1232
|
+
then(successCallback, failureCallback) {
|
|
1233
|
+
return this._promise.then(successCallback, failureCallback);
|
|
1234
|
+
}
|
|
1235
|
+
};
|
|
1236
|
+
var errAsync = (err2) => new ResultAsync(Promise.resolve(new Err(err2)));
|
|
1237
|
+
var fromPromise = ResultAsync.fromPromise;
|
|
1238
|
+
var fromSafePromise = ResultAsync.fromSafePromise;
|
|
1239
|
+
var fromAsyncThrowable = ResultAsync.fromThrowable;
|
|
1240
|
+
var combineResultList = (resultList) => {
|
|
1241
|
+
let acc = ok([]);
|
|
1242
|
+
for (const result of resultList) {
|
|
1243
|
+
if (result.isErr()) {
|
|
1244
|
+
acc = err(result.error);
|
|
1245
|
+
break;
|
|
1246
|
+
} else {
|
|
1247
|
+
acc.map((list) => list.push(result.value));
|
|
1248
|
+
}
|
|
1249
|
+
}
|
|
1250
|
+
return acc;
|
|
1251
|
+
};
|
|
1252
|
+
var combineResultAsyncList = (asyncResultList) => ResultAsync.fromSafePromise(Promise.all(asyncResultList)).andThen(combineResultList);
|
|
1253
|
+
var combineResultListWithAllErrors = (resultList) => {
|
|
1254
|
+
let acc = ok([]);
|
|
1255
|
+
for (const result of resultList) {
|
|
1256
|
+
if (result.isErr() && acc.isErr()) {
|
|
1257
|
+
acc.error.push(result.error);
|
|
1258
|
+
} else if (result.isErr() && acc.isOk()) {
|
|
1259
|
+
acc = err([result.error]);
|
|
1260
|
+
} else if (result.isOk() && acc.isOk()) {
|
|
1261
|
+
acc.value.push(result.value);
|
|
1262
|
+
}
|
|
1263
|
+
}
|
|
1264
|
+
return acc;
|
|
1265
|
+
};
|
|
1266
|
+
var combineResultAsyncListWithAllErrors = (asyncResultList) => ResultAsync.fromSafePromise(Promise.all(asyncResultList)).andThen(combineResultListWithAllErrors);
|
|
1267
|
+
var Result;
|
|
1268
|
+
(function(Result2) {
|
|
1269
|
+
function fromThrowable2(fn, errorFn) {
|
|
1270
|
+
return (...args) => {
|
|
1271
|
+
try {
|
|
1272
|
+
const result = fn(...args);
|
|
1273
|
+
return ok(result);
|
|
1274
|
+
} catch (e) {
|
|
1275
|
+
return err(errorFn ? errorFn(e) : e);
|
|
1276
|
+
}
|
|
1277
|
+
};
|
|
1278
|
+
}
|
|
1279
|
+
Result2.fromThrowable = fromThrowable2;
|
|
1280
|
+
function combine(resultList) {
|
|
1281
|
+
return combineResultList(resultList);
|
|
1282
|
+
}
|
|
1283
|
+
Result2.combine = combine;
|
|
1284
|
+
function combineWithAllErrors(resultList) {
|
|
1285
|
+
return combineResultListWithAllErrors(resultList);
|
|
1286
|
+
}
|
|
1287
|
+
Result2.combineWithAllErrors = combineWithAllErrors;
|
|
1288
|
+
})(Result || (Result = {}));
|
|
1289
|
+
var ok = (value) => new Ok(value);
|
|
1290
|
+
var err = (err2) => new Err(err2);
|
|
1291
|
+
var Ok = class {
|
|
1292
|
+
constructor(value) {
|
|
1293
|
+
this.value = value;
|
|
1294
|
+
}
|
|
1295
|
+
isOk() {
|
|
1296
|
+
return true;
|
|
1297
|
+
}
|
|
1298
|
+
isErr() {
|
|
1299
|
+
return !this.isOk();
|
|
1300
|
+
}
|
|
1301
|
+
map(f) {
|
|
1302
|
+
return ok(f(this.value));
|
|
1303
|
+
}
|
|
1304
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1305
|
+
mapErr(_f) {
|
|
1306
|
+
return ok(this.value);
|
|
1307
|
+
}
|
|
1308
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
|
|
1309
|
+
andThen(f) {
|
|
1310
|
+
return f(this.value);
|
|
1311
|
+
}
|
|
1312
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
|
|
1313
|
+
orElse(_f) {
|
|
1314
|
+
return ok(this.value);
|
|
1315
|
+
}
|
|
1316
|
+
asyncAndThen(f) {
|
|
1317
|
+
return f(this.value);
|
|
1318
|
+
}
|
|
1319
|
+
asyncMap(f) {
|
|
1320
|
+
return ResultAsync.fromSafePromise(f(this.value));
|
|
1321
|
+
}
|
|
1322
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1323
|
+
unwrapOr(_v) {
|
|
1324
|
+
return this.value;
|
|
1325
|
+
}
|
|
1326
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1327
|
+
match(ok2, _err) {
|
|
1328
|
+
return ok2(this.value);
|
|
1329
|
+
}
|
|
1330
|
+
safeUnwrap() {
|
|
1331
|
+
const value = this.value;
|
|
1332
|
+
return function* () {
|
|
1333
|
+
return value;
|
|
1334
|
+
}();
|
|
1335
|
+
}
|
|
1336
|
+
_unsafeUnwrap(_) {
|
|
1337
|
+
return this.value;
|
|
1338
|
+
}
|
|
1339
|
+
_unsafeUnwrapErr(config) {
|
|
1340
|
+
throw createNeverThrowError("Called `_unsafeUnwrapErr` on an Ok", this, config);
|
|
1341
|
+
}
|
|
1342
|
+
};
|
|
1343
|
+
var Err = class {
|
|
1344
|
+
constructor(error) {
|
|
1345
|
+
this.error = error;
|
|
1346
|
+
}
|
|
1347
|
+
isOk() {
|
|
1348
|
+
return false;
|
|
1349
|
+
}
|
|
1350
|
+
isErr() {
|
|
1351
|
+
return !this.isOk();
|
|
1352
|
+
}
|
|
1353
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1354
|
+
map(_f) {
|
|
1355
|
+
return err(this.error);
|
|
1356
|
+
}
|
|
1357
|
+
mapErr(f) {
|
|
1358
|
+
return err(f(this.error));
|
|
1359
|
+
}
|
|
1360
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
|
|
1361
|
+
andThen(_f) {
|
|
1362
|
+
return err(this.error);
|
|
1363
|
+
}
|
|
1364
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
|
|
1365
|
+
orElse(f) {
|
|
1366
|
+
return f(this.error);
|
|
1367
|
+
}
|
|
1368
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1369
|
+
asyncAndThen(_f) {
|
|
1370
|
+
return errAsync(this.error);
|
|
1371
|
+
}
|
|
1372
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1373
|
+
asyncMap(_f) {
|
|
1374
|
+
return errAsync(this.error);
|
|
1375
|
+
}
|
|
1376
|
+
unwrapOr(v) {
|
|
1377
|
+
return v;
|
|
1378
|
+
}
|
|
1379
|
+
match(_ok, err2) {
|
|
1380
|
+
return err2(this.error);
|
|
1381
|
+
}
|
|
1382
|
+
safeUnwrap() {
|
|
1383
|
+
const error = this.error;
|
|
1384
|
+
return function* () {
|
|
1385
|
+
yield err(error);
|
|
1386
|
+
throw new Error("Do not use this generator out of `safeTry`");
|
|
1387
|
+
}();
|
|
1388
|
+
}
|
|
1389
|
+
_unsafeUnwrap(config) {
|
|
1390
|
+
throw createNeverThrowError("Called `_unsafeUnwrap` on an Err", this, config);
|
|
1391
|
+
}
|
|
1392
|
+
_unsafeUnwrapErr(_) {
|
|
1393
|
+
return this.error;
|
|
1394
|
+
}
|
|
1395
|
+
};
|
|
1396
|
+
var fromThrowable = Result.fromThrowable;
|
|
16
1397
|
|
|
1398
|
+
// src/client/workflow/index.ts
|
|
1399
|
+
var Workflow = class {
|
|
1400
|
+
http;
|
|
1401
|
+
constructor(http) {
|
|
1402
|
+
this.http = http;
|
|
1403
|
+
}
|
|
1404
|
+
/**
|
|
1405
|
+
* Cancel an ongoing workflow
|
|
1406
|
+
*
|
|
1407
|
+
* @param workflowRunId run id of the workflow to delete
|
|
1408
|
+
* @returns true if workflow is succesfully deleted. Otherwise throws QStashError
|
|
1409
|
+
*/
|
|
1410
|
+
async cancel(workflowRunId) {
|
|
1411
|
+
const result = await this.http.request({
|
|
1412
|
+
path: ["v2", "workflows", "runs", `${workflowRunId}?cancel=true`],
|
|
1413
|
+
method: "DELETE",
|
|
1414
|
+
parseResponseAsJson: false
|
|
1415
|
+
});
|
|
1416
|
+
return result ?? true;
|
|
1417
|
+
}
|
|
1418
|
+
};
|
|
17
1419
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
1420
|
+
// src/client/client.ts
|
|
1421
|
+
var Client = class {
|
|
1422
|
+
http;
|
|
1423
|
+
token;
|
|
1424
|
+
constructor(config) {
|
|
1425
|
+
this.http = new HttpClient({
|
|
1426
|
+
retry: config.retry,
|
|
1427
|
+
baseUrl: config.baseUrl ? config.baseUrl.replace(/\/$/, "") : "https://qstash.upstash.io",
|
|
1428
|
+
authorization: `Bearer ${config.token}`
|
|
1429
|
+
});
|
|
1430
|
+
this.token = config.token;
|
|
1431
|
+
}
|
|
1432
|
+
/**
|
|
1433
|
+
* Access the urlGroup API.
|
|
1434
|
+
*
|
|
1435
|
+
* Create, read, update or delete urlGroups.
|
|
1436
|
+
*/
|
|
1437
|
+
get urlGroups() {
|
|
1438
|
+
return new UrlGroups(this.http);
|
|
1439
|
+
}
|
|
1440
|
+
/**
|
|
1441
|
+
* Deprecated. Use urlGroups instead.
|
|
1442
|
+
*
|
|
1443
|
+
* Access the topic API.
|
|
1444
|
+
*
|
|
1445
|
+
* Create, read, update or delete topics.
|
|
1446
|
+
*/
|
|
1447
|
+
get topics() {
|
|
1448
|
+
return this.urlGroups;
|
|
1449
|
+
}
|
|
1450
|
+
/**
|
|
1451
|
+
* Access the dlq API.
|
|
1452
|
+
*
|
|
1453
|
+
* List or remove messages from the DLQ.
|
|
1454
|
+
*/
|
|
1455
|
+
get dlq() {
|
|
1456
|
+
return new DLQ(this.http);
|
|
1457
|
+
}
|
|
1458
|
+
/**
|
|
1459
|
+
* Access the message API.
|
|
1460
|
+
*
|
|
1461
|
+
* Read or cancel messages.
|
|
1462
|
+
*/
|
|
1463
|
+
get messages() {
|
|
1464
|
+
return new Messages(this.http);
|
|
1465
|
+
}
|
|
1466
|
+
/**
|
|
1467
|
+
* Access the schedule API.
|
|
1468
|
+
*
|
|
1469
|
+
* Create, read or delete schedules.
|
|
1470
|
+
*/
|
|
1471
|
+
get schedules() {
|
|
1472
|
+
return new Schedules(this.http);
|
|
1473
|
+
}
|
|
1474
|
+
/**
|
|
1475
|
+
* Access the workflow API.
|
|
1476
|
+
*
|
|
1477
|
+
* cancel workflows.
|
|
1478
|
+
*/
|
|
1479
|
+
get workflow() {
|
|
1480
|
+
return new Workflow(this.http);
|
|
1481
|
+
}
|
|
1482
|
+
/**
|
|
1483
|
+
* Access the queue API.
|
|
1484
|
+
*
|
|
1485
|
+
* Create, read, update or delete queues.
|
|
1486
|
+
*/
|
|
1487
|
+
queue(request) {
|
|
1488
|
+
return new Queue(this.http, request?.queueName);
|
|
1489
|
+
}
|
|
1490
|
+
/**
|
|
1491
|
+
* Access the Chat API
|
|
1492
|
+
*
|
|
1493
|
+
* Call the create or prompt methods
|
|
1494
|
+
*/
|
|
1495
|
+
chat() {
|
|
1496
|
+
return new Chat(this.http, this.token);
|
|
1497
|
+
}
|
|
1498
|
+
async publish(request) {
|
|
1499
|
+
const headers = processHeaders(request);
|
|
1500
|
+
const response = await this.http.request({
|
|
1501
|
+
path: ["v2", "publish", getRequestPath(request)],
|
|
1502
|
+
body: request.body,
|
|
1503
|
+
headers,
|
|
1504
|
+
method: "POST"
|
|
1505
|
+
});
|
|
1506
|
+
return response;
|
|
1507
|
+
}
|
|
1508
|
+
/**
|
|
1509
|
+
* publishJSON is a utility wrapper around `publish` that automatically serializes the body
|
|
1510
|
+
* and sets the `Content-Type` header to `application/json`.
|
|
1511
|
+
*/
|
|
1512
|
+
async publishJSON(request) {
|
|
1513
|
+
const headers = prefixHeaders(new Headers(request.headers));
|
|
1514
|
+
headers.set("Content-Type", "application/json");
|
|
1515
|
+
ensureCallbackPresent(request);
|
|
1516
|
+
appendLLMOptionsIfNeeded(request, headers, this.http);
|
|
1517
|
+
const response = await this.publish({
|
|
1518
|
+
...request,
|
|
1519
|
+
headers,
|
|
1520
|
+
body: JSON.stringify(request.body)
|
|
1521
|
+
});
|
|
1522
|
+
return response;
|
|
1523
|
+
}
|
|
1524
|
+
/**
|
|
1525
|
+
* Batch publish messages to QStash.
|
|
1526
|
+
*/
|
|
1527
|
+
async batch(request) {
|
|
1528
|
+
const messages = [];
|
|
1529
|
+
for (const message of request) {
|
|
1530
|
+
const headers = processHeaders(message);
|
|
1531
|
+
const headerEntries = Object.fromEntries(headers.entries());
|
|
1532
|
+
messages.push({
|
|
1533
|
+
destination: getRequestPath(message),
|
|
1534
|
+
headers: headerEntries,
|
|
1535
|
+
body: message.body,
|
|
1536
|
+
...message.queueName && { queue: message.queueName }
|
|
1537
|
+
});
|
|
1538
|
+
}
|
|
1539
|
+
const response = await this.http.request({
|
|
1540
|
+
path: ["v2", "batch"],
|
|
1541
|
+
body: JSON.stringify(messages),
|
|
1542
|
+
headers: {
|
|
1543
|
+
"Content-Type": "application/json"
|
|
1544
|
+
},
|
|
1545
|
+
method: "POST"
|
|
1546
|
+
});
|
|
1547
|
+
const arrayResposne = Array.isArray(response) ? response : [response];
|
|
1548
|
+
return arrayResposne;
|
|
1549
|
+
}
|
|
1550
|
+
/**
|
|
1551
|
+
* Batch publish messages to QStash, serializing each body to JSON.
|
|
1552
|
+
*/
|
|
1553
|
+
async batchJSON(request) {
|
|
1554
|
+
for (const message of request) {
|
|
1555
|
+
if ("body" in message) {
|
|
1556
|
+
message.body = JSON.stringify(message.body);
|
|
1557
|
+
}
|
|
1558
|
+
message.headers = new Headers(message.headers);
|
|
1559
|
+
ensureCallbackPresent(message);
|
|
1560
|
+
appendLLMOptionsIfNeeded(message, message.headers, this.http);
|
|
1561
|
+
message.headers.set("Content-Type", "application/json");
|
|
1562
|
+
}
|
|
1563
|
+
const response = await this.batch(request);
|
|
1564
|
+
return response;
|
|
1565
|
+
}
|
|
1566
|
+
/**
|
|
1567
|
+
* Retrieve your logs.
|
|
1568
|
+
*
|
|
1569
|
+
* The logs endpoint is paginated and returns only 100 logs at a time.
|
|
1570
|
+
* If you want to receive more logs, you can use the cursor to paginate.
|
|
1571
|
+
*
|
|
1572
|
+
* The cursor is a unix timestamp with millisecond precision
|
|
1573
|
+
*
|
|
1574
|
+
* @example
|
|
1575
|
+
* ```ts
|
|
1576
|
+
* let cursor = Date.now()
|
|
1577
|
+
* const logs: Log[] = []
|
|
1578
|
+
* while (cursor > 0) {
|
|
1579
|
+
* const res = await qstash.logs({ cursor })
|
|
1580
|
+
* logs.push(...res.logs)
|
|
1581
|
+
* cursor = res.cursor ?? 0
|
|
1582
|
+
* }
|
|
1583
|
+
* ```
|
|
1584
|
+
*/
|
|
1585
|
+
async events(request) {
|
|
1586
|
+
const query = {};
|
|
1587
|
+
if (typeof request?.cursor === "number" && request.cursor > 0) {
|
|
1588
|
+
query.cursor = request.cursor.toString();
|
|
1589
|
+
} else if (typeof request?.cursor === "string" && request.cursor !== "") {
|
|
1590
|
+
query.cursor = request.cursor;
|
|
1591
|
+
}
|
|
1592
|
+
for (const [key, value] of Object.entries(request?.filter ?? {})) {
|
|
1593
|
+
if (typeof value === "number" && value < 0) {
|
|
1594
|
+
continue;
|
|
1595
|
+
}
|
|
1596
|
+
if (key === "urlGroup") {
|
|
1597
|
+
query.topicName = value.toString();
|
|
1598
|
+
} else if (typeof value !== "undefined") {
|
|
1599
|
+
query[key] = value.toString();
|
|
1600
|
+
}
|
|
1601
|
+
}
|
|
1602
|
+
const responsePayload = await this.http.request({
|
|
1603
|
+
path: ["v2", "events"],
|
|
1604
|
+
method: "GET",
|
|
1605
|
+
query
|
|
1606
|
+
});
|
|
1607
|
+
return {
|
|
1608
|
+
cursor: responsePayload.cursor,
|
|
1609
|
+
events: responsePayload.events.map((event) => {
|
|
1610
|
+
return {
|
|
1611
|
+
...event,
|
|
1612
|
+
urlGroup: event.topicName
|
|
1613
|
+
};
|
|
1614
|
+
})
|
|
1615
|
+
};
|
|
1616
|
+
}
|
|
1617
|
+
};
|
|
1618
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1619
|
+
0 && (module.exports = {
|
|
1620
|
+
Chat,
|
|
1621
|
+
Client,
|
|
1622
|
+
Messages,
|
|
1623
|
+
QStashWorkflowAbort,
|
|
1624
|
+
QStashWorkflowError,
|
|
1625
|
+
QstashChatRatelimitError,
|
|
1626
|
+
QstashDailyRatelimitError,
|
|
1627
|
+
QstashError,
|
|
1628
|
+
QstashRatelimitError,
|
|
1629
|
+
Receiver,
|
|
1630
|
+
Schedules,
|
|
1631
|
+
SignatureError,
|
|
1632
|
+
UrlGroups,
|
|
1633
|
+
custom,
|
|
1634
|
+
decodeBase64,
|
|
1635
|
+
formatWorkflowError,
|
|
1636
|
+
openai,
|
|
1637
|
+
setupAnalytics,
|
|
1638
|
+
upstash
|
|
1639
|
+
});
|