| | import { normalizeAppPath } from './app-paths' |
| |
|
| | |
| | export const INTERCEPTION_ROUTE_MARKERS = [ |
| | '(..)(..)', |
| | '(.)', |
| | '(..)', |
| | '(...)', |
| | ] as const |
| |
|
| | export function isInterceptionRouteAppPath(path: string): boolean { |
| | |
| | return ( |
| | path |
| | .split('/') |
| | .find((segment) => |
| | INTERCEPTION_ROUTE_MARKERS.find((m) => segment.startsWith(m)) |
| | ) !== undefined |
| | ) |
| | } |
| |
|
| | export function extractInterceptionRouteInformation(path: string) { |
| | let interceptingRoute: string | undefined, |
| | marker: (typeof INTERCEPTION_ROUTE_MARKERS)[number] | undefined, |
| | interceptedRoute: string | undefined |
| |
|
| | for (const segment of path.split('/')) { |
| | marker = INTERCEPTION_ROUTE_MARKERS.find((m) => segment.startsWith(m)) |
| | if (marker) { |
| | ;[interceptingRoute, interceptedRoute] = path.split(marker, 2) |
| | break |
| | } |
| | } |
| |
|
| | if (!interceptingRoute || !marker || !interceptedRoute) { |
| | throw new Error( |
| | `Invalid interception route: ${path}. Must be in the format /<intercepting route>/(..|...|..)(..)/<intercepted route>` |
| | ) |
| | } |
| |
|
| | interceptingRoute = normalizeAppPath(interceptingRoute) |
| |
|
| | switch (marker) { |
| | case '(.)': |
| | |
| | if (interceptingRoute === '/') { |
| | interceptedRoute = `/${interceptedRoute}` |
| | } else { |
| | interceptedRoute = interceptingRoute + '/' + interceptedRoute |
| | } |
| | break |
| | case '(..)': |
| | |
| | if (interceptingRoute === '/') { |
| | throw new Error( |
| | `Invalid interception route: ${path}. Cannot use (..) marker at the root level, use (.) instead.` |
| | ) |
| | } |
| | interceptedRoute = interceptingRoute |
| | .split('/') |
| | .slice(0, -1) |
| | .concat(interceptedRoute) |
| | .join('/') |
| | break |
| | case '(...)': |
| | |
| | interceptedRoute = '/' + interceptedRoute |
| | break |
| | case '(..)(..)': |
| | |
| |
|
| | const splitInterceptingRoute = interceptingRoute.split('/') |
| | if (splitInterceptingRoute.length <= 2) { |
| | throw new Error( |
| | `Invalid interception route: ${path}. Cannot use (..)(..) marker at the root level or one level up.` |
| | ) |
| | } |
| |
|
| | interceptedRoute = splitInterceptingRoute |
| | .slice(0, -2) |
| | .concat(interceptedRoute) |
| | .join('/') |
| | break |
| | default: |
| | throw new Error('Invariant: unexpected marker') |
| | } |
| |
|
| | return { interceptingRoute, interceptedRoute } |
| | } |
| |
|