blob: 4a1f65c552764e1f9b2c3651cc48c11b180a8907 [file] [log] [blame]
Paul Lewis40e28672020-11-27 09:51:37 +00001declare function pathToRegexp (path: pathToRegexp.Path, options?: pathToRegexp.RegExpOptions & pathToRegexp.ParseOptions): pathToRegexp.PathRegExp;
2declare function pathToRegexp (path: pathToRegexp.Path, keys?: pathToRegexp.Key[], options?: pathToRegexp.RegExpOptions & pathToRegexp.ParseOptions): pathToRegexp.PathRegExp;
3
4declare namespace pathToRegexp {
5 export interface PathRegExp extends RegExp {
6 // An array to be populated with the keys found in the path.
7 keys: Key[];
8 }
9
10 export interface RegExpOptions {
11 /**
12 * When `true` the route will be case sensitive. (default: `false`)
13 */
14 sensitive?: boolean;
15 /**
16 * When `false` the trailing slash is optional. (default: `false`)
17 */
18 strict?: boolean;
19 /**
20 * When `false` the path will match at the beginning. (default: `true`)
21 */
22 end?: boolean;
23 /**
24 * Sets the final character for non-ending optimistic matches. (default: `/`)
25 */
26 delimiter?: string;
27 }
28
29 export interface ParseOptions {
30 /**
31 * Set the default delimiter for repeat parameters. (default: `'/'`)
32 */
33 delimiter?: string;
34 }
35
36 export interface TokensToFunctionOptions {
37 /**
38 * When `true` the regexp will be case sensitive. (default: `false`)
39 */
40 sensitive?: boolean;
41 }
42
43 /**
44 * Parse an Express-style path into an array of tokens.
45 */
46 export function parse (path: string, options?: ParseOptions): Token[];
47
48 /**
49 * Transforming an Express-style path into a valid path.
50 */
51 export function compile (path: string, options?: ParseOptions & TokensToFunctionOptions): PathFunction;
52
53 /**
54 * Transform an array of tokens into a path generator function.
55 */
56 export function tokensToFunction (tokens: Token[], options?: TokensToFunctionOptions): PathFunction;
57
58 /**
59 * Transform an array of tokens into a matching regular expression.
60 */
61 export function tokensToRegExp (tokens: Token[], options?: RegExpOptions): PathRegExp;
62 export function tokensToRegExp (tokens: Token[], keys?: Key[], options?: RegExpOptions): PathRegExp;
63
64 export interface Key {
65 name: string | number;
66 prefix: string;
67 delimiter: string;
68 optional: boolean;
69 repeat: boolean;
70 pattern: string;
71 partial: boolean;
72 asterisk: boolean;
73 }
74
75 interface PathFunctionOptions {
76 pretty?: boolean;
77 }
78
79 export type Token = string | Key;
80 export type Path = string | RegExp | Array<string | RegExp>;
81 export type PathFunction = (data?: Object, options?: PathFunctionOptions) => string;
82}
83
84export = pathToRegexp;