(path: string, params: unknown)
| 35 | } |
| 36 | |
| 37 | function replacePathParams(path: string, params: unknown): string { |
| 38 | if (!params || typeof params !== 'object') return path |
| 39 | |
| 40 | const values = params as Record<string, unknown> |
| 41 | return path.replace( |
| 42 | /\[\[?(\.\.\.)?([^\][]+)\]\]?/g, |
| 43 | (match, rest: string | undefined, key: string) => { |
| 44 | const value = values[key] |
| 45 | const isOptionalCatchAll = match.startsWith('[[...') |
| 46 | |
| 47 | if (rest && Array.isArray(value)) { |
| 48 | return value.map((item) => encodeURIComponent(String(item))).join('/') |
| 49 | } |
| 50 | |
| 51 | if (value === undefined && isOptionalCatchAll) return '' |
| 52 | |
| 53 | if (typeof value !== 'string' && typeof value !== 'number' && typeof value !== 'boolean') { |
| 54 | throw new Error(`Missing route param "${key}"`) |
| 55 | } |
| 56 | |
| 57 | return encodeURIComponent(String(value)) |
| 58 | } |
| 59 | ) |
| 60 | } |
| 61 | |
| 62 | function appendQuery(path: string, query: unknown): string { |
| 63 | if (!query || typeof query !== 'object') return path |
no test coverage detected