@upstash/qstash 0.0.3 → 0.0.7

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.
Files changed (44) hide show
  1. package/README.md +119 -1
  2. package/esm/_dnt.shims.js +65 -0
  3. package/esm/consumer.js +82 -0
  4. package/esm/deps/deno.land/std@0.144.0/encoding/base64.js +115 -0
  5. package/esm/deps/deno.land/std@0.144.0/encoding/base64url.js +42 -0
  6. package/esm/entrypoints/nextjs.js +49 -0
  7. package/package.json +15 -21
  8. package/script/_dnt.shims.js +70 -0
  9. package/script/consumer.js +110 -0
  10. package/script/deps/deno.land/std@0.144.0/encoding/base64.js +120 -0
  11. package/script/deps/deno.land/std@0.144.0/encoding/base64url.js +71 -0
  12. package/script/entrypoints/nextjs.js +53 -0
  13. package/types/_dnt.shims.d.ts +7 -0
  14. package/types/consumer.d.ts +43 -0
  15. package/types/deps/deno.land/std@0.144.0/encoding/base64.d.ts +11 -0
  16. package/types/deps/deno.land/std@0.144.0/encoding/base64url.d.ts +11 -0
  17. package/types/entrypoints/nextjs.d.ts +6 -0
  18. package/esm/client.js +0 -133
  19. package/esm/endpoints.js +0 -63
  20. package/esm/error.js +0 -9
  21. package/esm/http.js +0 -81
  22. package/esm/messages.js +0 -64
  23. package/esm/platforms/nodejs.js +0 -1
  24. package/esm/schedules.js +0 -41
  25. package/esm/topics.js +0 -71
  26. package/esm/types.js +0 -1
  27. package/script/client.js +0 -137
  28. package/script/endpoints.js +0 -67
  29. package/script/error.js +0 -13
  30. package/script/http.js +0 -85
  31. package/script/messages.js +0 -68
  32. package/script/platforms/nodejs.js +0 -5
  33. package/script/schedules.js +0 -45
  34. package/script/topics.js +0 -75
  35. package/script/types.js +0 -2
  36. package/types/client.d.ts +0 -198
  37. package/types/endpoints.d.ts +0 -59
  38. package/types/error.d.ts +0 -6
  39. package/types/http.d.ts +0 -65
  40. package/types/messages.d.ts +0 -66
  41. package/types/platforms/nodejs.d.ts +0 -1
  42. package/types/schedules.d.ts +0 -63
  43. package/types/topics.d.ts +0 -64
  44. package/types/types.d.ts +0 -20
package/esm/http.js DELETED
@@ -1,81 +0,0 @@
1
- import { QstashError } from "./error.js";
2
- export class HttpClient {
3
- constructor(config) {
4
- Object.defineProperty(this, "baseUrl", {
5
- enumerable: true,
6
- configurable: true,
7
- writable: true,
8
- value: void 0
9
- });
10
- Object.defineProperty(this, "authorization", {
11
- enumerable: true,
12
- configurable: true,
13
- writable: true,
14
- value: void 0
15
- });
16
- Object.defineProperty(this, "options", {
17
- enumerable: true,
18
- configurable: true,
19
- writable: true,
20
- value: void 0
21
- });
22
- Object.defineProperty(this, "retry", {
23
- enumerable: true,
24
- configurable: true,
25
- writable: true,
26
- value: void 0
27
- });
28
- this.baseUrl = config.baseUrl.replace(/\/$/, "");
29
- this.authorization = config.authorization;
30
- if (typeof config?.retry === "boolean" && config?.retry === false) {
31
- this.retry = {
32
- attempts: 1,
33
- backoff: () => 0,
34
- };
35
- }
36
- else {
37
- this.retry = {
38
- attempts: config?.retry?.retries ?? 5,
39
- backoff: config?.retry?.backoff ??
40
- ((retryCount) => Math.exp(retryCount) * 50),
41
- };
42
- }
43
- }
44
- async request(req) {
45
- const headers = new Headers(req.headers);
46
- headers.set("Upstash-Authorization", `Bearer ${this.authorization}`);
47
- const requestOptions = {
48
- method: req.method,
49
- headers,
50
- body: req.body,
51
- keepalive: req.keepalive,
52
- };
53
- const url = new URL([this.baseUrl, ...(req.path ?? [])].join("/"));
54
- if (req.query) {
55
- for (const [key, value] of Object.entries(req.query)) {
56
- if (typeof value !== "undefined") {
57
- url.searchParams.set(key, value.toString());
58
- }
59
- }
60
- }
61
- let res = null;
62
- let error = null;
63
- for (let i = 0; i <= this.retry.attempts; i++) {
64
- try {
65
- res = await fetch(url.toString(), requestOptions);
66
- break;
67
- }
68
- catch (err) {
69
- error = err;
70
- await new Promise((r) => setTimeout(r, this.retry.backoff(i)));
71
- }
72
- }
73
- if (!res) {
74
- throw error ?? new Error("Exhausted all retries");
75
- }
76
- if (res.status < 200 || res.status >= 300) {
77
- throw new QstashError(await res.text() ?? res.statusText);
78
- }
79
- return (await res.json());
80
- }
81
- }
package/esm/messages.js DELETED
@@ -1,64 +0,0 @@
1
- export class Messages {
2
- constructor(http) {
3
- Object.defineProperty(this, "http", {
4
- enumerable: true,
5
- configurable: true,
6
- writable: true,
7
- value: void 0
8
- });
9
- this.http = http;
10
- }
11
- /**
12
- * Get a message
13
- */
14
- async get(req) {
15
- return await this.http.request({
16
- method: "GET",
17
- path: ["v1", "messages", req.id],
18
- headers: { "Content-Type": "application/json" },
19
- });
20
- }
21
- /**
22
- * List your messages
23
- */
24
- async list(req) {
25
- return await this.http.request({
26
- method: "GET",
27
- path: ["v1", "messages"],
28
- headers: { "Content-Type": "application/json" },
29
- query: { cursor: req?.cursor },
30
- });
31
- }
32
- /**
33
- * List logs from a message
34
- */
35
- async logs(req) {
36
- return await this.http.request({
37
- method: "GET",
38
- path: ["v1", "messages", req.id, "logs"],
39
- headers: { "Content-Type": "application/json" },
40
- query: { cursor: req.cursor },
41
- });
42
- }
43
- /**
44
- * List tasks for a message
45
- */
46
- async tasks(req) {
47
- return await this.http.request({
48
- method: "GET",
49
- path: ["v1", "messages", req.id, "tasks"],
50
- headers: { "Content-Type": "application/json" },
51
- query: { cursor: req.cursor },
52
- });
53
- }
54
- /**
55
- * Cancel a topic by name or ID.
56
- */
57
- async delete(req) {
58
- return await this.http.request({
59
- method: "DELETE",
60
- path: ["v1", "messages", req.id],
61
- headers: { "Content-Type": "application/json" },
62
- });
63
- }
64
- }
@@ -1 +0,0 @@
1
- export { Client } from "../client.js";
package/esm/schedules.js DELETED
@@ -1,41 +0,0 @@
1
- export class Schedules {
2
- constructor(http) {
3
- Object.defineProperty(this, "http", {
4
- enumerable: true,
5
- configurable: true,
6
- writable: true,
7
- value: void 0
8
- });
9
- this.http = http;
10
- }
11
- /**
12
- * Get a schedule
13
- */
14
- async get(req) {
15
- return await this.http.request({
16
- method: "GET",
17
- path: ["v1", "schedules", req.id],
18
- headers: { "Content-Type": "application/json" },
19
- });
20
- }
21
- /**
22
- * List your schedules
23
- */
24
- async list() {
25
- return await this.http.request({
26
- method: "GET",
27
- path: ["v1", "schedules"],
28
- headers: { "Content-Type": "application/json" },
29
- });
30
- }
31
- /**
32
- * Delete a schedule
33
- */
34
- async delete(req) {
35
- return await this.http.request({
36
- method: "DELETE",
37
- path: ["v1", "schedules", req.id],
38
- headers: { "Content-Type": "application/json" },
39
- });
40
- }
41
- }
package/esm/topics.js DELETED
@@ -1,71 +0,0 @@
1
- export class Topics {
2
- constructor(http) {
3
- Object.defineProperty(this, "http", {
4
- enumerable: true,
5
- configurable: true,
6
- writable: true,
7
- value: void 0
8
- });
9
- this.http = http;
10
- }
11
- /**
12
- * Create a new topic with the given name.
13
- */
14
- async create(req) {
15
- return await this.http.request({
16
- method: "POST",
17
- path: ["v1", "topics"],
18
- headers: { "Content-Type": "application/json" },
19
- body: JSON.stringify({ name: req.name }),
20
- });
21
- }
22
- /**
23
- * Get a list of all topics.
24
- */
25
- async list() {
26
- return await this.http.request({
27
- method: "GET",
28
- path: ["v1", "topics"],
29
- headers: { "Content-Type": "application/json" },
30
- });
31
- }
32
- /**
33
- * Get a single topic by name or ID.
34
- */
35
- async get(req) {
36
- const idOrName = req.id ?? req.name;
37
- if (!idOrName) {
38
- throw new Error("Either id or name must be provided");
39
- }
40
- return await this.http.request({
41
- method: "GET",
42
- path: ["v1", "topics", idOrName],
43
- headers: { "Content-Type": "application/json" },
44
- });
45
- }
46
- /**
47
- * Update a topic
48
- */
49
- async update(req) {
50
- return await this.http.request({
51
- method: "PUT",
52
- path: ["v1", "topics", req.id],
53
- body: JSON.stringify({ name: req.name }),
54
- headers: { "Content-Type": "application/json" },
55
- });
56
- }
57
- /**
58
- * Delete a topic by name or ID.
59
- */
60
- async delete(req) {
61
- const idOrName = req.id ?? req.name;
62
- if (!idOrName) {
63
- throw new Error("Either id or name must be provided");
64
- }
65
- return await this.http.request({
66
- method: "DELETE",
67
- path: ["v1", "topics", idOrName],
68
- headers: { "Content-Type": "application/json" },
69
- });
70
- }
71
- }
package/esm/types.js DELETED
@@ -1 +0,0 @@
1
- export {};
package/script/client.js DELETED
@@ -1,137 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Client = void 0;
4
- const http_js_1 = require("./http.js");
5
- const topics_js_1 = require("./topics.js");
6
- const messages_js_1 = require("./messages.js");
7
- const schedules_js_1 = require("./schedules.js");
8
- const endpoints_js_1 = require("./endpoints.js");
9
- class Client {
10
- constructor(config) {
11
- Object.defineProperty(this, "http", {
12
- enumerable: true,
13
- configurable: true,
14
- writable: true,
15
- value: void 0
16
- });
17
- this.http = new http_js_1.HttpClient({
18
- baseUrl: config.baseUrl
19
- ? config.baseUrl.replace(/\/$/, "")
20
- : "https://qstash.upstash.io",
21
- authorization: config.authorization,
22
- });
23
- }
24
- /**
25
- * Access the topic API.
26
- *
27
- * Create, read, update or delete topics.
28
- */
29
- get topics() {
30
- return new topics_js_1.Topics(this.http);
31
- }
32
- /**
33
- * Access the endpoint API.
34
- *
35
- * Create, read, update or delete endpoints.
36
- */
37
- get endpoints() {
38
- return new endpoints_js_1.Endpoints(this.http);
39
- }
40
- /**
41
- * Access the message API.
42
- *
43
- * Read or cancel messages.
44
- */
45
- get messages() {
46
- return new messages_js_1.Messages(this.http);
47
- }
48
- /**
49
- * Access the schedule API.
50
- *
51
- * Read or delete schedules.
52
- */
53
- get schedules() {
54
- return new schedules_js_1.Schedules(this.http);
55
- }
56
- async publish(req) {
57
- const destination = req.url ?? req.topic;
58
- if (!destination) {
59
- throw new Error("Either url or topic must be set");
60
- }
61
- const headers = new Headers(req.headers);
62
- if (req.delay) {
63
- headers.set("Upstash-Delay", req.delay.toFixed());
64
- }
65
- if (req.notBefore) {
66
- headers.set("Upstash-Not-Before", req.notBefore.toFixed());
67
- }
68
- if (req.deadline) {
69
- headers.set("Upstash-Deadline", req.deadline.toFixed());
70
- }
71
- if (req.deduplicationID) {
72
- headers.set("Upstash-Deduplication-ID", req.deduplicationID);
73
- }
74
- if (req.contentBasedDeduplication) {
75
- headers.set("Upstash-Content-Based-Deduplication", "true");
76
- }
77
- if (req.retries) {
78
- headers.set("Upstash-Retries", req.retries.toFixed());
79
- }
80
- if (req.cron) {
81
- headers.set("Upstash-Cron", req.cron);
82
- }
83
- const res = await this.http.request({
84
- path: ["v1", "publish", destination],
85
- body: req.body,
86
- headers,
87
- method: "POST",
88
- });
89
- return res;
90
- }
91
- /**
92
- * publishJSON is a utility wrapper around `publish` that automatically serializes the body
93
- * and sets the `Content-Type` header to `application/json`.
94
- */
95
- async publishJSON(req) {
96
- const headers = new Headers(req.headers);
97
- headers.set("Content-Type", "application/json");
98
- const res = await this.publish({
99
- ...req,
100
- headers,
101
- body: JSON.stringify(req.body),
102
- });
103
- return res;
104
- }
105
- /**
106
- * Retrieve your logs.
107
- *
108
- * The logs endpoint is paginated and returns only 100 logs at a time.
109
- * If you want to receive more logs, you can use the cursor to paginate.
110
- *
111
- * The cursor is a unix timestamp with millisecond precision
112
- *
113
- * @example
114
- * ```ts
115
- * let cursor = Date.now()
116
- * const logs: Log[] = []
117
- * while (cursor > 0) {
118
- * const res = await qstash.logs({ cursor })
119
- * logs.push(...res.logs)
120
- * cursor = res.cursor ?? 0
121
- * }
122
- * ```
123
- */
124
- async logs(req) {
125
- const query = {};
126
- if (req?.cursor && req.cursor > 0) {
127
- query["cursor"] = req.cursor;
128
- }
129
- const res = await this.http.request({
130
- path: ["v1", "logs"],
131
- method: "GET",
132
- query,
133
- });
134
- return res;
135
- }
136
- }
137
- exports.Client = Client;
@@ -1,67 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Endpoints = void 0;
4
- class Endpoints {
5
- constructor(http) {
6
- Object.defineProperty(this, "http", {
7
- enumerable: true,
8
- configurable: true,
9
- writable: true,
10
- value: void 0
11
- });
12
- this.http = http;
13
- }
14
- /**
15
- * Create a new endpoint with the given name.
16
- */
17
- async create(req) {
18
- return await this.http.request({
19
- method: "POST",
20
- path: ["v1", "endpoints"],
21
- body: JSON.stringify(req),
22
- headers: { "Content-Type": "application/json" },
23
- });
24
- }
25
- /**
26
- * Get a list of all endpoints.
27
- */
28
- async list() {
29
- return await this.http.request({
30
- method: "GET",
31
- path: ["v1", "endpoints"],
32
- headers: { "Content-Type": "application/json" },
33
- });
34
- }
35
- /**
36
- * Get a single endpoint.
37
- */
38
- async get(req) {
39
- return await this.http.request({
40
- method: "GET",
41
- path: ["v1", "endpoints", req.id],
42
- headers: { "Content-Type": "application/json" },
43
- });
44
- }
45
- /**
46
- * Update a endpoint
47
- */
48
- async update(req) {
49
- return await this.http.request({
50
- method: "PUT",
51
- path: ["v1", "endpoints", req.id],
52
- body: JSON.stringify({ url: req.url }),
53
- headers: { "Content-Type": "application/json" },
54
- });
55
- }
56
- /**
57
- * Delete a endpoint.
58
- */
59
- async delete(req) {
60
- return await this.http.request({
61
- method: "DELETE",
62
- path: ["v1", "endpoints", req.id],
63
- headers: { "Content-Type": "application/json" },
64
- });
65
- }
66
- }
67
- exports.Endpoints = Endpoints;
package/script/error.js DELETED
@@ -1,13 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.QstashError = void 0;
4
- /**
5
- * Result of 500 Internal Server Error
6
- */
7
- class QstashError extends Error {
8
- constructor(message) {
9
- super(message);
10
- this.name = "QstashError";
11
- }
12
- }
13
- exports.QstashError = QstashError;
package/script/http.js DELETED
@@ -1,85 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.HttpClient = void 0;
4
- const error_js_1 = require("./error.js");
5
- class HttpClient {
6
- constructor(config) {
7
- Object.defineProperty(this, "baseUrl", {
8
- enumerable: true,
9
- configurable: true,
10
- writable: true,
11
- value: void 0
12
- });
13
- Object.defineProperty(this, "authorization", {
14
- enumerable: true,
15
- configurable: true,
16
- writable: true,
17
- value: void 0
18
- });
19
- Object.defineProperty(this, "options", {
20
- enumerable: true,
21
- configurable: true,
22
- writable: true,
23
- value: void 0
24
- });
25
- Object.defineProperty(this, "retry", {
26
- enumerable: true,
27
- configurable: true,
28
- writable: true,
29
- value: void 0
30
- });
31
- this.baseUrl = config.baseUrl.replace(/\/$/, "");
32
- this.authorization = config.authorization;
33
- if (typeof config?.retry === "boolean" && config?.retry === false) {
34
- this.retry = {
35
- attempts: 1,
36
- backoff: () => 0,
37
- };
38
- }
39
- else {
40
- this.retry = {
41
- attempts: config?.retry?.retries ?? 5,
42
- backoff: config?.retry?.backoff ??
43
- ((retryCount) => Math.exp(retryCount) * 50),
44
- };
45
- }
46
- }
47
- async request(req) {
48
- const headers = new Headers(req.headers);
49
- headers.set("Upstash-Authorization", `Bearer ${this.authorization}`);
50
- const requestOptions = {
51
- method: req.method,
52
- headers,
53
- body: req.body,
54
- keepalive: req.keepalive,
55
- };
56
- const url = new URL([this.baseUrl, ...(req.path ?? [])].join("/"));
57
- if (req.query) {
58
- for (const [key, value] of Object.entries(req.query)) {
59
- if (typeof value !== "undefined") {
60
- url.searchParams.set(key, value.toString());
61
- }
62
- }
63
- }
64
- let res = null;
65
- let error = null;
66
- for (let i = 0; i <= this.retry.attempts; i++) {
67
- try {
68
- res = await fetch(url.toString(), requestOptions);
69
- break;
70
- }
71
- catch (err) {
72
- error = err;
73
- await new Promise((r) => setTimeout(r, this.retry.backoff(i)));
74
- }
75
- }
76
- if (!res) {
77
- throw error ?? new Error("Exhausted all retries");
78
- }
79
- if (res.status < 200 || res.status >= 300) {
80
- throw new error_js_1.QstashError(await res.text() ?? res.statusText);
81
- }
82
- return (await res.json());
83
- }
84
- }
85
- exports.HttpClient = HttpClient;
@@ -1,68 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Messages = void 0;
4
- class Messages {
5
- constructor(http) {
6
- Object.defineProperty(this, "http", {
7
- enumerable: true,
8
- configurable: true,
9
- writable: true,
10
- value: void 0
11
- });
12
- this.http = http;
13
- }
14
- /**
15
- * Get a message
16
- */
17
- async get(req) {
18
- return await this.http.request({
19
- method: "GET",
20
- path: ["v1", "messages", req.id],
21
- headers: { "Content-Type": "application/json" },
22
- });
23
- }
24
- /**
25
- * List your messages
26
- */
27
- async list(req) {
28
- return await this.http.request({
29
- method: "GET",
30
- path: ["v1", "messages"],
31
- headers: { "Content-Type": "application/json" },
32
- query: { cursor: req?.cursor },
33
- });
34
- }
35
- /**
36
- * List logs from a message
37
- */
38
- async logs(req) {
39
- return await this.http.request({
40
- method: "GET",
41
- path: ["v1", "messages", req.id, "logs"],
42
- headers: { "Content-Type": "application/json" },
43
- query: { cursor: req.cursor },
44
- });
45
- }
46
- /**
47
- * List tasks for a message
48
- */
49
- async tasks(req) {
50
- return await this.http.request({
51
- method: "GET",
52
- path: ["v1", "messages", req.id, "tasks"],
53
- headers: { "Content-Type": "application/json" },
54
- query: { cursor: req.cursor },
55
- });
56
- }
57
- /**
58
- * Cancel a topic by name or ID.
59
- */
60
- async delete(req) {
61
- return await this.http.request({
62
- method: "DELETE",
63
- path: ["v1", "messages", req.id],
64
- headers: { "Content-Type": "application/json" },
65
- });
66
- }
67
- }
68
- exports.Messages = Messages;
@@ -1,5 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Client = void 0;
4
- var client_js_1 = require("../client.js");
5
- Object.defineProperty(exports, "Client", { enumerable: true, get: function () { return client_js_1.Client; } });
@@ -1,45 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Schedules = void 0;
4
- class Schedules {
5
- constructor(http) {
6
- Object.defineProperty(this, "http", {
7
- enumerable: true,
8
- configurable: true,
9
- writable: true,
10
- value: void 0
11
- });
12
- this.http = http;
13
- }
14
- /**
15
- * Get a schedule
16
- */
17
- async get(req) {
18
- return await this.http.request({
19
- method: "GET",
20
- path: ["v1", "schedules", req.id],
21
- headers: { "Content-Type": "application/json" },
22
- });
23
- }
24
- /**
25
- * List your schedules
26
- */
27
- async list() {
28
- return await this.http.request({
29
- method: "GET",
30
- path: ["v1", "schedules"],
31
- headers: { "Content-Type": "application/json" },
32
- });
33
- }
34
- /**
35
- * Delete a schedule
36
- */
37
- async delete(req) {
38
- return await this.http.request({
39
- method: "DELETE",
40
- path: ["v1", "schedules", req.id],
41
- headers: { "Content-Type": "application/json" },
42
- });
43
- }
44
- }
45
- exports.Schedules = Schedules;