UNPKG

4.78 kBTypeScriptView Raw
1/// <reference types="react" />
2import type { Location, Path, To } from "history";
3export declare function invariant(cond: any, message: string): asserts cond;
4export declare function warning(cond: any, message: string): void;
5export declare function warningOnce(key: string, cond: boolean, message: string): void;
6declare type ParamParseFailed = {
7 failed: true;
8};
9declare type ParamParseSegment<Segment extends string> = Segment extends `${infer LeftSegment}/${infer RightSegment}` ? ParamParseSegment<LeftSegment> extends infer LeftResult ? ParamParseSegment<RightSegment> extends infer RightResult ? LeftResult extends string ? RightResult extends string ? LeftResult | RightResult : LeftResult : RightResult extends string ? RightResult : ParamParseFailed : ParamParseFailed : ParamParseSegment<RightSegment> extends infer RightResult ? RightResult extends string ? RightResult : ParamParseFailed : ParamParseFailed : Segment extends `:${infer Remaining}` ? Remaining : ParamParseFailed;
10export declare type ParamParseKey<Segment extends string> = ParamParseSegment<Segment> extends string ? ParamParseSegment<Segment> : string;
11/**
12 * The parameters that were parsed from the URL path.
13 */
14export declare type Params<Key extends string = string> = {
15 readonly [key in Key]: string | undefined;
16};
17/**
18 * A route object represents a logical route, with (optionally) its child
19 * routes organized in a tree-like structure.
20 */
21export interface RouteObject {
22 caseSensitive?: boolean;
23 children?: RouteObject[];
24 element?: React.ReactNode;
25 index?: boolean;
26 path?: string;
27}
28/**
29 * Returns a path with params interpolated.
30 *
31 * @see https://reactrouter.com/docs/en/v6/api#generatepath
32 */
33export declare function generatePath(path: string, params?: Params): string;
34/**
35 * A RouteMatch contains info about how a route matched a URL.
36 */
37export interface RouteMatch<ParamKey extends string = string> {
38 /**
39 * The names and values of dynamic parameters in the URL.
40 */
41 params: Params<ParamKey>;
42 /**
43 * The portion of the URL pathname that was matched.
44 */
45 pathname: string;
46 /**
47 * The portion of the URL pathname that was matched before child routes.
48 */
49 pathnameBase: string;
50 /**
51 * The route object that was used to match.
52 */
53 route: RouteObject;
54}
55/**
56 * Matches the given routes to a location and returns the match data.
57 *
58 * @see https://reactrouter.com/docs/en/v6/api#matchroutes
59 */
60export declare function matchRoutes(routes: RouteObject[], locationArg: Partial<Location> | string, basename?: string): RouteMatch[] | null;
61/**
62 * A PathPattern is used to match on some portion of a URL pathname.
63 */
64export interface PathPattern<Path extends string = string> {
65 /**
66 * A string to match against a URL pathname. May contain `:id`-style segments
67 * to indicate placeholders for dynamic parameters. May also end with `/*` to
68 * indicate matching the rest of the URL pathname.
69 */
70 path: Path;
71 /**
72 * Should be `true` if the static portions of the `path` should be matched in
73 * the same case.
74 */
75 caseSensitive?: boolean;
76 /**
77 * Should be `true` if this pattern should match the entire URL pathname.
78 */
79 end?: boolean;
80}
81/**
82 * A PathMatch contains info about how a PathPattern matched on a URL pathname.
83 */
84export interface PathMatch<ParamKey extends string = string> {
85 /**
86 * The names and values of dynamic parameters in the URL.
87 */
88 params: Params<ParamKey>;
89 /**
90 * The portion of the URL pathname that was matched.
91 */
92 pathname: string;
93 /**
94 * The portion of the URL pathname that was matched before child routes.
95 */
96 pathnameBase: string;
97 /**
98 * The pattern that was used to match.
99 */
100 pattern: PathPattern;
101}
102/**
103 * Performs pattern matching on a URL pathname and returns information about
104 * the match.
105 *
106 * @see https://reactrouter.com/docs/en/v6/api#matchpath
107 */
108export declare function matchPath<ParamKey extends ParamParseKey<Path>, Path extends string>(pattern: PathPattern<Path> | Path, pathname: string): PathMatch<ParamKey> | null;
109/**
110 * Returns a resolved path object relative to the given pathname.
111 *
112 * @see https://reactrouter.com/docs/en/v6/api#resolvepath
113 */
114export declare function resolvePath(to: To, fromPathname?: string): Path;
115export declare function resolveTo(toArg: To, routePathnames: string[], locationPathname: string): Path;
116export declare function getToPathname(to: To): string | undefined;
117export declare function stripBasename(pathname: string, basename: string): string | null;
118export declare const joinPaths: (paths: string[]) => string;
119export declare const normalizePathname: (pathname: string) => string;
120export {};