mirror of
https://github.com/jdalrymple/gitbeaker.git
synced 2026-01-18 15:55:30 +00:00
Extend support for the reformat function & add tests for verification (#3576)
This commit is contained in:
parent
b30c703ee8
commit
bc3f390ddb
@ -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;
|
||||
|
||||
|
||||
@ -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' });
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user