(id, params)
| 239 | * @returns {string} |
| 240 | */ |
| 241 | export function resolve_route(id, params) { |
| 242 | const segments = get_route_segments(id); |
| 243 | return ( |
| 244 | '/' + |
| 245 | segments |
| 246 | .map((segment) => |
| 247 | segment.replace(basic_param_pattern, (_, optional, rest, name) => { |
| 248 | const param_value = params[name]; |
| 249 | |
| 250 | // This is nested so TS correctly narrows the type |
| 251 | if (!param_value) { |
| 252 | if (optional) return ''; |
| 253 | if (rest && param_value !== undefined) return ''; |
| 254 | throw new Error(`Missing parameter '${name}' in route ${id}`); |
| 255 | } |
| 256 | |
| 257 | if (param_value.startsWith('/') || param_value.endsWith('/')) |
| 258 | throw new Error( |
| 259 | `Parameter '${name}' in route ${id} cannot start or end with a slash -- this would cause an invalid route like foo//bar` |
| 260 | ); |
| 261 | return param_value; |
| 262 | }) |
| 263 | ) |
| 264 | .filter(Boolean) |
| 265 | .join('/') |
| 266 | ); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * @param {import('types').SSRNode} node |
no test coverage detected