(path: string, query: unknown)
| 60 | } |
| 61 | |
| 62 | function appendQuery(path: string, query: unknown): string { |
| 63 | if (!query || typeof query !== 'object') return path |
| 64 | |
| 65 | const searchParams = new URLSearchParams() |
| 66 | for (const [key, value] of Object.entries(query as Record<string, unknown>)) { |
| 67 | if (value === undefined || value === null || value === '') continue |
| 68 | |
| 69 | if (Array.isArray(value)) { |
| 70 | for (const item of value) { |
| 71 | if (item === undefined || item === null || item === '') continue |
| 72 | // A non-scalar in a query array would stringify to "[object Object]" and |
| 73 | // silently corrupt the request. Encode such values as a single JSON |
| 74 | // string param and decode them server-side instead. Failing loudly here |
| 75 | // keeps the boundary honest (this is how the knowledge tagFilters bug |
| 76 | // shipped undetected). |
| 77 | if (typeof item === 'object') { |
| 78 | throw new Error( |
| 79 | `Cannot serialize query param "${key}": arrays of objects are not URL-safe — ` + |
| 80 | 'encode the value as a JSON string param and decode it server-side.' |
| 81 | ) |
| 82 | } |
| 83 | searchParams.append(key, String(item)) |
| 84 | } |
| 85 | continue |
| 86 | } |
| 87 | |
| 88 | if (typeof value === 'object') { |
| 89 | searchParams.set(key, JSON.stringify(value)) |
| 90 | continue |
| 91 | } |
| 92 | |
| 93 | searchParams.set(key, String(value)) |
| 94 | } |
| 95 | |
| 96 | const queryString = searchParams.toString() |
| 97 | if (!queryString) return path |
| 98 | |
| 99 | return `${path}${path.includes('?') ? '&' : '?'}${queryString}` |
| 100 | } |
| 101 | |
| 102 | function buildHeaders(headers: unknown, hasBody: boolean): Record<string, string> { |
| 103 | const output: Record<string, string> = {} |
no test coverage detected