From a087a31e2a57e01c82675f7901a7f6fcb4131b4a Mon Sep 17 00:00:00 2001 From: Roy Weisselberg Date: Mon, 16 Dec 2024 20:03:39 +0200 Subject: [PATCH] feat(storage): [beta] added error propagation and more input validation to search function --- lib/minimal-package.ts | 2 +- lib/storage/storage.ts | 7 +++++++ lib/types/storage.ts | 1 + package.json | 2 +- tests/storage/storage.test.ts | 12 ++++++++++++ 5 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/minimal-package.ts b/lib/minimal-package.ts index 03ad212..4a480e8 100644 --- a/lib/minimal-package.ts +++ b/lib/minimal-package.ts @@ -1 +1 @@ -export default { name: '@mondaycom/apps-sdk', version: '3.0.12' }; +export default { name: '@mondaycom/apps-sdk', version: '3.0.12-beta' }; diff --git a/lib/storage/storage.ts b/lib/storage/storage.ts index 3c68c38..d602f62 100644 --- a/lib/storage/storage.ts +++ b/lib/storage/storage.ts @@ -42,6 +42,9 @@ export class Storage extends BaseStorage implements IStorageInstance { } async search(key: string, options: SearchOptions = {}): Promise> { + if (!key?.length){ + return { success: false, error: 'key cannot be empty', records: null }; + } const url = this.searchUrl(key, options); const params = { method: 'GET' }; const result = await this.storageFetchV2>(url, params); @@ -49,6 +52,10 @@ export class Storage extends BaseStorage implements IStorageInstance { return { success: false, records: null }; } + if (result.error) { + return { success: false, error: result.error, records: null }; + } + const response: SearchResponse = { success: true, records: result.records }; if (result.cursor) { response.cursor = result.cursor; diff --git a/lib/types/storage.ts b/lib/types/storage.ts index 9e5477e..97894c2 100644 --- a/lib/types/storage.ts +++ b/lib/types/storage.ts @@ -43,6 +43,7 @@ export type SearchEntity = { export type SearchServerResponse = { records: Array> | null cursor?: string + error?: string } export type SearchResponse = { diff --git a/package.json b/package.json index 9bdbfb2..675851b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@mondaycom/apps-sdk", - "version": "3.0.12", + "version": "3.0.12-beta", "description": "monday apps SDK for NodeJS", "main": "./dist/cjs/index.js", "module": "./dist/esm/index.js", diff --git a/tests/storage/storage.test.ts b/tests/storage/storage.test.ts index 4c400f8..7990947 100644 --- a/tests/storage/storage.test.ts +++ b/tests/storage/storage.test.ts @@ -64,5 +64,17 @@ describe('Storage', () => { await expect(async () => await storage.search(term)).rejects.toThrow(InternalServerError); expect(fetch).toHaveBeenCalledTimes(1); }); + + it('Should return error that cannot search with empty term', async () => { + // Arrange + const storage = new Storage(FAKE_TOKEN); + + // Act + const res = await storage.search(''); + + // Assert + expect(res).toEqual({ success: false, error: 'key cannot be empty', records: null }); + expect(fetch).toHaveBeenCalledTimes(0); + }); }); });