| 1 |
import { match } from 'path-to-regexp'; |
| 2 |
|
| 3 |
import type { Path, MatchFunction } from 'path-to-regexp'; |
| 4 |
|
| 5 |
export { type Path }; |
| 6 |
|
| 7 |
type Params = Parameters<typeof match>; |
| 8 |
|
| 9 |
/** Create a match function from a path pattern that checks if a URLs matches it. */ |
| 10 |
export const matchPath = <P extends object = object>( |
| 11 |
path: Params[0], |
| 12 |
options?: Params[1] |
| 13 |
): MatchFunction<P> => { |
| 14 |
if (Array.isArray(path) && !path.length) { |
| 15 |
path = ''; |
| 16 |
} |
| 17 |
|
| 18 |
try { |
| 19 |
return match<P>(path, options); |
| 20 |
} catch (error) { |
| 21 |
throw new Error(`[swup] Error parsing path "${String(path)}":\n${String(error)}`); |
| 22 |
} |
| 23 |
}; |