UNPKG

2 kBTypeScriptView Raw
1import * as React from "react";
2import * as url from "url";
3
4type UrlLike = url.UrlObject | url.Url;
5
6export interface EventChangeOptions {
7 shallow?: boolean;
8 [key: string]: any;
9}
10
11export type PopStateCallback = (state: any) => boolean | undefined;
12
13export type RouterCallback = () => void;
14export interface RouterProps {
15 // url property fields
16 readonly pathname: string;
17 readonly route: string;
18 readonly asPath?: string;
19 readonly query?: {
20 [key: string]:
21 | boolean
22 | boolean[]
23 | number
24 | number[]
25 | string
26 | string[];
27 };
28
29 // property fields
30 readonly components: {
31 [key: string]: { Component: React.ComponentType<any>; err: any };
32 };
33
34 // core method fields
35 back(): void;
36 beforePopState(cb: PopStateCallback): boolean;
37 prefetch(url: string): Promise<React.ComponentType<any>>;
38 push(
39 url: string | UrlLike,
40 as?: string | UrlLike,
41 options?: EventChangeOptions,
42 ): Promise<boolean>;
43 reload(route: string): Promise<void>;
44 replace(
45 url: string | UrlLike,
46 as?: string | UrlLike,
47 options?: EventChangeOptions,
48 ): Promise<boolean>;
49
50 // events
51 onAppUpdated?(nextRoute: string): void;
52 onBeforeHistoryChange?(as: string): void;
53 onHashChangeStart?(url: string): void;
54 onHashChangeComplete?(url: string): void;
55 onRouteChangeComplete?(url: string): void;
56 onRouteChangeError?(error: any, url: string): void;
57 onRouteChangeStart?(url: string): void;
58}
59
60export interface SingletonRouter extends RouterProps {
61 router: RouterProps | null;
62 readyCallbacks: RouterCallback[];
63 ready(cb: RouterCallback): void;
64}
65
66export interface WithRouterProps {
67 router: SingletonRouter;
68}
69
70export function withRouter<T extends {}>(
71 Component: React.ComponentType<T & WithRouterProps>,
72): React.ComponentType<T>;
73
74declare const Router: SingletonRouter;
75export default Router;