From 89721b3cbec8ded04dd80de54ebaa282d9435447 Mon Sep 17 00:00:00 2001 From: Sam Thomas Date: Wed, 12 Jun 2024 12:14:22 +0100 Subject: [PATCH] fix(project): fix object stringifying --- src/resources/Resource.ts | 5 +++++ src/resources/Search/SearchInterfaces.ts | 6 +++--- src/resources/Search/test/Search.spec.ts | 10 ++++++++-- src/utils/stringifyNestedObjects.ts | 9 +++++++++ .../tests/stringifyNestedObjects.spec.ts | 19 +++++++++++++++++++ 5 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 src/utils/stringifyNestedObjects.ts create mode 100644 src/utils/tests/stringifyNestedObjects.spec.ts diff --git a/src/resources/Resource.ts b/src/resources/Resource.ts index 200749793..4f62aba4a 100644 --- a/src/resources/Resource.ts +++ b/src/resources/Resource.ts @@ -1,4 +1,6 @@ import API from '../APICore.js'; +import {stringifyNestedObjects} from '../utils/stringifyNestedObjects.js'; + import queryString from '#query-string'; const defaultOptions: queryString.StringifyOptions = {skipEmptyString: true, skipNull: true, sort: false}; @@ -19,6 +21,9 @@ class Resource { if (!parameters) { return ''; } else { + if (typeof parameters === 'object' && !Array.isArray(parameters)) { + parameters = stringifyNestedObjects(parameters); + } const requestURL = queryString.stringify(parameters, {...defaultOptions, ...userOptions}); return requestURL.length ? `?${requestURL}` : ''; } diff --git a/src/resources/Search/SearchInterfaces.ts b/src/resources/Search/SearchInterfaces.ts index 83415ac97..9d0483518 100644 --- a/src/resources/Search/SearchInterfaces.ts +++ b/src/resources/Search/SearchInterfaces.ts @@ -204,9 +204,9 @@ export interface RestUserId { } export interface RestCommerceParameters { - catalogId: string; - filter: string; - operation: string; + catalogId?: string; + filter?: string; + operation?: string; } export interface TokenModel { diff --git a/src/resources/Search/test/Search.spec.ts b/src/resources/Search/test/Search.spec.ts index 0fb6bb040..a6677efc5 100644 --- a/src/resources/Search/test/Search.spec.ts +++ b/src/resources/Search/test/Search.spec.ts @@ -95,11 +95,17 @@ describe('Search', () => { describe('getFieldValue', () => { it('should make a get call to searchAPI correct url with its params to fetch the values of a field', () => { - const params = {ignoreAccents: false}; + const params = { + ignoreAccents: false, + commerce: {catalogId: 'test-id', filter: 'test-filter', operation: 'test-operation'}, + organizationId: 'test-org-id', + }; const fieldName = 'author'; search.getFieldValues(fieldName, params); expect(api.get).toHaveBeenCalledTimes(1); - expect(api.get).toHaveBeenCalledWith(`${Search.baseUrl}/values?field=author&ignoreAccents=false`); + expect(api.get).toHaveBeenCalledWith( + `${Search.baseUrl}/values?field=author&ignoreAccents=false&commerce=%7B%22catalogId%22%3A%22test-id%22%2C%22filter%22%3A%22test-filter%22%2C%22operation%22%3A%22test-operation%22%7D&organizationId=test-org-id`, + ); }); }); diff --git a/src/utils/stringifyNestedObjects.ts b/src/utils/stringifyNestedObjects.ts new file mode 100644 index 000000000..7a5519093 --- /dev/null +++ b/src/utils/stringifyNestedObjects.ts @@ -0,0 +1,9 @@ +export const stringifyNestedObjects = (obj: object): object => { + Object.keys(obj).forEach((key) => { + const value = obj[key]; + if (value && typeof value === 'object' && !Array.isArray(value)) { + obj[key] = JSON.stringify(value, null, 0); + } + }); + return obj; +}; diff --git a/src/utils/tests/stringifyNestedObjects.spec.ts b/src/utils/tests/stringifyNestedObjects.spec.ts new file mode 100644 index 000000000..efdf94c41 --- /dev/null +++ b/src/utils/tests/stringifyNestedObjects.spec.ts @@ -0,0 +1,19 @@ +import {stringifyNestedObjects} from '../stringifyNestedObjects.js'; + +describe('stringifyNestedObjects', () => { + it('should stringify a nested object', () => { + expect(stringifyNestedObjects({commerce: {catalogId: 'test'}})).toEqual({commerce: '{"catalogId":"test"}'}); + }); + it('should not stringify booleans', () => { + expect(stringifyNestedObjects({commerce: {catalogId: 'test'}, bool: true})).toEqual({ + bool: true, + commerce: '{"catalogId":"test"}', + }); + }); + it('should not stringify arrays', () => { + expect(stringifyNestedObjects({commerce: {catalogId: 'test'}, arr: ['1', '2']})).toEqual({ + arr: ['1', '2'], + commerce: '{"catalogId":"test"}', + }); + }); +});