Skip to content

Commit fa13fcb

Browse files
committed
Migrate to URLSearchParams
1 parent fe751be commit fa13fcb

File tree

1 file changed

+7
-24
lines changed

1 file changed

+7
-24
lines changed

src/util.ts

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,13 @@ export function getJSON(
5757
callback: (message: any) => void
5858
): void {
5959
const headers = { Accept: 'application/json' };
60-
fetch(url + getParamString(params), { headers })
60+
const request = new URL(url);
61+
Object.entries(params).forEach(([key, value]) => {
62+
(Array.isArray(value) ? value : [value]).forEach(v => {
63+
request.searchParams.append(key, v);
64+
});
65+
});
66+
fetch(request.toString(), { headers })
6167
.then(response => response.json())
6268
.then(j => callback(j));
6369
}
@@ -76,26 +82,3 @@ export function template(str: string, data: Record<string, any>): string {
7682
return htmlEscape(value);
7783
});
7884
}
79-
80-
/**
81-
* @internal
82-
*/
83-
export function getParamString(
84-
obj: Record<string, unknown | unknown[]>,
85-
existingUrl?: string,
86-
uppercase?: boolean
87-
): string {
88-
const params = [];
89-
for (const i in obj) {
90-
const key = encodeURIComponent(uppercase ? i.toUpperCase() : i);
91-
const value = obj[i];
92-
if (!Array.isArray(value)) {
93-
params.push(key + '=' + encodeURIComponent(String(value)));
94-
} else {
95-
for (let j = 0; j < value.length; j++) {
96-
params.push(key + '=' + encodeURIComponent(value[j]));
97-
}
98-
}
99-
}
100-
return (!existingUrl || existingUrl.indexOf('?') === -1 ? '?' : '&') + params.join('&');
101-
}

0 commit comments

Comments
 (0)