Skip to content

Commit

Permalink
fix: use URLSearchParams as source for request params and data
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey committed Jun 26, 2024
1 parent e9b2e21 commit 605e2bc
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src/common/__test__/string-builder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ test('makeParams should add params', () => {
expect(result).toContain(JSON.stringify(params));
});

test('makeParams should add params URLSearchParams', () => {
const sb = new StringBuilder({ params: true });
const params = new URLSearchParams({ param1: 'value1', param2: 'value2' });
const result = sb.makeParams(params).build();

expect(result).toContain(JSON.stringify(params));
});

test('makeMethod should add method with upper case', () => {
const sb = new StringBuilder(getGlobalConfig());
const result = sb.makeMethod('get').build();
Expand All @@ -72,6 +80,13 @@ test('makeStatus should add status', () => {
expect(result).toContain('200:OK');
});

test('makeData should string if URLSearchParams passed', () => {
const data = new URLSearchParams({ param1: 'value1', param2: 'value2' });
const sb = new StringBuilder(getGlobalConfig());
const result = sb.makeData(data).build();
expect(result).toContain(JSON.stringify(data));
});

test('makeData should not stringify data if configured not to', () => {
const config = {
...getGlobalConfig(),
Expand Down
21 changes: 18 additions & 3 deletions src/common/string-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import dateformat from 'dateformat';
import { GlobalLogConfig } from './types';
import chalk from 'chalk';
import { AxiosResponse } from "axios/index";
import { join } from "./utils";
import { join, isURLSearchParams, convertURLSearchParamsToObject } from "./utils";

class StringBuilder {
private config: GlobalLogConfig;
Expand Down Expand Up @@ -54,7 +54,16 @@ class StringBuilder {
}

makeParams(params?: object) {
if(this.config.params && params) this.printQueue.push(JSON.stringify(params));
if(this.config.params && params) {
let str: string = '';
if (isURLSearchParams(params)) {
const obj = convertURLSearchParamsToObject(params as URLSearchParams);
str = JSON.stringify(obj);
} else {
str = typeof params === `string` ? params : JSON.stringify(params);
}
this.printQueue.push(str);
}
return this;
}

Expand All @@ -65,7 +74,13 @@ class StringBuilder {

makeData(data: object) {
if(this.config.data && data) {
const str = typeof data === `string` ? data : JSON.stringify(data);
let str: string = '';
if (isURLSearchParams(data)) {
const obj = convertURLSearchParamsToObject(data as URLSearchParams);
str = JSON.stringify(obj);
} else {
str = typeof data === `string` ? data : JSON.stringify(data);
}
this.printQueue.push(str);
}
return this;
Expand Down
12 changes: 12 additions & 0 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,15 @@ export function join(...paths: string[]) {
.replace(/\/+/g, '/'); // Replace multiple slashes with a single slash

}

export function isURLSearchParams(val: object) {
return typeof URLSearchParams !== 'undefined' && val instanceof URLSearchParams;
}

export function convertURLSearchParamsToObject(params: URLSearchParams) {
let arr: object = {};
for (const obj of params.entries()) {
Object.defineProperty(arr, obj[0], { value: obj[1]});
}
return arr;
}

0 comments on commit 605e2bc

Please sign in to comment.