Extend support for the reformat function & add tests for verification (#3576)

This commit is contained in:
Justin Dalrymple 2024-04-24 21:22:41 -04:00 committed by GitHub
parent b30c703ee8
commit bc3f390ddb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 3 deletions

View File

@ -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;

View File

@ -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' });
});
});