@upstash/redis 1.3.2-alpha.0 → 1.3.2-alpha.1
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/README.md +25 -0
- package/esm/platforms/nodejs.js +0 -2
- package/esm/platforms/vercel.js +68 -0
- package/package.json +6 -1
- package/script/platforms/nodejs.js +0 -2
- package/script/platforms/vercel.js +95 -0
- package/types/platforms/vercel.d.ts +80 -0
package/README.md
CHANGED
|
@@ -34,10 +34,18 @@ further explanation we wrote a
|
|
|
34
34
|
|
|
35
35
|
### Install
|
|
36
36
|
|
|
37
|
+
#### npm
|
|
38
|
+
|
|
37
39
|
```bash
|
|
38
40
|
npm install @upstash/redis
|
|
39
41
|
```
|
|
40
42
|
|
|
43
|
+
#### Deno
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
import { Redis } from "https://deno.land/x/upstash_redis/mod.ts";
|
|
47
|
+
```
|
|
48
|
+
|
|
41
49
|
### Create database
|
|
42
50
|
|
|
43
51
|
Create a new redis database on [upstash](https://console.upstash.com/)
|
|
@@ -127,6 +135,23 @@ const redis = new Redis({
|
|
|
127
135
|
- [Code example](https://github.com/upstash/upstash-redis/tree/main/examples/fastly)
|
|
128
136
|
- [Documentation](https://blog.upstash.com/fastly-compute-edge-with-redi)
|
|
129
137
|
|
|
138
|
+
#### Deno
|
|
139
|
+
|
|
140
|
+
Examples: [Deno Deploy](https://deno.com/deploy),
|
|
141
|
+
[Netlify Edge](https://www.netlify.com/products/edge/)
|
|
142
|
+
|
|
143
|
+
```ts
|
|
144
|
+
import { Redis } from "https://deno.land/x/upstash_redis/mod.ts"
|
|
145
|
+
|
|
146
|
+
const redis = new Redis({
|
|
147
|
+
url: <UPSTASH_REDIS_REST_URL>,
|
|
148
|
+
token: <UPSTASH_REDIS_REST_TOKEN>,
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
// or
|
|
152
|
+
const redis = Redis.fromEnv();
|
|
153
|
+
```
|
|
154
|
+
|
|
130
155
|
### Working with types
|
|
131
156
|
|
|
132
157
|
Most commands allow you to provide a type to make working with typescript
|
package/esm/platforms/nodejs.js
CHANGED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// deno-lint-ignore-file
|
|
2
|
+
import * as core from "../pkg/redis.js";
|
|
3
|
+
import { UpstashError } from "../pkg/error.js";
|
|
4
|
+
/**
|
|
5
|
+
* Serverless redis client for upstash.
|
|
6
|
+
*/
|
|
7
|
+
export class Redis extends core.Redis {
|
|
8
|
+
constructor(configOrRequester) {
|
|
9
|
+
if ("request" in configOrRequester) {
|
|
10
|
+
super(configOrRequester);
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
const client = defaultRequester({
|
|
14
|
+
baseUrl: configOrRequester.url,
|
|
15
|
+
headers: { authorization: `Bearer ${configOrRequester.token}` },
|
|
16
|
+
agent: configOrRequester.agent,
|
|
17
|
+
});
|
|
18
|
+
super(client);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Create a new Upstash Redis instance from environment variables.
|
|
22
|
+
*
|
|
23
|
+
* Use this to automatically load connection secrets from your environment
|
|
24
|
+
* variables. For instance when using the Vercel integration.
|
|
25
|
+
*
|
|
26
|
+
* This tries to load `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN` from
|
|
27
|
+
* your environment using `process.env`.
|
|
28
|
+
*/
|
|
29
|
+
static fromEnv(config) {
|
|
30
|
+
// @ts-ignore process will be defined in node
|
|
31
|
+
if (typeof process?.env === "undefined") {
|
|
32
|
+
throw new Error('Unable to get environment variables, `process.env` is undefined. If you are deploying to cloudflare, please import from "@upstash/redis/cloudflare" instead');
|
|
33
|
+
}
|
|
34
|
+
// @ts-ignore process will be defined in node
|
|
35
|
+
const url = process?.env["UPSTASH_REDIS_REST_URL"];
|
|
36
|
+
if (!url) {
|
|
37
|
+
throw new Error("Unable to find environment variable: `UPSTASH_REDIS_REST_URL`");
|
|
38
|
+
}
|
|
39
|
+
// @ts-ignore process will be defined in node
|
|
40
|
+
const token = process?.env["UPSTASH_REDIS_REST_TOKEN"];
|
|
41
|
+
if (!token) {
|
|
42
|
+
throw new Error("Unable to find environment variable: `UPSTASH_REDIS_REST_TOKEN`");
|
|
43
|
+
}
|
|
44
|
+
return new Redis({ url, token, ...config });
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function defaultRequester(config) {
|
|
48
|
+
return {
|
|
49
|
+
request: async function (req) {
|
|
50
|
+
if (!req.path) {
|
|
51
|
+
req.path = [];
|
|
52
|
+
}
|
|
53
|
+
const res = await fetch([config.baseUrl, ...req.path].join("/"), {
|
|
54
|
+
method: "POST",
|
|
55
|
+
headers: { "Content-Type": "application/json", ...config.headers },
|
|
56
|
+
body: JSON.stringify(req.body),
|
|
57
|
+
keepalive: true,
|
|
58
|
+
// @ts-ignore
|
|
59
|
+
agent: config.agent,
|
|
60
|
+
});
|
|
61
|
+
const body = (await res.json());
|
|
62
|
+
if (!res.ok) {
|
|
63
|
+
throw new UpstashError(body.error);
|
|
64
|
+
}
|
|
65
|
+
return body;
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
}
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"main": "./script/platforms/nodejs.js",
|
|
4
4
|
"types": "./types/platforms/nodejs.d.ts",
|
|
5
5
|
"name": "@upstash/redis",
|
|
6
|
-
"version": "v1.3.2-alpha.
|
|
6
|
+
"version": "v1.3.2-alpha.1",
|
|
7
7
|
"description": "An HTTP/REST based Redis client built on top of Upstash REST API.",
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|
|
@@ -71,6 +71,11 @@
|
|
|
71
71
|
"require": "./script/platforms/nodejs.js",
|
|
72
72
|
"types": "./types/platforms/nodejs.d.ts"
|
|
73
73
|
},
|
|
74
|
+
"./vercel": {
|
|
75
|
+
"import": "./esm/platforms/vercel.js",
|
|
76
|
+
"require": "./script/platforms/vercel.js",
|
|
77
|
+
"types": "./types/platforms/vercel.d.ts"
|
|
78
|
+
},
|
|
74
79
|
"./cloudflare": {
|
|
75
80
|
"import": "./esm/platforms/cloudflare.js",
|
|
76
81
|
"require": "./script/platforms/cloudflare.js",
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// deno-lint-ignore-file
|
|
3
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
4
|
+
if (k2 === undefined) k2 = k;
|
|
5
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
6
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
7
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
8
|
+
}
|
|
9
|
+
Object.defineProperty(o, k2, desc);
|
|
10
|
+
}) : (function(o, m, k, k2) {
|
|
11
|
+
if (k2 === undefined) k2 = k;
|
|
12
|
+
o[k2] = m[k];
|
|
13
|
+
}));
|
|
14
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
15
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
16
|
+
}) : function(o, v) {
|
|
17
|
+
o["default"] = v;
|
|
18
|
+
});
|
|
19
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
20
|
+
if (mod && mod.__esModule) return mod;
|
|
21
|
+
var result = {};
|
|
22
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
23
|
+
__setModuleDefault(result, mod);
|
|
24
|
+
return result;
|
|
25
|
+
};
|
|
26
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
27
|
+
exports.Redis = void 0;
|
|
28
|
+
const core = __importStar(require("../pkg/redis.js"));
|
|
29
|
+
const error_js_1 = require("../pkg/error.js");
|
|
30
|
+
/**
|
|
31
|
+
* Serverless redis client for upstash.
|
|
32
|
+
*/
|
|
33
|
+
class Redis extends core.Redis {
|
|
34
|
+
constructor(configOrRequester) {
|
|
35
|
+
if ("request" in configOrRequester) {
|
|
36
|
+
super(configOrRequester);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const client = defaultRequester({
|
|
40
|
+
baseUrl: configOrRequester.url,
|
|
41
|
+
headers: { authorization: `Bearer ${configOrRequester.token}` },
|
|
42
|
+
agent: configOrRequester.agent,
|
|
43
|
+
});
|
|
44
|
+
super(client);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Create a new Upstash Redis instance from environment variables.
|
|
48
|
+
*
|
|
49
|
+
* Use this to automatically load connection secrets from your environment
|
|
50
|
+
* variables. For instance when using the Vercel integration.
|
|
51
|
+
*
|
|
52
|
+
* This tries to load `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN` from
|
|
53
|
+
* your environment using `process.env`.
|
|
54
|
+
*/
|
|
55
|
+
static fromEnv(config) {
|
|
56
|
+
// @ts-ignore process will be defined in node
|
|
57
|
+
if (typeof process?.env === "undefined") {
|
|
58
|
+
throw new Error('Unable to get environment variables, `process.env` is undefined. If you are deploying to cloudflare, please import from "@upstash/redis/cloudflare" instead');
|
|
59
|
+
}
|
|
60
|
+
// @ts-ignore process will be defined in node
|
|
61
|
+
const url = process?.env["UPSTASH_REDIS_REST_URL"];
|
|
62
|
+
if (!url) {
|
|
63
|
+
throw new Error("Unable to find environment variable: `UPSTASH_REDIS_REST_URL`");
|
|
64
|
+
}
|
|
65
|
+
// @ts-ignore process will be defined in node
|
|
66
|
+
const token = process?.env["UPSTASH_REDIS_REST_TOKEN"];
|
|
67
|
+
if (!token) {
|
|
68
|
+
throw new Error("Unable to find environment variable: `UPSTASH_REDIS_REST_TOKEN`");
|
|
69
|
+
}
|
|
70
|
+
return new Redis({ url, token, ...config });
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
exports.Redis = Redis;
|
|
74
|
+
function defaultRequester(config) {
|
|
75
|
+
return {
|
|
76
|
+
request: async function (req) {
|
|
77
|
+
if (!req.path) {
|
|
78
|
+
req.path = [];
|
|
79
|
+
}
|
|
80
|
+
const res = await fetch([config.baseUrl, ...req.path].join("/"), {
|
|
81
|
+
method: "POST",
|
|
82
|
+
headers: { "Content-Type": "application/json", ...config.headers },
|
|
83
|
+
body: JSON.stringify(req.body),
|
|
84
|
+
keepalive: true,
|
|
85
|
+
// @ts-ignore
|
|
86
|
+
agent: config.agent,
|
|
87
|
+
});
|
|
88
|
+
const body = (await res.json());
|
|
89
|
+
if (!res.ok) {
|
|
90
|
+
throw new error_js_1.UpstashError(body.error);
|
|
91
|
+
}
|
|
92
|
+
return body;
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import * as core from "../pkg/redis.js";
|
|
2
|
+
import { Requester, UpstashRequest, UpstashResponse } from "../pkg/http.js";
|
|
3
|
+
import https from "https";
|
|
4
|
+
import http from "http";
|
|
5
|
+
export type { Requester, UpstashRequest, UpstashResponse };
|
|
6
|
+
/**
|
|
7
|
+
* Connection credentials for upstash redis.
|
|
8
|
+
* Get them from https://console.upstash.com/redis/<uuid>
|
|
9
|
+
*/
|
|
10
|
+
export declare type RedisConfigVercel = {
|
|
11
|
+
/**
|
|
12
|
+
* UPSTASH_REDIS_REST_URL
|
|
13
|
+
*/
|
|
14
|
+
url: string;
|
|
15
|
+
/**
|
|
16
|
+
* UPSTASH_REDIS_REST_TOKEN
|
|
17
|
+
*/
|
|
18
|
+
token: string;
|
|
19
|
+
/**
|
|
20
|
+
* An agent allows you to reuse connections to reduce latency for multiple sequential requests.
|
|
21
|
+
*
|
|
22
|
+
* This is a node specific implementation and is not supported in various runtimes like Vercel
|
|
23
|
+
* edge functions.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* import https from "https"
|
|
28
|
+
*
|
|
29
|
+
* const options: RedisConfigVercel = {
|
|
30
|
+
* agent: new https.Agent({ keepAlive: true })
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
agent?: http.Agent | https.Agent;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Serverless redis client for upstash.
|
|
38
|
+
*/
|
|
39
|
+
export declare class Redis extends core.Redis {
|
|
40
|
+
/**
|
|
41
|
+
* Create a new redis client by providing the url and token
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* const redis = new Redis({
|
|
46
|
+
* url: "<UPSTASH_REDIS_REST_URL>",
|
|
47
|
+
* token: "<UPSTASH_REDIS_REST_TOKEN>",
|
|
48
|
+
* });
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
constructor(config: RedisConfigVercel);
|
|
52
|
+
/**
|
|
53
|
+
* Create a new redis client by providing a custom `Requester` implementation
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
*
|
|
58
|
+
* import { UpstashRequest, Requester, UpstashResponse, Redis } from "@upstash/redis/vercel"
|
|
59
|
+
*
|
|
60
|
+
* const requester: Requester = {
|
|
61
|
+
* request: <TResult>(req: UpstashRequest): Promise<UpstashResponse<TResult>> => {
|
|
62
|
+
* // ...
|
|
63
|
+
* }
|
|
64
|
+
* }
|
|
65
|
+
*
|
|
66
|
+
* const redis = new Redis(requester)
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
constructor(requesters: Requester);
|
|
70
|
+
/**
|
|
71
|
+
* Create a new Upstash Redis instance from environment variables.
|
|
72
|
+
*
|
|
73
|
+
* Use this to automatically load connection secrets from your environment
|
|
74
|
+
* variables. For instance when using the Vercel integration.
|
|
75
|
+
*
|
|
76
|
+
* This tries to load `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN` from
|
|
77
|
+
* your environment using `process.env`.
|
|
78
|
+
*/
|
|
79
|
+
static fromEnv(config?: Omit<RedisConfigVercel, "url" | "token">): Redis;
|
|
80
|
+
}
|