forked from clerk/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathToRegexp.ts
More file actions
36 lines (33 loc) · 1.1 KB
/
Copy pathpathToRegexp.ts
File metadata and controls
36 lines (33 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import type {
Match,
MatchFunction,
ParseOptions,
Path,
RegexpToFunctionOptions,
TokensToRegexpOptions,
} from './compiled/path-to-regexp';
import { match as matchBase, pathToRegexp as pathToRegexpBase } from './compiled/path-to-regexp';
export const pathToRegexp = (path: string) => {
try {
// @ts-ignore no types exists for the pre-compiled package
return pathToRegexpBase(path);
} catch (e: any) {
throw new Error(
`Invalid path: ${path}.\nConsult the documentation of path-to-regexp here: https://github.com/pillarjs/path-to-regexp/tree/6.x\n${e.message}`,
);
}
};
export function match<P extends object = object>(
str: Path,
options?: ParseOptions & TokensToRegexpOptions & RegexpToFunctionOptions,
): MatchFunction<P> {
try {
// @ts-ignore no types exists for the pre-compiled package
return matchBase(str, options);
} catch (e: any) {
throw new Error(
`Invalid path and options: Consult the documentation of path-to-regexp here: https://github.com/pillarjs/path-to-regexp/tree/6.x\n${e.message}`,
);
}
}
export { type Match, type MatchFunction };