UNPKG

4.11 kBTypeScriptView Raw
1/**
2 * Encode a string into another string.
3 */
4export type Encode = (value: string) => string;
5/**
6 * Decode a string into another string.
7 */
8export type Decode = (value: string) => string;
9export interface ParseOptions {
10 /**
11 * The default delimiter for segments. (default: `'/'`)
12 */
13 delimiter?: string;
14 /**
15 * A function for encoding input strings.
16 */
17 encodePath?: Encode;
18}
19export interface PathToRegexpOptions extends ParseOptions {
20 /**
21 * Regexp will be case sensitive. (default: `false`)
22 */
23 sensitive?: boolean;
24 /**
25 * Allow the delimiter to be arbitrarily repeated. (default: `true`)
26 */
27 loose?: boolean;
28 /**
29 * Verify patterns are valid and safe to use. (default: `false`)
30 */
31 strict?: boolean;
32 /**
33 * Match from the beginning of the string. (default: `true`)
34 */
35 start?: boolean;
36 /**
37 * Match to the end of the string. (default: `true`)
38 */
39 end?: boolean;
40 /**
41 * Allow optional trailing delimiter to match. (default: `true`)
42 */
43 trailing?: boolean;
44}
45export interface MatchOptions extends PathToRegexpOptions {
46 /**
47 * Function for decoding strings for params, or `false` to disable entirely. (default: `decodeURIComponent`)
48 */
49 decode?: Decode | false;
50}
51export interface CompileOptions extends ParseOptions {
52 /**
53 * Regexp will be case sensitive. (default: `false`)
54 */
55 sensitive?: boolean;
56 /**
57 * Allow the delimiter to be arbitrarily repeated. (default: `true`)
58 */
59 loose?: boolean;
60 /**
61 * Verify patterns are valid and safe to use. (default: `false`)
62 */
63 strict?: boolean;
64 /**
65 * Verifies the function is producing a valid path. (default: `true`)
66 */
67 validate?: boolean;
68 /**
69 * Function for encoding input strings for output into the path, or `false` to disable entirely. (default: `encodeURIComponent`)
70 */
71 encode?: Encode | false;
72}
73/**
74 * Tokenized path instance. Can we passed around instead of string.
75 */
76export declare class TokenData {
77 readonly tokens: Token[];
78 readonly delimiter: string;
79 constructor(tokens: Token[], delimiter: string);
80}
81/**
82 * Parse a string for the raw tokens.
83 */
84export declare function parse(str: string, options?: ParseOptions): TokenData;
85/**
86 * Compile a string to a template function for the path.
87 */
88export declare function compile<P extends ParamData = ParamData>(path: Path, options?: CompileOptions): PathFunction<P>;
89export type ParamData = Partial<Record<string, string | string[]>>;
90export type PathFunction<P extends ParamData> = (data?: P) => string;
91/**
92 * A match result contains data about the path match.
93 */
94export interface MatchResult<P extends ParamData> {
95 path: string;
96 index: number;
97 params: P;
98}
99/**
100 * A match is either `false` (no match) or a match result.
101 */
102export type Match<P extends ParamData> = false | MatchResult<P>;
103/**
104 * The match function takes a string and returns whether it matched the path.
105 */
106export type MatchFunction<P extends ParamData> = (path: string) => Match<P>;
107/**
108 * Create path match function from `path-to-regexp` spec.
109 */
110export declare function match<P extends ParamData>(path: Path, options?: MatchOptions): MatchFunction<P>;
111/**
112 * A key is a capture group in the regex.
113 */
114export interface Key {
115 name: string;
116 prefix?: string;
117 suffix?: string;
118 pattern?: string;
119 modifier?: string;
120 separator?: string;
121}
122/**
123 * A token is a string (nothing special) or key metadata (capture group).
124 */
125export type Token = string | Key;
126/**
127 * Repeated and simple input types.
128 */
129export type Path = string | TokenData;
130/**
131 * Normalize the given path string, returning a regular expression.
132 *
133 * An empty array can be passed in for the keys, which will hold the
134 * placeholder key descriptions. For example, using `/user/:id`, `keys` will
135 * contain `[{ name: 'id', delimiter: '/', optional: false, repeat: false }]`.
136 */
137export declare function pathToRegexp(path: Path, options?: PathToRegexpOptions): RegExp & {
138 keys: Key[];
139};