@upstash/redis 1.0.0-alpha.0 → 1.0.0-alpha.3

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/fastly.mjs ADDED
@@ -0,0 +1,25 @@
1
+ import {
2
+ Redis
3
+ } from "./chunk-Y5TC4HX2.mjs";
4
+ import "./chunk-ZIB6XPPC.mjs";
5
+ import {
6
+ HttpClient
7
+ } from "./chunk-U7OXAQMQ.mjs";
8
+ import "./chunk-7YUZYRJS.mjs";
9
+
10
+ // pkg/fastly.ts
11
+ var Redis2 = class extends Redis {
12
+ constructor(config) {
13
+ const client = new HttpClient({
14
+ baseUrl: config.url,
15
+ headers: {
16
+ authorization: `Bearer ${config.token}`
17
+ },
18
+ options: { backend: config.backend }
19
+ });
20
+ super(client);
21
+ }
22
+ };
23
+ export {
24
+ Redis2 as Redis
25
+ };
package/http.d.ts ADDED
@@ -0,0 +1,29 @@
1
+ declare type HttpRequest = {
2
+ path?: string[];
3
+ /**
4
+ * Request body will be serialized to json
5
+ */
6
+ body?: unknown;
7
+ };
8
+ declare type UpstashResponse<TResult> = {
9
+ result?: TResult;
10
+ error?: string;
11
+ };
12
+ declare type HttpClientConfig = {
13
+ headers?: Record<string, string>;
14
+ baseUrl: string;
15
+ options?: {
16
+ backend?: string;
17
+ };
18
+ };
19
+ declare class HttpClient {
20
+ baseUrl: string;
21
+ headers: Record<string, string>;
22
+ readonly options?: {
23
+ backend?: string;
24
+ };
25
+ constructor(config: HttpClientConfig);
26
+ request<TResponse>(req: HttpRequest): Promise<TResponse>;
27
+ }
28
+
29
+ export { HttpClient, HttpClientConfig, HttpRequest, UpstashResponse };
package/http.js ADDED
@@ -0,0 +1,84 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
20
+ var __export = (target, all) => {
21
+ for (var name in all)
22
+ __defProp(target, name, { get: all[name], enumerable: true });
23
+ };
24
+ var __reExport = (target, module2, copyDefault, desc) => {
25
+ if (module2 && typeof module2 === "object" || typeof module2 === "function") {
26
+ for (let key of __getOwnPropNames(module2))
27
+ if (!__hasOwnProp.call(target, key) && (copyDefault || key !== "default"))
28
+ __defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
29
+ }
30
+ return target;
31
+ };
32
+ var __toCommonJS = /* @__PURE__ */ ((cache) => {
33
+ return (module2, temp) => {
34
+ return cache && cache.get(module2) || (temp = __reExport(__markAsModule({}), module2, 1), cache && cache.set(module2, temp), temp);
35
+ };
36
+ })(typeof WeakMap !== "undefined" ? /* @__PURE__ */ new WeakMap() : 0);
37
+
38
+ // pkg/http.ts
39
+ var http_exports = {};
40
+ __export(http_exports, {
41
+ HttpClient: () => HttpClient
42
+ });
43
+ var import_isomorphic_fetch = require("isomorphic-fetch");
44
+
45
+ // pkg/error.ts
46
+ var UpstashError = class extends Error {
47
+ constructor(message) {
48
+ super(message);
49
+ this.name = "UpstashError";
50
+ }
51
+ };
52
+
53
+ // pkg/http.ts
54
+ var HttpClient = class {
55
+ constructor(config) {
56
+ this.baseUrl = config.baseUrl.replace(/\/$/, "");
57
+ this.headers = __spreadValues({
58
+ "Content-Type": "application/json"
59
+ }, config.headers);
60
+ this.options = config.options;
61
+ }
62
+ async request(req) {
63
+ var _a;
64
+ if (!req.path) {
65
+ req.path = [];
66
+ }
67
+ const res = await fetch([this.baseUrl, ...req.path].join("/"), {
68
+ method: "POST",
69
+ headers: this.headers,
70
+ body: JSON.stringify(req.body),
71
+ backend: (_a = this.options) == null ? void 0 : _a.backend
72
+ });
73
+ const body = await res.json();
74
+ if (!res.ok) {
75
+ throw new UpstashError(body.error);
76
+ }
77
+ return body;
78
+ }
79
+ };
80
+ module.exports = __toCommonJS(http_exports);
81
+ // Annotate the CommonJS export names for ESM import in node:
82
+ 0 && (module.exports = {
83
+ HttpClient
84
+ });
package/http.mjs ADDED
@@ -0,0 +1,7 @@
1
+ import {
2
+ HttpClient
3
+ } from "./chunk-U7OXAQMQ.mjs";
4
+ import "./chunk-7YUZYRJS.mjs";
5
+ export {
6
+ HttpClient
7
+ };
package/index.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ export { Redis, RedisConfigNodejs } from './nodejs';
2
+ import './redis-dd052782';
3
+ import './zunionstore-dffa797d';
4
+ import './http';
5
+
6
+ /**
7
+ * Result of a bad request to upstash
8
+ */
9
+ declare class UpstashError extends Error {
10
+ constructor(message: string);
11
+ }
12
+
13
+ export { UpstashError };
@@ -38,13 +38,10 @@ var __toCommonJS = /* @__PURE__ */ ((cache) => {
38
38
  // pkg/index.ts
39
39
  var pkg_exports = {};
40
40
  __export(pkg_exports, {
41
- Redis: () => Redis,
41
+ Redis: () => Redis2,
42
42
  UpstashError: () => UpstashError
43
43
  });
44
44
 
45
- // pkg/http.ts
46
- var import_isomorphic_fetch = require("isomorphic-fetch");
47
-
48
45
  // pkg/error.ts
49
46
  var UpstashError = class extends Error {
50
47
  constructor(message) {
@@ -53,36 +50,6 @@ var UpstashError = class extends Error {
53
50
  }
54
51
  };
55
52
 
56
- // pkg/http.ts
57
- var HttpClient = class {
58
- constructor(config) {
59
- var _a;
60
- this.baseUrl = config.baseUrl.replace(/\/$/, "");
61
- this.headers = (_a = config.headers) != null ? _a : {};
62
- }
63
- async request(method, req) {
64
- if (!req.path) {
65
- req.path = [];
66
- }
67
- const headers = __spreadValues(__spreadValues({
68
- "Content-Type": "application/json"
69
- }, this.headers), req.headers);
70
- const res = await fetch([this.baseUrl, ...req.path].join("/"), {
71
- method,
72
- headers,
73
- body: JSON.stringify(req.body)
74
- });
75
- const body = await res.json();
76
- if (!res.ok) {
77
- throw new UpstashError(body.error);
78
- }
79
- return body;
80
- }
81
- async post(req) {
82
- return await this.request("POST", req);
83
- }
84
- };
85
-
86
53
  // pkg/util.ts
87
54
  function parseRecursive(obj) {
88
55
  return Array.isArray(obj) ? obj.map((o) => {
@@ -109,7 +76,7 @@ var Command = class {
109
76
  this.deserialize = (_a = opts == null ? void 0 : opts.deserialize) != null ? _a : parseResponse;
110
77
  }
111
78
  async exec(client) {
112
- const { result, error } = await client.post({
79
+ const { result, error } = await client.request({
113
80
  body: this.command
114
81
  });
115
82
  if (error) {
@@ -1041,7 +1008,7 @@ var Pipeline = class {
1041
1008
  if (this.commands.length === 0) {
1042
1009
  throw new Error("Pipeline is empty");
1043
1010
  }
1044
- const res = await this.client.post({
1011
+ const res = await this.client.request({
1045
1012
  path: ["pipeline"],
1046
1013
  body: Object.values(this.commands).map((c) => c.command)
1047
1014
  });
@@ -1381,38 +1348,8 @@ var Pipeline = class {
1381
1348
 
1382
1349
  // pkg/redis.ts
1383
1350
  var Redis = class {
1384
- constructor(config) {
1385
- this.client = new HttpClient({
1386
- baseUrl: config.url,
1387
- headers: {
1388
- authorization: `Bearer ${config.token}`
1389
- }
1390
- });
1391
- }
1392
- static fromEnv() {
1393
- if (typeof (process == null ? void 0 : process.env) === "undefined") {
1394
- throw new Error("Unable to get environment variables, `process.env` is undefined. If you are deploying to cloudflare, please use `fromCloudflareEnv()` instead");
1395
- }
1396
- const url = process.env["UPSTASH_REDIS_REST_URL"];
1397
- if (!url) {
1398
- throw new Error("Unable to find environment variable: `UPSTASH_REDIS_REST_URL`");
1399
- }
1400
- const token = process.env["UPSTASH_REDIS_REST_TOKEN"];
1401
- if (!token) {
1402
- throw new Error("Unable to find environment variable: `UPSTASH_REDIS_REST_TOKEN`");
1403
- }
1404
- return new Redis({ url, token });
1405
- }
1406
- static fromCloudflareEnv() {
1407
- const url = UPSTASH_REDIS_REST_URL;
1408
- const token = UPSTASH_REDIS_REST_TOKEN;
1409
- if (!url) {
1410
- throw new Error("Unable to find environment variable: `UPSTASH_REDIS_REST_URL`");
1411
- }
1412
- if (!token) {
1413
- throw new Error("Unable to find environment variable: `UPSTASH_REDIS_REST_TOKEN`");
1414
- }
1415
- return new Redis({ url, token });
1351
+ constructor(client) {
1352
+ this.client = client;
1416
1353
  }
1417
1354
  pipeline() {
1418
1355
  return new Pipeline(this.client);
@@ -1742,6 +1679,63 @@ var Redis = class {
1742
1679
  return new ZUnionStoreCommand(...args).exec(this.client);
1743
1680
  }
1744
1681
  };
1682
+
1683
+ // pkg/http.ts
1684
+ var import_isomorphic_fetch = require("isomorphic-fetch");
1685
+ var HttpClient = class {
1686
+ constructor(config) {
1687
+ this.baseUrl = config.baseUrl.replace(/\/$/, "");
1688
+ this.headers = __spreadValues({
1689
+ "Content-Type": "application/json"
1690
+ }, config.headers);
1691
+ this.options = config.options;
1692
+ }
1693
+ async request(req) {
1694
+ var _a;
1695
+ if (!req.path) {
1696
+ req.path = [];
1697
+ }
1698
+ const res = await fetch([this.baseUrl, ...req.path].join("/"), {
1699
+ method: "POST",
1700
+ headers: this.headers,
1701
+ body: JSON.stringify(req.body),
1702
+ backend: (_a = this.options) == null ? void 0 : _a.backend
1703
+ });
1704
+ const body = await res.json();
1705
+ if (!res.ok) {
1706
+ throw new UpstashError(body.error);
1707
+ }
1708
+ return body;
1709
+ }
1710
+ };
1711
+
1712
+ // pkg/nodejs.ts
1713
+ var import_isomorphic_fetch2 = require("isomorphic-fetch");
1714
+ var Redis2 = class extends Redis {
1715
+ constructor(config) {
1716
+ const client = new HttpClient({
1717
+ baseUrl: config.url,
1718
+ headers: {
1719
+ authorization: `Bearer ${config.token}`
1720
+ }
1721
+ });
1722
+ super(client);
1723
+ }
1724
+ static fromEnv() {
1725
+ if (typeof (process == null ? void 0 : process.env) === "undefined") {
1726
+ throw new Error("Unable to get environment variables, `process.env` is undefined. If you are deploying to cloudflare, please use `Redis.onCloudflare()` instead");
1727
+ }
1728
+ const url = process.env["UPSTASH_REDIS_REST_URL"];
1729
+ if (!url) {
1730
+ throw new Error("Unable to find environment variable: `UPSTASH_REDIS_REST_URL`");
1731
+ }
1732
+ const token = process.env["UPSTASH_REDIS_REST_TOKEN"];
1733
+ if (!token) {
1734
+ throw new Error("Unable to find environment variable: `UPSTASH_REDIS_REST_TOKEN`");
1735
+ }
1736
+ return new Redis2({ url, token });
1737
+ }
1738
+ };
1745
1739
  module.exports = __toCommonJS(pkg_exports);
1746
1740
  // Annotate the CommonJS export names for ESM import in node:
1747
1741
  0 && (module.exports = {
package/index.mjs ADDED
@@ -0,0 +1,13 @@
1
+ import {
2
+ Redis
3
+ } from "./chunk-FJSI5EBJ.mjs";
4
+ import "./chunk-Y5TC4HX2.mjs";
5
+ import "./chunk-ZIB6XPPC.mjs";
6
+ import "./chunk-U7OXAQMQ.mjs";
7
+ import {
8
+ UpstashError
9
+ } from "./chunk-7YUZYRJS.mjs";
10
+ export {
11
+ Redis,
12
+ UpstashError
13
+ };
package/nodejs.d.ts ADDED
@@ -0,0 +1,47 @@
1
+ import { R as Redis$1 } from './redis-dd052782';
2
+ import './zunionstore-dffa797d';
3
+ import './http';
4
+
5
+ /**
6
+ * Connection credentials for upstash redis.
7
+ * Get them from https://console.upstash.com/redis/<uuid>
8
+ */
9
+ declare type RedisConfigNodejs = {
10
+ /**
11
+ * UPSTASH_REDIS_REST_URL
12
+ */
13
+ url: string;
14
+ /**
15
+ * UPSTASH_REDIS_REST_TOKEN
16
+ */
17
+ token: string;
18
+ };
19
+ /**
20
+ * Serverless redis client for upstash.
21
+ */
22
+ declare class Redis extends Redis$1 {
23
+ /**
24
+ * Create a new redis client
25
+ *
26
+ * @example
27
+ * ```typescript
28
+ * const redis = new Redis({
29
+ * url: "<UPSTASH_REDIS_REST_URL>",
30
+ * token: "<UPSTASH_REDIS_REST_TOKEN>",
31
+ * });
32
+ * ```
33
+ */
34
+ constructor(config: RedisConfigNodejs);
35
+ /**
36
+ * Create a new Upstash Redis instance from environment variables.
37
+ *
38
+ * Use this to automatically load connection secrets from your environment
39
+ * variables. For instance when using the Vercel integration.
40
+ *
41
+ * This tries to load `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN` from
42
+ * your environment using `process.env`.
43
+ */
44
+ static fromEnv(): Redis;
45
+ }
46
+
47
+ export { Redis, RedisConfigNodejs };