index.d.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. export interface ParseOptions {
  2. /**
  3. * Set the default delimiter for repeat parameters. (default: `'/'`)
  4. */
  5. delimiter?: string;
  6. /**
  7. * List of characters to automatically consider prefixes when parsing.
  8. */
  9. prefixes?: string;
  10. }
  11. /**
  12. * Parse a string for the raw tokens.
  13. */
  14. export declare function parse(str: string, options?: ParseOptions): Token[];
  15. export interface TokensToFunctionOptions {
  16. /**
  17. * When `true` the regexp will be case sensitive. (default: `false`)
  18. */
  19. sensitive?: boolean;
  20. /**
  21. * Function for encoding input strings for output.
  22. */
  23. encode?: (value: string, token: Key) => string;
  24. /**
  25. * When `false` the function can produce an invalid (unmatched) path. (default: `true`)
  26. */
  27. validate?: boolean;
  28. }
  29. /**
  30. * Compile a string to a template function for the path.
  31. */
  32. export declare function compile<P extends object = object>(str: string, options?: ParseOptions & TokensToFunctionOptions): PathFunction<P>;
  33. export declare type PathFunction<P extends object = object> = (data?: P) => string;
  34. /**
  35. * Expose a method for transforming tokens into the path function.
  36. */
  37. export declare function tokensToFunction<P extends object = object>(tokens: Token[], options?: TokensToFunctionOptions): PathFunction<P>;
  38. export interface RegexpToFunctionOptions {
  39. /**
  40. * Function for decoding strings for params.
  41. */
  42. decode?: (value: string, token: Key) => string;
  43. }
  44. /**
  45. * A match result contains data about the path match.
  46. */
  47. export interface MatchResult<P extends object = object> {
  48. path: string;
  49. index: number;
  50. params: P;
  51. }
  52. /**
  53. * A match is either `false` (no match) or a match result.
  54. */
  55. export declare type Match<P extends object = object> = false | MatchResult<P>;
  56. /**
  57. * The match function takes a string and returns whether it matched the path.
  58. */
  59. export declare type MatchFunction<P extends object = object> = (path: string) => Match<P>;
  60. /**
  61. * Create path match function from `path-to-regexp` spec.
  62. */
  63. export declare function match<P extends object = object>(str: Path, options?: ParseOptions & TokensToRegexpOptions & RegexpToFunctionOptions): MatchFunction<P>;
  64. /**
  65. * Create a path match function from `path-to-regexp` output.
  66. */
  67. export declare function regexpToFunction<P extends object = object>(re: RegExp, keys: Key[], options?: RegexpToFunctionOptions): MatchFunction<P>;
  68. /**
  69. * Metadata about a key.
  70. */
  71. export interface Key {
  72. name: string | number;
  73. prefix: string;
  74. suffix: string;
  75. pattern: string;
  76. modifier: string;
  77. }
  78. /**
  79. * A token is a string (nothing special) or key metadata (capture group).
  80. */
  81. export declare type Token = string | Key;
  82. export interface TokensToRegexpOptions {
  83. /**
  84. * When `true` the regexp will be case sensitive. (default: `false`)
  85. */
  86. sensitive?: boolean;
  87. /**
  88. * When `true` the regexp won't allow an optional trailing delimiter to match. (default: `false`)
  89. */
  90. strict?: boolean;
  91. /**
  92. * When `true` the regexp will match to the end of the string. (default: `true`)
  93. */
  94. end?: boolean;
  95. /**
  96. * When `true` the regexp will match from the beginning of the string. (default: `true`)
  97. */
  98. start?: boolean;
  99. /**
  100. * Sets the final character for non-ending optimistic matches. (default: `/`)
  101. */
  102. delimiter?: string;
  103. /**
  104. * List of characters that can also be "end" characters.
  105. */
  106. endsWith?: string;
  107. /**
  108. * Encode path tokens for use in the `RegExp`.
  109. */
  110. encode?: (value: string) => string;
  111. }
  112. /**
  113. * Expose a function for taking tokens and returning a RegExp.
  114. */
  115. export declare function tokensToRegexp(tokens: Token[], keys?: Key[], options?: TokensToRegexpOptions): RegExp;
  116. /**
  117. * Supported `path-to-regexp` input types.
  118. */
  119. export declare type Path = string | RegExp | Array<string | RegExp>;
  120. /**
  121. * Normalize the given path string, returning a regular expression.
  122. *
  123. * An empty array can be passed in for the keys, which will hold the
  124. * placeholder key descriptions. For example, using `/user/:id`, `keys` will
  125. * contain `[{ name: 'id', delimiter: '/', optional: false, repeat: false }]`.
  126. */
  127. export declare function pathToRegexp(path: Path, keys?: Key[], options?: TokensToRegexpOptions & ParseOptions): RegExp;