Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(project): fix object stringifying #832

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/resources/Resource.ts
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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}` : '';
}
Expand Down
6 changes: 3 additions & 3 deletions src/resources/Search/SearchInterfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 8 additions & 2 deletions src/resources/Search/test/Search.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
);
});
});

Expand Down
9 changes: 9 additions & 0 deletions src/utils/stringifyNestedObjects.ts
Original file line number Diff line number Diff line change
@@ -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;
};
19 changes: 19 additions & 0 deletions src/utils/tests/stringifyNestedObjects.spec.ts
Original file line number Diff line number Diff line change
@@ -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"}',
});
});
});