( path: string, root: string, includeLast: boolean = false, )
| 298 | } |
| 299 | |
| 300 | export function patternToSegments( |
| 301 | path: string, |
| 302 | root: string, |
| 303 | includeLast: boolean = false, |
| 304 | ): string[] { |
| 305 | const out: string[] = [root]; |
| 306 | |
| 307 | if (path === "/" || path === "*" || path === "/*") return out; |
| 308 | |
| 309 | // Strip optional groups like {/:param}? before segmenting, so that |
| 310 | // /api{/:opt}?/endpoint produces the same segments as /api/endpoint. |
| 311 | // This ensures middleware registered at /api applies to routes with |
| 312 | // optional parameters under /api. |
| 313 | const cleaned = path.replace(/\{[^}]*\}\??/g, ""); |
| 314 | |
| 315 | let start = -1; |
| 316 | for (let i = 0; i < cleaned.length; i++) { |
| 317 | const ch = cleaned[i]; |
| 318 | |
| 319 | if (ch === "/") { |
| 320 | if (i > 0) { |
| 321 | const raw = cleaned.slice(start + 1, i); |
| 322 | out.push(raw); |
| 323 | } |
| 324 | start = i; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | if (includeLast && start < cleaned.length - 1) { |
| 329 | out.push(cleaned.slice(start + 1)); |
| 330 | } |
| 331 | |
| 332 | return out; |
| 333 | } |
| 334 | |
| 335 | export function mergePath( |
| 336 | basePath: string, |
no outgoing calls
no test coverage detected