Skip to content

Commit

Permalink
validate options and support env
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed May 5, 2023
1 parent c8e12de commit ca64677
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
13 changes: 8 additions & 5 deletions docs/content/6.drivers/vercel-kv.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import vercelKVDriver from "unstorage/drivers/vercel-kv";

const storage = createStorage({
driver: vercelKVDriver({
// base: "unstorage",
url: "https://<your-project-slug>.kv.vercel-storage.com",
token: "<your secret token>",
// url: "https://<your-project-slug>.kv.vercel-storage.com", // KV_REST_API_URL
// token: "<your secret token>", // KV_REST_API_TOKEN
// base: "test",
// env: "KV",
}),
});
```
Expand All @@ -29,7 +30,9 @@ To use, you will need to install `@vercel/kv` dependency in your project:

**Options:**

- `base`: Optional prefix to use for all keys. Can be used for namespacing.
- `url`: Url to use for connecting to your Vercel KV store. It has the format `https://<your-project-slug>.kv.vercel-storage.com`
- `url`: Rest API URL to use for connecting to your Vercel KV store. Default is `KV_REST_API_URL`.
- `token`: Rest API Token to use for connecting to your Vercel KV store. Default is `KV_REST_API_TOKEN`.
- `base`: [optional] Prefix to use for all keys. Can be used for namespacing.
- `env`: [optional] Flag to customzize environment variable prefix (Default is `KV`). Set to `false` to disable env inference for `url` and `token` options.

See [@upstash/redis](https://docs.upstash.com/redis/sdks/javascriptsdk/advanced) for all available options.
20 changes: 18 additions & 2 deletions src/drivers/vercel-kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import type { RedisConfigNodejs } from "@upstash/redis";

import { defineDriver, normalizeKey, joinKeys } from "./utils";

export interface VercelKVOptions extends RedisConfigNodejs {
export interface VercelKVOptions extends Partial<RedisConfigNodejs> {
base?: string;
env?: false | string;
}

export default defineDriver<VercelKVOptions>((opts) => {
Expand All @@ -14,7 +15,22 @@ export default defineDriver<VercelKVOptions>((opts) => {

let _client: VercelKV;
const getClient = () => {
if (!_client ) {
if (!_client) {
const envPrefix = typeof process !== "undefined" && opts.env !== false ? `${opts.env || "KV"}_` : "";
if (!opts.url) {
if (envPrefix && process.env[envPrefix + 'REST_API_URL']) {
opts.url = process.env[envPrefix + 'REST_API_URL'];
} else {
throw new Error('[unstorage] [vercel-kv] missing required `url` option or `KV_REST_API_URL` env.');
}
}
if (!opts.token) {
if (envPrefix && process.env[envPrefix + 'REST_API_TOKEN']) {
opts.token = process.env[envPrefix + 'REST_API_TOKEN'];
} else {
throw new Error('[unstorage] [vercel-kv] missing required `token` option or `KV_REST_API_TOKEN` env.');
}
}
_client = createClient(opts);
}
return _client;
Expand Down

0 comments on commit ca64677

Please sign in to comment.