forked from preactjs/preact-iso
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.d.ts
More file actions
95 lines (73 loc) · 2.85 KB
/
router.d.ts
File metadata and controls
95 lines (73 loc) · 2.85 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { AnyComponent, ComponentChildren, Context, VNode } from 'preact';
export const LocationProvider: {
(props: { scope?: string | RegExp; children?: ComponentChildren; }): VNode;
ctx: Context<LocationHook>;
};
type NestedArray<T> = Array<T | NestedArray<T>>;
interface KnownProps {
path: string;
query: Record<string, string>;
params: Record<string, string>;
default?: boolean;
rest?: string;
component?: AnyComponent;
}
interface ArbitraryProps {
[prop: string]: any;
}
type MatchProps = KnownProps & ArbitraryProps;
/**
* Check if a URL path matches against a URL path pattern.
*
* Warning: This is largely an internal API, it may change in the future
* @param url - URL path (e.g. /user/12345)
* @param route - URL pattern (e.g. /user/:id)
*/
export function exec(url: string, route: string, matches?: MatchProps): MatchProps
export function Router(props: {
onRouteChange?: (url: string) => void;
onLoadEnd?: (url: string) => void;
onLoadStart?: (url: string) => void;
children?: NestedArray<VNode>;
}): VNode;
interface LocationHook {
url: string;
path: string;
query: Record<string, string>;
route: (url: string, replace?: boolean) => void;
}
export const useLocation: () => LocationHook;
interface RouteHook {
path: string;
query: Record<string, string>;
params: Record<string, string>;
}
export const useRoute: () => RouteHook;
type RoutableProps =
| { path: string; default?: false; }
| { path?: never; default: true; }
export type RouteProps<Props> = RoutableProps & { component: AnyComponent<Props> };
export type RoutePropsForPath<Path extends string> = Path extends '*'
? { params: {}; rest: string }
: Path extends `:${infer placeholder}?/${infer rest}`
? { [k in placeholder]?: string } & { params: RoutePropsForPath<rest>['params'] & { [k in placeholder]?: string } } & Omit<RoutePropsForPath<rest>, 'params'>
: Path extends `:${infer placeholder}/${infer rest}`
? { [k in placeholder]: string } & { params: RoutePropsForPath<rest>['params'] & { [k in placeholder]: string } } & Omit<RoutePropsForPath<rest>, 'params'>
: Path extends `:${infer placeholder}?`
? { [k in placeholder]?: string } & { params: { [k in placeholder]?: string } }
: Path extends `:${infer placeholder}*`
? { [k in placeholder]?: string } & { params: { [k in placeholder]?: string } }
: Path extends `:${infer placeholder}+`
? { [k in placeholder]: string } & { params: { [k in placeholder]: string } }
: Path extends `:${infer placeholder}`
? { [k in placeholder]: string } & { params: { [k in placeholder]: string } }
: Path extends (`/${infer rest}` | `${infer _}/${infer rest}`)
? RoutePropsForPath<rest>
: { params: {} };
export function Route<Props>(props: RouteProps<Props> & Partial<Props>): VNode;
declare module 'preact' {
namespace JSX {
interface IntrinsicAttributes extends RoutableProps {}
}
interface Attributes extends RoutableProps {}
}