Skip to content

Commit

Permalink
rename kv default export (#131)
Browse files Browse the repository at this point in the history
* rename kv default export
  • Loading branch information
correttojs committed May 11, 2023
1 parent 3db4ff3 commit 30e3f04
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/eighty-starfishes-laugh.md
@@ -0,0 +1,5 @@
---
'@vercel/kv': minor
---

move default export to named kv export
2 changes: 1 addition & 1 deletion packages/kv/README.md
Expand Up @@ -12,7 +12,7 @@ npm install @vercel/kv
## Usage

```js
import kv from '@vercel/kv';
import { kv } from '@vercel/kv';

// string
await kv.set('key', 'value');
Expand Down
29 changes: 27 additions & 2 deletions packages/kv/src/index.test.ts
@@ -1,4 +1,4 @@
import kv, { VercelKV, createClient } from '.';
import defaultKv, { kv, VercelKV, createClient } from '.';

let scanReturnValues: [number, string[]][] = [[0, []]];
jest.mock('@upstash/redis', () => ({
Expand All @@ -15,10 +15,11 @@ jest.mock('@upstash/redis', () => ({
describe('@vercel/kv', () => {
beforeEach(() => {
scanReturnValues = [[0, []]];
jest.clearAllMocks();
});

describe('kv export', () => {
it('exports a default client', async () => {
it('exports "kv" client', async () => {
process.env.KV_REST_API_URL =
'https://foobar-6739.redis.vercel-storage.com';
process.env.KV_REST_API_TOKEN = 'tok_foobar';
Expand All @@ -28,6 +29,30 @@ describe('@vercel/kv', () => {
process.env.KV_REST_API_URL = undefined;
process.env.KV_REST_API_TOKEN = undefined;
});

it('exports default legacy client', async () => {
process.env.KV_REST_API_URL =
'https://foobar-6739.redis.vercel-storage.com';
process.env.KV_REST_API_TOKEN = 'tok_foobar';

expect(await defaultKv.get('foo')).toEqual('bar');

process.env.KV_REST_API_URL = undefined;
process.env.KV_REST_API_TOKEN = undefined;
});

it('should load awaited default module (Vite use case', async () => {
const kvModule = await import('.').then((m) => m.default);

process.env.KV_REST_API_URL =
'https://foobar-6739.redis.vercel-storage.com';
process.env.KV_REST_API_TOKEN = 'tok_foobar';

expect(await kvModule.get('foo')).toEqual('bar');

process.env.KV_REST_API_URL = undefined;
process.env.KV_REST_API_TOKEN = undefined;
});
});

describe('createClient', () => {
Expand Down
33 changes: 33 additions & 0 deletions packages/kv/src/index.ts
Expand Up @@ -82,6 +82,39 @@ export function createClient(config: RedisConfigNodejs): VercelKV {

// eslint-disable-next-line import/no-default-export
export default new Proxy(
{},
{
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
get(target, prop, receiver) {
if (prop === 'then' || prop === 'parse') {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return Reflect.get(target, prop, receiver);
}

if (!_kv) {
if (!process.env.KV_REST_API_URL || !process.env.KV_REST_API_TOKEN) {
throw new Error(
'@vercel/kv: Missing required environment variables KV_REST_API_URL and KV_REST_API_TOKEN',
);
}
// eslint-disable-next-line no-console
console.warn(
'\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"',
);

_kv = createClient({
url: process.env.KV_REST_API_URL,
token: process.env.KV_REST_API_TOKEN,
});
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return Reflect.get(_kv, prop);
},
},
) as VercelKV;

export const kv = new Proxy(
{},
{
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
Expand Down
2 changes: 1 addition & 1 deletion test/next/README.md
@@ -1,6 +1,6 @@
Instructions:

- `cd packages/integration`
- `cd test/next`
- `vc link` => link to `Curated Tests` => `vercel-storage-next-integration-test-suite`
- `vc env pull`
- `pnpm i`
Expand Down

1 comment on commit 30e3f04

@vercel
Copy link

@vercel vercel bot commented on 30e3f04 May 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.