Skip to content

Commit 30e3f04

Browse files
authoredMay 11, 2023
rename kv default export (#131)
* rename kv default export
1 parent 3db4ff3 commit 30e3f04

File tree

5 files changed

+67
-4
lines changed

5 files changed

+67
-4
lines changed
 

‎.changeset/eighty-starfishes-laugh.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@vercel/kv': minor
3+
---
4+
5+
move default export to named kv export

‎packages/kv/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ npm install @vercel/kv
1212
## Usage
1313

1414
```js
15-
import kv from '@vercel/kv';
15+
import { kv } from '@vercel/kv';
1616

1717
// string
1818
await kv.set('key', 'value');

‎packages/kv/src/index.test.ts

+27-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import kv, { VercelKV, createClient } from '.';
1+
import defaultKv, { kv, VercelKV, createClient } from '.';
22

33
let scanReturnValues: [number, string[]][] = [[0, []]];
44
jest.mock('@upstash/redis', () => ({
@@ -15,10 +15,11 @@ jest.mock('@upstash/redis', () => ({
1515
describe('@vercel/kv', () => {
1616
beforeEach(() => {
1717
scanReturnValues = [[0, []]];
18+
jest.clearAllMocks();
1819
});
1920

2021
describe('kv export', () => {
21-
it('exports a default client', async () => {
22+
it('exports "kv" client', async () => {
2223
process.env.KV_REST_API_URL =
2324
'https://foobar-6739.redis.vercel-storage.com';
2425
process.env.KV_REST_API_TOKEN = 'tok_foobar';
@@ -28,6 +29,30 @@ describe('@vercel/kv', () => {
2829
process.env.KV_REST_API_URL = undefined;
2930
process.env.KV_REST_API_TOKEN = undefined;
3031
});
32+
33+
it('exports default legacy client', async () => {
34+
process.env.KV_REST_API_URL =
35+
'https://foobar-6739.redis.vercel-storage.com';
36+
process.env.KV_REST_API_TOKEN = 'tok_foobar';
37+
38+
expect(await defaultKv.get('foo')).toEqual('bar');
39+
40+
process.env.KV_REST_API_URL = undefined;
41+
process.env.KV_REST_API_TOKEN = undefined;
42+
});
43+
44+
it('should load awaited default module (Vite use case', async () => {
45+
const kvModule = await import('.').then((m) => m.default);
46+
47+
process.env.KV_REST_API_URL =
48+
'https://foobar-6739.redis.vercel-storage.com';
49+
process.env.KV_REST_API_TOKEN = 'tok_foobar';
50+
51+
expect(await kvModule.get('foo')).toEqual('bar');
52+
53+
process.env.KV_REST_API_URL = undefined;
54+
process.env.KV_REST_API_TOKEN = undefined;
55+
});
3156
});
3257

3358
describe('createClient', () => {

‎packages/kv/src/index.ts

+33
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,39 @@ export function createClient(config: RedisConfigNodejs): VercelKV {
8282

8383
// eslint-disable-next-line import/no-default-export
8484
export default new Proxy(
85+
{},
86+
{
87+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
88+
get(target, prop, receiver) {
89+
if (prop === 'then' || prop === 'parse') {
90+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
91+
return Reflect.get(target, prop, receiver);
92+
}
93+
94+
if (!_kv) {
95+
if (!process.env.KV_REST_API_URL || !process.env.KV_REST_API_TOKEN) {
96+
throw new Error(
97+
'@vercel/kv: Missing required environment variables KV_REST_API_URL and KV_REST_API_TOKEN',
98+
);
99+
}
100+
// eslint-disable-next-line no-console
101+
console.warn(
102+
'\x1b[33m"The default export has been moved to a named export and it will be removed in version 1, change to import { kv }\x1b[0m"',
103+
);
104+
105+
_kv = createClient({
106+
url: process.env.KV_REST_API_URL,
107+
token: process.env.KV_REST_API_TOKEN,
108+
});
109+
}
110+
111+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
112+
return Reflect.get(_kv, prop);
113+
},
114+
},
115+
) as VercelKV;
116+
117+
export const kv = new Proxy(
85118
{},
86119
{
87120
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type

‎test/next/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Instructions:
22

3-
- `cd packages/integration`
3+
- `cd test/next`
44
- `vc link` => link to `Curated Tests` => `vercel-storage-next-integration-test-suite`
55
- `vc env pull`
66
- `pnpm i`

1 commit comments

Comments
 (1)

vercel[bot] commented on May 11, 2023

@vercel[bot]
Please sign in to comment.