Skip to content

Commit

Permalink
feat(storage): [beta] added error propagation and more input validati…
Browse files Browse the repository at this point in the history
…on to search function
  • Loading branch information
royWeisselberg committed Dec 16, 2024
1 parent 02edcfd commit a087a31
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/minimal-package.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default { name: '@mondaycom/apps-sdk', version: '3.0.12' };
export default { name: '@mondaycom/apps-sdk', version: '3.0.12-beta' };
7 changes: 7 additions & 0 deletions lib/storage/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,20 @@ export class Storage extends BaseStorage implements IStorageInstance {
}

async search<T extends JsonValue>(key: string, options: SearchOptions = {}): Promise<SearchResponse<T>> {
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<SearchServerResponse<T>>(url, params);
if (!isDefined(result)) {
return { success: false, records: null };
}

if (result.error) {
return { success: false, error: result.error, records: null };
}

const response: SearchResponse<T> = { success: true, records: result.records };
if (result.cursor) {
response.cursor = result.cursor;
Expand Down
1 change: 1 addition & 0 deletions lib/types/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export type SearchEntity<T extends JsonValue> = {
export type SearchServerResponse<T extends JsonValue> = {
records: Array<SearchEntity<T>> | null
cursor?: string
error?: string
}

export type SearchResponse<T extends JsonValue> = {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
12 changes: 12 additions & 0 deletions tests/storage/storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});

0 comments on commit a087a31

Please sign in to comment.