fix(buildQueryString): change the way the searchParams built

This commit is contained in:
Ilia Mashkov
2026-04-03 16:08:15 +03:00
parent 37e0c29788
commit bc4ab58644

View File

@@ -7,7 +7,7 @@
* @example * @example
* ```ts * ```ts
* buildQueryString({ category: 'serif', subsets: ['latin', 'latin-ext'] }) * buildQueryString({ category: 'serif', subsets: ['latin', 'latin-ext'] })
* // Returns: "?category=serif&subsets=latin%2Clatin-ext" * // Returns: "?category=serif&subsets=latin&subsets=latin-ext"
* *
* buildQueryString({ limit: 50, page: 1 }) * buildQueryString({ limit: 50, page: 1 })
* // Returns: "?limit=50&page=1" * // Returns: "?limit=50&page=1"
@@ -16,7 +16,7 @@
* // Returns: "" * // Returns: ""
* *
* buildQueryString({ search: 'hello world', active: true }) * buildQueryString({ search: 'hello world', active: true })
* // Returns: "?search=hello%20world&active=true" * // Returns: "?search=hello+world&active=true"
* ``` * ```
*/ */
@@ -35,7 +35,7 @@ export type QueryParams = Record<string, QueryParamValue | undefined | null>;
* *
* Handles: * Handles:
* - Primitive values (string, number, boolean) - converted to strings * - Primitive values (string, number, boolean) - converted to strings
* - Arrays - comma-separated values * - Arrays - multiple parameters with same key (e.g., ?key=1&key=2&key=3)
* - null/undefined - omitted from output * - null/undefined - omitted from output
* - Special characters - URL encoded * - Special characters - URL encoded
* *
@@ -51,14 +51,12 @@ export function buildQueryString(params: QueryParams): string {
continue; continue;
} }
// Handle arrays (comma-separated values) // Handle arrays - append each item as separate parameter with same key
if (Array.isArray(value)) { if (Array.isArray(value)) {
const joined = value for (const item of value) {
.filter(item => item !== undefined && item !== null) if (item !== undefined && item !== null) {
.map(String) searchParams.append(key, String(item));
.join(','); }
if (joined) {
searchParams.append(key, joined);
} }
} else { } else {
// Handle primitives // Handle primitives