From bc3f390ddbd70ac1ad8e53cd70d0c5bc10db76dd Mon Sep 17 00:00:00 2001 From: Justin Dalrymple Date: Wed, 24 Apr 2024 21:22:41 -0400 Subject: [PATCH] Extend support for the reformat function & add tests for verification (#3576) --- packages/core/src/infrastructure/Utils.ts | 4 +-- .../core/test/unit/infrastructure/Utils.ts | 28 ++++++++++++++++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/packages/core/src/infrastructure/Utils.ts b/packages/core/src/infrastructure/Utils.ts index e2e9a833..678d09cf 100644 --- a/packages/core/src/infrastructure/Utils.ts +++ b/packages/core/src/infrastructure/Utils.ts @@ -74,13 +74,13 @@ export function reformatObjectOptions( obj: Record, prefixKey: string, decamelizeValues = false, -) { +): Record { const formatted = decamelizeValues ? decamelizeKeys(obj) : obj; return QS.stringify({ [prefixKey]: formatted }, { encode: false }) .split('&') .reduce((acc: Record, cur: string) => { - const [key, val] = cur.split('='); + const [key, val] = cur.split(/=(.*)/); acc[key] = val; diff --git a/packages/core/test/unit/infrastructure/Utils.ts b/packages/core/test/unit/infrastructure/Utils.ts index ef5228c3..124afff4 100644 --- a/packages/core/test/unit/infrastructure/Utils.ts +++ b/packages/core/test/unit/infrastructure/Utils.ts @@ -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', () => { @@ -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' }); + }); +});