diff --git a/index.ts b/index.ts index f156820..b1d28bc 100644 --- a/index.ts +++ b/index.ts @@ -68,18 +68,21 @@ async function get(key: string): Promise(key: string, value: TValue, maxAge: TimeDescriptor = {days: 30}): Promise { - if (typeof value === 'undefined') { - // @ts-expect-error This never happens in TS because `value` can't be undefined - return; + if (arguments.length < 2) { + throw new TypeError('Expected a value as the second argument'); } - const internalKey = `cache:${key}`; - await storageSet({ - [internalKey]: { - data: value, - maxAge: timeInTheFuture(maxAge) - } - }); + if (typeof value === 'undefined') { + await delete_(key); + } else { + const internalKey = `cache:${key}`; + await storageSet({ + [internalKey]: { + data: value, + maxAge: timeInTheFuture(maxAge) + } + }); + } return value; } diff --git a/test/index.js b/test/index.js index 74c5040..873cf23 100644 --- a/test/index.js +++ b/test/index.js @@ -64,10 +64,15 @@ test.serial('has() with expired cache', async t => { t.is(await cache.has('name'), false); }); +test.serial('set() without a value', async t => { + await t.throwsAsync(cache.set('name'), {instanceOf: TypeError, message: 'Expected a value as the second argument'}); +}); + test.serial('set() with undefined', async t => { - await cache.set('name'); - // StorageArea.set should not be called with `undefined` - t.is(chrome.storage.local.set.callCount, 0); + await cache.set('name', 'Anne'); + await cache.set('name', undefined); + // Cached value should be erased + t.is(await cache.has('name'), false); }); test.serial('set() with value', async t => {