|
| 1 | +/** |
| 2 | + * Native query string serialization that replaces the qs library. |
| 3 | + * Maintains backward compatibility with existing URL formatting. |
| 4 | + * |
| 5 | + * This implementation: |
| 6 | + * - Uses only native Web APIs (works in all runtimes) |
| 7 | + * - Handles arrays with 'repeat' format: scope=read&scope=write |
| 8 | + * - Sorts keys alphabetically for consistency |
| 9 | + * - Uses RFC1738 encoding (space as +) |
| 10 | + * - Handles nested objects: provider_query_params[key]=value |
| 11 | + * - Filters out undefined values |
| 12 | + */ |
| 13 | + |
| 14 | +type QueryValue = string | string[] | Record<string, string | boolean | number> | undefined; |
| 15 | + |
| 16 | +/** |
| 17 | + * Converts an options object to a query string. |
| 18 | + * Compatible with the qs library's stringify method when used with: |
| 19 | + * - arrayFormat: 'repeat' |
| 20 | + * - format: 'RFC1738' |
| 21 | + * - sort: alphabetical |
| 22 | + */ |
| 23 | +export function toQueryString(options: Record<string, QueryValue>): string { |
| 24 | + const params: Array<[string, string]> = []; |
| 25 | + |
| 26 | + // Get sorted keys for consistent output (matches qs behavior) |
| 27 | + const sortedKeys = Object.keys(options).sort((a, b) => a.localeCompare(b)); |
| 28 | + |
| 29 | + for (const key of sortedKeys) { |
| 30 | + const value = options[key]; |
| 31 | + |
| 32 | + // Skip undefined values (matches qs behavior) |
| 33 | + if (value === undefined) { |
| 34 | + continue; |
| 35 | + } |
| 36 | + |
| 37 | + // Handle arrays with 'repeat' format: key=val1&key=val2 |
| 38 | + if (Array.isArray(value)) { |
| 39 | + for (const item of value) { |
| 40 | + params.push([key, String(item)]); |
| 41 | + } |
| 42 | + } |
| 43 | + // Handle nested objects: key[subkey]=value |
| 44 | + else if (typeof value === 'object' && value !== null) { |
| 45 | + const sortedSubKeys = Object.keys(value).sort((a, b) => a.localeCompare(b)); |
| 46 | + for (const subKey of sortedSubKeys) { |
| 47 | + const subValue = value[subKey]; |
| 48 | + if (subValue !== undefined) { |
| 49 | + params.push([`${key}[${subKey}]`, String(subValue)]); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + // Handle primitives (string, number, boolean) |
| 54 | + else { |
| 55 | + params.push([key, String(value)]); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // Build query string with RFC1738 encoding (space as +) |
| 60 | + return params |
| 61 | + .map(([key, value]) => { |
| 62 | + // Encode using RFC1738 format (matches qs behavior) |
| 63 | + const encodedKey = encodeRFC1738(key); |
| 64 | + const encodedValue = encodeRFC1738(value); |
| 65 | + return `${encodedKey}=${encodedValue}`; |
| 66 | + }) |
| 67 | + .join('&'); |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Encodes a string using RFC1738 format. |
| 72 | + * - Space is encoded as + |
| 73 | + * - Additional characters encoded to match qs library behavior |
| 74 | + */ |
| 75 | +function encodeRFC1738(str: string): string { |
| 76 | + return encodeURIComponent(str) |
| 77 | + .replace(/%20/g, '+') // Space as + (RFC1738) |
| 78 | + .replace(/[!'*]/g, (c) => { |
| 79 | + // Encode additional characters to match qs behavior |
| 80 | + return '%' + c.charCodeAt(0).toString(16).toUpperCase(); |
| 81 | + }); |
| 82 | +} |
0 commit comments