Skip to content

Commit

Permalink
tsdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
james-elicx committed Sep 27, 2023
1 parent ad381c0 commit d5b3785
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
23 changes: 22 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,28 @@ export const binding = <T>(id: string, opts?: BindingOpts): T => {

type DeriveCacheReturnType<T> = T extends 'default' | undefined ? Cache : Promise<Cache>;

export const cacheApi = <T extends string | undefined>(cacheName?: T): DeriveCacheReturnType<T> => {
/**
* Interfaces with the Cloudflare Cache API.
*
* By default, the `default` cache is used, however, a custom cache can be provided by passing a
* cache name as the first argument.
*
* @example
* ```ts
* const value = await cacheApi().put(..., ...);
* ```
*
* @example
* ```ts
* const value = await cacheApi('custom').put(..., ...);
* ```
*
* @param cacheName Name of the cache to open, or `undefined` to open the default cache.
* @returns Cache instance.
*/
export const cacheApi = <T extends string | undefined = undefined>(
cacheName?: T,
): DeriveCacheReturnType<T> => {
if (isProxyEnabled()) {
return new Proxy(
{},
Expand Down
10 changes: 4 additions & 6 deletions tests/proxy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,18 +474,16 @@ suite('bindings', () => {
const parseRes = async (res: MaybePromise<CfResponse | undefined>) => (await res)?.text();

test('default cache -> put/match/delete', async () => {
const defaultCache = cacheApi('default');

const firstKey = buildUrl('first-key');
await defaultCache.put(firstKey, buildRes('first-value'));
await cacheApi('default').put(firstKey, buildRes('first-value'));

const firstValue = await parseRes(defaultCache.match(firstKey));
const firstValue = await parseRes(cacheApi('default').match(firstKey));
expect(firstValue).toEqual('first-value');

const isDeleted = await defaultCache.delete(firstKey);
const isDeleted = await cacheApi().delete(firstKey);
expect(isDeleted).toEqual(true);

const firstValueAfterDelete = await parseRes(defaultCache.match(firstKey));
const firstValueAfterDelete = await parseRes(cacheApi().match(firstKey));
expect(firstValueAfterDelete).toEqual(undefined);
});

Expand Down

0 comments on commit d5b3785

Please sign in to comment.