Skip to content

Commit 605e2bc

Browse files
author
sergey
committed
fix: use URLSearchParams as source for request params and data
fixes hg-pyun#126
1 parent e9b2e21 commit 605e2bc

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

src/common/__test__/string-builder.spec.js

+15
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ test('makeParams should add params', () => {
5858
expect(result).toContain(JSON.stringify(params));
5959
});
6060

61+
test('makeParams should add params URLSearchParams', () => {
62+
const sb = new StringBuilder({ params: true });
63+
const params = new URLSearchParams({ param1: 'value1', param2: 'value2' });
64+
const result = sb.makeParams(params).build();
65+
66+
expect(result).toContain(JSON.stringify(params));
67+
});
68+
6169
test('makeMethod should add method with upper case', () => {
6270
const sb = new StringBuilder(getGlobalConfig());
6371
const result = sb.makeMethod('get').build();
@@ -72,6 +80,13 @@ test('makeStatus should add status', () => {
7280
expect(result).toContain('200:OK');
7381
});
7482

83+
test('makeData should string if URLSearchParams passed', () => {
84+
const data = new URLSearchParams({ param1: 'value1', param2: 'value2' });
85+
const sb = new StringBuilder(getGlobalConfig());
86+
const result = sb.makeData(data).build();
87+
expect(result).toContain(JSON.stringify(data));
88+
});
89+
7590
test('makeData should not stringify data if configured not to', () => {
7691
const config = {
7792
...getGlobalConfig(),

src/common/string-builder.ts

+18-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import dateformat from 'dateformat';
22
import { GlobalLogConfig } from './types';
33
import chalk from 'chalk';
44
import { AxiosResponse } from "axios/index";
5-
import { join } from "./utils";
5+
import { join, isURLSearchParams, convertURLSearchParamsToObject } from "./utils";
66

77
class StringBuilder {
88
private config: GlobalLogConfig;
@@ -54,7 +54,16 @@ class StringBuilder {
5454
}
5555

5656
makeParams(params?: object) {
57-
if(this.config.params && params) this.printQueue.push(JSON.stringify(params));
57+
if(this.config.params && params) {
58+
let str: string = '';
59+
if (isURLSearchParams(params)) {
60+
const obj = convertURLSearchParamsToObject(params as URLSearchParams);
61+
str = JSON.stringify(obj);
62+
} else {
63+
str = typeof params === `string` ? params : JSON.stringify(params);
64+
}
65+
this.printQueue.push(str);
66+
}
5867
return this;
5968
}
6069

@@ -65,7 +74,13 @@ class StringBuilder {
6574

6675
makeData(data: object) {
6776
if(this.config.data && data) {
68-
const str = typeof data === `string` ? data : JSON.stringify(data);
77+
let str: string = '';
78+
if (isURLSearchParams(data)) {
79+
const obj = convertURLSearchParamsToObject(data as URLSearchParams);
80+
str = JSON.stringify(obj);
81+
} else {
82+
str = typeof data === `string` ? data : JSON.stringify(data);
83+
}
6984
this.printQueue.push(str);
7085
}
7186
return this;

src/common/utils.ts

+12
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,15 @@ export function join(...paths: string[]) {
1313
.replace(/\/+/g, '/'); // Replace multiple slashes with a single slash
1414

1515
}
16+
17+
export function isURLSearchParams(val: object) {
18+
return typeof URLSearchParams !== 'undefined' && val instanceof URLSearchParams;
19+
}
20+
21+
export function convertURLSearchParamsToObject(params: URLSearchParams) {
22+
let arr: object = {};
23+
for (const obj of params.entries()) {
24+
Object.defineProperty(arr, obj[0], { value: obj[1]});
25+
}
26+
return arr;
27+
}

0 commit comments

Comments
 (0)