Skip to content
This repository was archived by the owner on Aug 7, 2022. It is now read-only.

fix: KV Namespace list method accepts an object #32

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions packages/cloudflare-worker-mock/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,13 @@ export function makeCloudflareWorkerKVEnv(
return Promise.resolve(undefined);
},
list(
_prefix: string,
_limit: number,
_cursor: string,
_params:
| {
prefix?: string | undefined;
limit?: number | undefined;
cursor?: string | undefined;
}
| undefined,
): Promise<CloudflareWorkerKVList> {
return Promise.resolve({
cursor: '1234567890',
Expand Down
10 changes: 5 additions & 5 deletions packages/types-cloudflare-worker/src/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -665,11 +665,11 @@ export interface CloudflareWorkerKV {
* cursor: "6Ck1la0VxJ0djhidm1MdX2FyD"
* }
*/
list(
prefix?: string,
limit?: number,
cursor?: string,
): Promise<CloudflareWorkerKVList>;
list(params?: {
prefix?: string;
limit?: number;
cursor?: string;
}): Promise<CloudflareWorkerKVList>;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/helloworkerclass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ export class HelloWorkerClass {
const country = request.cf.country;
countryCodeKV.put(country, '!', { expiration: 100 });
const countryCode = await countryCodeKV.get(country);
const countryList = await countryCodeKV.list({ prefix: 'countries' });

response = new Response(
`${body} ${request.cf.country} ${countryCode}!`,
`${body} ${request.cf.country} ${countryCode} ${countryList.keys[0].name}!`,
this.responseInit,
);
}
Expand Down
28 changes: 26 additions & 2 deletions test/helloworker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ limitations under the License.
import {
CloudflareWorkerGlobalScope,
CloudflareWorkerKVOptions,
CloudflareWorkerKVList,
} from 'types-cloudflare-worker';
declare var self: CloudflareWorkerGlobalScope;

Expand Down Expand Up @@ -82,7 +83,7 @@ describe('helloworker', () => {
return Promise.resolve(undefined);
};

// Setup mock responses for the KV put() and get().
// Setup mock responses for the KV put(), get(), and list().
let putKVCalled = false;
countryCodeKV.put = (
_key: string,
Expand All @@ -102,6 +103,28 @@ describe('helloworker', () => {
return Promise.resolve('+1');
};

let listKVCalled = false;
countryCodeKV.list = (
params:
| {
prefix?: string;
limit?: number;
cursor?: string;
}
| undefined,
): Promise<CloudflareWorkerKVList> => {
listKVCalled = true;
let prefix = 'empty';
if (params) {
prefix = params.prefix || 'missing';
}
return Promise.resolve({
keys: [{ name: prefix, expiration: 1234 }],
list_complete: false,
cursor: 'CursorID',
});
};

const request = makeCloudflareWorkerRequest('/path', {
cf: {
colo: 'SFO',
Expand All @@ -115,9 +138,10 @@ describe('helloworker', () => {

expect(fetchMock).toBeCalledTimes(1);
expect(response.status).toBe(200);
expect(await response.text()).toBe('Hello US +1!');
expect(await response.text()).toBe('Hello US +1 countries!');
expect(putCacheCalled).toBe(true);
expect(putKVCalled).toBe(true);
expect(getKVCalled).toBe(true);
expect(listKVCalled).toBe(true);
});
});