* Attempts to match a route against the current URL and conditions
( route: Route, url: URL, headers: Headers )
| 19 | * Attempts to match a route against the current URL and conditions |
| 20 | */ |
| 21 | function matchRoute( |
| 22 | route: Route, |
| 23 | url: URL, |
| 24 | headers: Headers |
| 25 | ): { |
| 26 | matched: boolean |
| 27 | destination?: string |
| 28 | headers?: Record<string, string> |
| 29 | regexMatches?: RegExpMatchArray |
| 30 | hasCaptures?: Record<string, string> |
| 31 | } { |
| 32 | // Check if source regex matches the pathname |
| 33 | const regex = new RegExp(route.sourceRegex) |
| 34 | const regexMatches = url.pathname.match(regex) |
| 35 | |
| 36 | if (!regexMatches) { |
| 37 | return { matched: false } |
| 38 | } |
| 39 | |
| 40 | // Check has conditions |
| 41 | const hasResult = checkHasConditions(route.has, url, headers) |
| 42 | if (!hasResult.matched) { |
| 43 | return { matched: false } |
| 44 | } |
| 45 | |
| 46 | // Check missing conditions |
| 47 | const missingMatched = checkMissingConditions(route.missing, url, headers) |
| 48 | if (!missingMatched) { |
| 49 | return { matched: false } |
| 50 | } |
| 51 | |
| 52 | // Replace placeholders in destination |
| 53 | const destination = route.destination |
| 54 | ? replaceDestination(route.destination, regexMatches, hasResult.captures) |
| 55 | : undefined |
| 56 | const resolvedHeaders = route.headers |
| 57 | ? Object.fromEntries( |
| 58 | Object.entries(route.headers).map(([key, value]) => [ |
| 59 | replaceDestination(key, regexMatches, hasResult.captures), |
| 60 | replaceDestination(value, regexMatches, hasResult.captures), |
| 61 | ]) |
| 62 | ) |
| 63 | : undefined |
| 64 | |
| 65 | return { |
| 66 | matched: true, |
| 67 | destination, |
| 68 | headers: resolvedHeaders, |
| 69 | regexMatches, |
| 70 | hasCaptures: hasResult.captures, |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Processes a list of routes and updates the URL if any match |
no test coverage detected