@upstash/qstash 2.4.3 → 2.5.1-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/dist/index.js DELETED
@@ -1,512 +0,0 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
-
3
-
4
-
5
-
6
-
7
- var _chunkEROSIHWEjs = require('./chunk-EROSIHWE.js');
8
-
9
- // src/client/dlq.ts
10
- var DLQ = class {
11
- constructor(http) {
12
- this.http = http;
13
- }
14
- /**
15
- * List messages in the dlq
16
- */
17
- listMessages(opts) {
18
- return _chunkEROSIHWEjs.__async.call(void 0, this, null, function* () {
19
- return yield this.http.request({
20
- method: "GET",
21
- path: ["v2", "dlq"],
22
- query: { cursor: opts == null ? void 0 : opts.cursor }
23
- });
24
- });
25
- }
26
- /**
27
- * Remove a message from the dlq using it's `dlqId`
28
- */
29
- delete(dlqMessageId) {
30
- return _chunkEROSIHWEjs.__async.call(void 0, this, null, function* () {
31
- return yield this.http.request({
32
- method: "DELETE",
33
- path: ["v2", "dlq", dlqMessageId],
34
- parseResponseAsJson: false
35
- // there is no response
36
- });
37
- });
38
- }
39
- /**
40
- * Remove multiple messages from the dlq using their `dlqId`s
41
- */
42
- deleteMany(req) {
43
- return _chunkEROSIHWEjs.__async.call(void 0, this, null, function* () {
44
- return yield this.http.request({
45
- method: "DELETE",
46
- path: ["v2", "dlq"],
47
- headers: { "Content-Type": "application/json" },
48
- body: JSON.stringify({ dlqIds: req.dlqIds })
49
- });
50
- });
51
- }
52
- };
53
-
54
- // src/client/error.ts
55
- var QstashError = class extends Error {
56
- constructor(message) {
57
- super(message);
58
- this.name = "QstashError";
59
- }
60
- };
61
- var QstashRatelimitError = class extends QstashError {
62
- constructor(args) {
63
- super(`You have been ratelimited. ${JSON.stringify(args)} `);
64
- }
65
- };
66
-
67
- // src/client/http.ts
68
- var HttpClient = class {
69
- constructor(config) {
70
- var _a, _b, _c;
71
- this.baseUrl = config.baseUrl.replace(/\/$/, "");
72
- this.authorization = config.authorization;
73
- if (typeof (config == null ? void 0 : config.retry) === "boolean" && (config == null ? void 0 : config.retry) === false) {
74
- this.retry = {
75
- attempts: 1,
76
- backoff: () => 0
77
- };
78
- } else {
79
- this.retry = {
80
- attempts: ((_a = config.retry) == null ? void 0 : _a.retries) ? config.retry.retries + 1 : 5,
81
- backoff: (_c = (_b = config.retry) == null ? void 0 : _b.backoff) != null ? _c : (retryCount) => Math.exp(retryCount) * 50
82
- };
83
- }
84
- }
85
- request(req) {
86
- return _chunkEROSIHWEjs.__async.call(void 0, this, null, function* () {
87
- var _a;
88
- const headers = new Headers(req.headers);
89
- headers.set("Authorization", this.authorization);
90
- const requestOptions = {
91
- method: req.method,
92
- headers,
93
- body: req.body,
94
- keepalive: req.keepalive
95
- };
96
- const url = new URL([this.baseUrl, ...(_a = req.path) != null ? _a : []].join("/"));
97
- if (req.query) {
98
- for (const [key, value] of Object.entries(req.query)) {
99
- if (typeof value !== "undefined") {
100
- url.searchParams.set(key, value.toString());
101
- }
102
- }
103
- }
104
- let res = null;
105
- let error = null;
106
- for (let i = 0; i < this.retry.attempts; i++) {
107
- try {
108
- res = yield fetch(url.toString(), requestOptions);
109
- break;
110
- } catch (err) {
111
- error = err;
112
- yield new Promise((r) => setTimeout(r, this.retry.backoff(i)));
113
- }
114
- }
115
- if (!res) {
116
- throw error != null ? error : new Error("Exhausted all retries");
117
- }
118
- if (res.status === 429) {
119
- throw new QstashRatelimitError({
120
- limit: res.headers.get("Burst-RateLimit-Limit"),
121
- remaining: res.headers.get("Burst-RateLimit-Remaining"),
122
- reset: res.headers.get("Burst-RateLimit-Reset")
123
- });
124
- }
125
- if (res.status < 200 || res.status >= 300) {
126
- const body = yield res.text();
127
- throw new QstashError(body.length > 0 ? body : `Error: status=${res.status}`);
128
- }
129
- if (req.parseResponseAsJson === false) {
130
- return void 0;
131
- } else {
132
- return yield res.json();
133
- }
134
- });
135
- }
136
- };
137
-
138
- // src/client/messages.ts
139
- var Messages = class {
140
- constructor(http) {
141
- this.http = http;
142
- }
143
- /**
144
- * Get a message
145
- */
146
- get(messageId) {
147
- return _chunkEROSIHWEjs.__async.call(void 0, this, null, function* () {
148
- return yield this.http.request({
149
- method: "GET",
150
- path: ["v2", "messages", messageId]
151
- });
152
- });
153
- }
154
- /**
155
- * Cancel a message
156
- */
157
- delete(messageId) {
158
- return _chunkEROSIHWEjs.__async.call(void 0, this, null, function* () {
159
- return yield this.http.request({
160
- method: "DELETE",
161
- path: ["v2", "messages", messageId],
162
- parseResponseAsJson: false
163
- });
164
- });
165
- }
166
- };
167
-
168
- // src/client/utils.ts
169
- function prefixHeaders(headers) {
170
- const isIgnoredHeader = (header) => {
171
- const lowerCaseHeader = header.toLowerCase();
172
- return lowerCaseHeader.startsWith("content-type") || lowerCaseHeader.startsWith("upstash-");
173
- };
174
- const keysToBePrefixed = Array.from(headers.keys()).filter(
175
- (key) => !isIgnoredHeader(key)
176
- );
177
- for (const key of keysToBePrefixed) {
178
- const value = headers.get(key);
179
- if (value !== null) {
180
- headers.set(`Upstash-Forward-${key}`, value);
181
- }
182
- headers.delete(key);
183
- }
184
- return headers;
185
- }
186
-
187
- // src/client/schedules.ts
188
- var Schedules = class {
189
- constructor(http) {
190
- this.http = http;
191
- }
192
- /**
193
- * Create a schedule
194
- */
195
- create(req) {
196
- return _chunkEROSIHWEjs.__async.call(void 0, this, null, function* () {
197
- const headers = prefixHeaders(new Headers(req.headers));
198
- if (!headers.has("Content-Type")) {
199
- headers.set("Content-Type", "application/json");
200
- }
201
- headers.set("Upstash-Cron", req.cron);
202
- if (typeof req.method !== "undefined") {
203
- headers.set("Upstash-Method", req.method);
204
- }
205
- if (typeof req.delay !== "undefined") {
206
- headers.set("Upstash-Delay", `${req.delay.toFixed()}s`);
207
- }
208
- if (typeof req.retries !== "undefined") {
209
- headers.set("Upstash-Retries", req.retries.toFixed());
210
- }
211
- if (typeof req.callback !== "undefined") {
212
- headers.set("Upstash-Callback", req.callback);
213
- }
214
- if (typeof req.failureCallback !== "undefined") {
215
- headers.set("Upstash-Failure-Callback", req.failureCallback);
216
- }
217
- return yield this.http.request({
218
- method: "POST",
219
- headers,
220
- path: ["v2", "schedules", req.destination],
221
- body: req.body
222
- });
223
- });
224
- }
225
- /**
226
- * Get a schedule
227
- */
228
- get(scheduleId) {
229
- return _chunkEROSIHWEjs.__async.call(void 0, this, null, function* () {
230
- return yield this.http.request({
231
- method: "GET",
232
- path: ["v2", "schedules", scheduleId]
233
- });
234
- });
235
- }
236
- /**
237
- * List your schedules
238
- */
239
- list() {
240
- return _chunkEROSIHWEjs.__async.call(void 0, this, null, function* () {
241
- return yield this.http.request({
242
- method: "GET",
243
- path: ["v2", "schedules"]
244
- });
245
- });
246
- }
247
- /**
248
- * Delete a schedule
249
- */
250
- delete(scheduleId) {
251
- return _chunkEROSIHWEjs.__async.call(void 0, this, null, function* () {
252
- return yield this.http.request({
253
- method: "DELETE",
254
- path: ["v2", "schedules", scheduleId],
255
- parseResponseAsJson: false
256
- });
257
- });
258
- }
259
- };
260
-
261
- // src/client/topics.ts
262
- var Topics = class {
263
- constructor(http) {
264
- this.http = http;
265
- }
266
- /**
267
- * Create a new topic with the given name and endpoints
268
- */
269
- addEndpoints(req) {
270
- return _chunkEROSIHWEjs.__async.call(void 0, this, null, function* () {
271
- yield this.http.request({
272
- method: "POST",
273
- path: ["v2", "topics", req.name, "endpoints"],
274
- headers: { "Content-Type": "application/json" },
275
- body: JSON.stringify({ endpoints: req.endpoints }),
276
- parseResponseAsJson: false
277
- });
278
- });
279
- }
280
- /**
281
- * Remove endpoints from a topic.
282
- */
283
- removeEndpoints(req) {
284
- return _chunkEROSIHWEjs.__async.call(void 0, this, null, function* () {
285
- yield this.http.request({
286
- method: "DELETE",
287
- path: ["v2", "topics", req.name, "endpoints"],
288
- headers: { "Content-Type": "application/json" },
289
- body: JSON.stringify({ endpoints: req.endpoints }),
290
- parseResponseAsJson: false
291
- });
292
- });
293
- }
294
- /**
295
- * Get a list of all topics.
296
- */
297
- list() {
298
- return _chunkEROSIHWEjs.__async.call(void 0, this, null, function* () {
299
- return yield this.http.request({
300
- method: "GET",
301
- path: ["v2", "topics"]
302
- });
303
- });
304
- }
305
- /**
306
- * Get a single topic
307
- */
308
- get(name) {
309
- return _chunkEROSIHWEjs.__async.call(void 0, this, null, function* () {
310
- return yield this.http.request({
311
- method: "GET",
312
- path: ["v2", "topics", name]
313
- });
314
- });
315
- }
316
- /**
317
- * Delete a topic
318
- */
319
- delete(name) {
320
- return _chunkEROSIHWEjs.__async.call(void 0, this, null, function* () {
321
- return yield this.http.request({
322
- method: "DELETE",
323
- path: ["v2", "topics", name],
324
- parseResponseAsJson: false
325
- });
326
- });
327
- }
328
- };
329
-
330
- // src/client/client.ts
331
- var Client = class {
332
- constructor(config) {
333
- this.http = new HttpClient({
334
- retry: config.retry,
335
- baseUrl: config.baseUrl ? config.baseUrl.replace(/\/$/, "") : "https://qstash.upstash.io",
336
- authorization: `Bearer ${config.token}`
337
- });
338
- }
339
- /**
340
- * Access the topic API.
341
- *
342
- * Create, read, update or delete topics.
343
- */
344
- get topics() {
345
- return new Topics(this.http);
346
- }
347
- /**
348
- * Access the dlq API.
349
- *
350
- * List or remove messages from the DLQ.
351
- */
352
- get dlq() {
353
- return new DLQ(this.http);
354
- }
355
- /**
356
- * Access the message API.
357
- *
358
- * Read or cancel messages.
359
- */
360
- get messages() {
361
- return new Messages(this.http);
362
- }
363
- /**
364
- * Access the schedule API.
365
- *
366
- * Create, read or delete schedules.
367
- */
368
- get schedules() {
369
- return new Schedules(this.http);
370
- }
371
- processHeaders(req) {
372
- var _a;
373
- const headers = prefixHeaders(new Headers(req.headers));
374
- headers.set("Upstash-Method", (_a = req.method) != null ? _a : "POST");
375
- if (typeof req.delay !== "undefined") {
376
- headers.set("Upstash-Delay", `${req.delay.toFixed()}s`);
377
- }
378
- if (typeof req.notBefore !== "undefined") {
379
- headers.set("Upstash-Not-Before", req.notBefore.toFixed());
380
- }
381
- if (typeof req.deduplicationId !== "undefined") {
382
- headers.set("Upstash-Deduplication-Id", req.deduplicationId);
383
- }
384
- if (typeof req.contentBasedDeduplication !== "undefined") {
385
- headers.set("Upstash-Content-Based-Deduplication", "true");
386
- }
387
- if (typeof req.retries !== "undefined") {
388
- headers.set("Upstash-Retries", req.retries.toFixed());
389
- }
390
- if (typeof req.callback !== "undefined") {
391
- headers.set("Upstash-Callback", req.callback);
392
- }
393
- if (typeof req.failureCallback !== "undefined") {
394
- headers.set("Upstash-Failure-Callback", req.failureCallback);
395
- }
396
- return headers;
397
- }
398
- publish(req) {
399
- return _chunkEROSIHWEjs.__async.call(void 0, this, null, function* () {
400
- var _a;
401
- const headers = this.processHeaders(req);
402
- const res = yield this.http.request({
403
- path: ["v2", "publish", (_a = req.url) != null ? _a : req.topic],
404
- body: req.body,
405
- headers,
406
- method: "POST"
407
- });
408
- return res;
409
- });
410
- }
411
- /**
412
- * publishJSON is a utility wrapper around `publish` that automatically serializes the body
413
- * and sets the `Content-Type` header to `application/json`.
414
- */
415
- publishJSON(req) {
416
- return _chunkEROSIHWEjs.__async.call(void 0, this, null, function* () {
417
- const headers = prefixHeaders(new Headers(req.headers));
418
- headers.set("Content-Type", "application/json");
419
- const res = yield this.publish(_chunkEROSIHWEjs.__spreadProps.call(void 0, _chunkEROSIHWEjs.__spreadValues.call(void 0, {}, req), {
420
- headers,
421
- body: JSON.stringify(req.body)
422
- }));
423
- return res;
424
- });
425
- }
426
- /**
427
- * Batch publish messages to QStash.
428
- */
429
- batch(req) {
430
- return _chunkEROSIHWEjs.__async.call(void 0, this, null, function* () {
431
- var _a;
432
- const messages = [];
433
- for (const message of req) {
434
- const headers = this.processHeaders(message);
435
- const headerEntries = Object.fromEntries(headers.entries());
436
- messages.push({
437
- destination: (_a = message.url) != null ? _a : message.topic,
438
- headers: headerEntries,
439
- body: message.body
440
- });
441
- }
442
- const res = yield this.http.request({
443
- path: ["v2", "batch"],
444
- body: JSON.stringify(messages),
445
- headers: {
446
- "Content-Type": "application/json"
447
- },
448
- method: "POST"
449
- });
450
- return res;
451
- });
452
- }
453
- /**
454
- * Batch publish messages to QStash, serializing each body to JSON.
455
- */
456
- batchJSON(req) {
457
- return _chunkEROSIHWEjs.__async.call(void 0, this, null, function* () {
458
- for (const message of req) {
459
- if ("body" in message) {
460
- message.body = JSON.stringify(message.body);
461
- }
462
- message.headers = new Headers(message.headers);
463
- message.headers.set("Content-Type", "application/json");
464
- }
465
- const res = yield this.batch(req);
466
- return res;
467
- });
468
- }
469
- /**
470
- * Retrieve your logs.
471
- *
472
- * The logs endpoint is paginated and returns only 100 logs at a time.
473
- * If you want to receive more logs, you can use the cursor to paginate.
474
- *
475
- * The cursor is a unix timestamp with millisecond precision
476
- *
477
- * @example
478
- * ```ts
479
- * let cursor = Date.now()
480
- * const logs: Log[] = []
481
- * while (cursor > 0) {
482
- * const res = await qstash.logs({ cursor })
483
- * logs.push(...res.logs)
484
- * cursor = res.cursor ?? 0
485
- * }
486
- * ```
487
- */
488
- events(req) {
489
- return _chunkEROSIHWEjs.__async.call(void 0, this, null, function* () {
490
- const query = {};
491
- if ((req == null ? void 0 : req.cursor) && req.cursor > 0) {
492
- query.cursor = req.cursor;
493
- }
494
- const res = yield this.http.request({
495
- path: ["v2", "events"],
496
- method: "GET",
497
- query
498
- });
499
- return res;
500
- });
501
- }
502
- };
503
-
504
-
505
-
506
-
507
-
508
-
509
-
510
-
511
-
512
- exports.Client = Client; exports.Messages = Messages; exports.QstashError = QstashError; exports.QstashRatelimitError = QstashRatelimitError; exports.Receiver = _chunkEROSIHWEjs.Receiver; exports.Schedules = Schedules; exports.SignatureError = _chunkEROSIHWEjs.SignatureError; exports.Topics = Topics;