Skip to content

Commit

Permalink
Extend support for the reformat function & add tests for verification (
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalrymple authored Apr 25, 2024
1 parent b30c703 commit bc3f390
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/infrastructure/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ export function reformatObjectOptions(
obj: Record<string, unknown>,
prefixKey: string,
decamelizeValues = false,
) {
): Record<string, string> {
const formatted = decamelizeValues ? decamelizeKeys(obj) : obj;

return QS.stringify({ [prefixKey]: formatted }, { encode: false })
.split('&')
.reduce((acc: Record<string, string>, cur: string) => {
const [key, val] = cur.split('=');
const [key, val] = cur.split(/=(.*)/);

acc[key] = val;

Expand Down
28 changes: 27 additions & 1 deletion packages/core/test/unit/infrastructure/Utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { appendFormFromObject, endpoint } from '../../../src/infrastructure';
import { appendFormFromObject, endpoint, reformatObjectOptions } from '../../../src/infrastructure';

describe('appendFormFromObject', () => {
it('should convert object key/values to formdata instance', () => {
Expand Down Expand Up @@ -35,3 +35,29 @@ describe('endpoint', () => {
expect(url).toBe('/projects/1');
});
});

describe('reformatObjectOptions', () => {
it('should convert simple nested object to be query parameter friendly', () => {
const data = {
a: {
b: 'test',
},
};

const formatted = reformatObjectOptions(data, 'test');

expect(formatted).toMatchObject({ 'test[a][b]': 'test' });
});

it('should convert nested object with "=" characters in the value', () => {
const data = {
a: {
b: '=5',
},
};

const formatted = reformatObjectOptions(data, 'test');

expect(formatted).toMatchObject({ 'test[a][b]': '=5' });
});
});

0 comments on commit bc3f390

Please sign in to comment.