From 368a5158cc5ebaea6c21b2a8c6428307713e04f3 Mon Sep 17 00:00:00 2001 From: Eric Peterson Date: Mon, 3 May 2021 18:13:12 +0200 Subject: [PATCH] Return an explicit undefined on no data. Signed-off-by: Eric Peterson --- .../backend-common/src/cache/CacheClient.test.ts | 4 ++-- packages/backend-common/src/cache/CacheClient.ts | 13 +++++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/packages/backend-common/src/cache/CacheClient.test.ts b/packages/backend-common/src/cache/CacheClient.test.ts index 2e14fdbea1..2ae7cee494 100644 --- a/packages/backend-common/src/cache/CacheClient.test.ts +++ b/packages/backend-common/src/cache/CacheClient.test.ts @@ -64,13 +64,13 @@ describe('CacheClient', () => { expect(actualValue).toMatchObject(expectedValue); }); - it('returns null on any underlying error', async () => { + it('returns undefined on any underlying error', async () => { const sut = new DefaultCacheClient({ client, defaultTtl, pluginId }); client.get = jest.fn().mockRejectedValue(undefined); const actualValue = await sut.get('someKey'); - expect(actualValue).toStrictEqual(null); + expect(actualValue).toStrictEqual(undefined); }); }); diff --git a/packages/backend-common/src/cache/CacheClient.ts b/packages/backend-common/src/cache/CacheClient.ts index 52703a28d4..523dae6fed 100644 --- a/packages/backend-common/src/cache/CacheClient.ts +++ b/packages/backend-common/src/cache/CacheClient.ts @@ -34,9 +34,10 @@ type CacheSetOptions = { */ export interface CacheClient { /** - * Reads data from a cache store for the given key. + * Reads data from a cache store for the given key. If no data was found, + * returns undefined. */ - get(key: string): Promise; + get(key: string): Promise; /** * Writes the given data to a cache store, associated with the given key. An @@ -66,13 +67,13 @@ export class DefaultCacheClient implements CacheClient { this.pluginId = pluginId; } - async get(key: string): Promise { + async get(key: string): Promise { const k = this.getNormalizedKey(key); try { const data = (await this.client.get(k)) as string | undefined; return this.deserializeData(data); } catch { - return null; + return undefined; } } @@ -122,7 +123,7 @@ export class DefaultCacheClient implements CacheClient { return JSON.stringify(data); } - private deserializeData(data: string | undefined): JsonValue { - return data ? JSON.parse(data) : null; + private deserializeData(data: string | undefined): JsonValue | undefined { + return data ? JSON.parse(data) : undefined; } }