|
| 1 | +import { AnyComponent, ComponentChildren, Context, VNode } from 'preact'; |
| 2 | + |
| 3 | +export const LocationProvider: { |
| 4 | + (props: { scope?: string | RegExp; children?: ComponentChildren; }): VNode; |
| 5 | + ctx: Context<LocationHook>; |
| 6 | +}; |
| 7 | + |
| 8 | +type NestedArray<T> = Array<T | NestedArray<T>>; |
| 9 | + |
| 10 | +interface KnownProps { |
| 11 | + path: string; |
| 12 | + query: Record<string, string>; |
| 13 | + params: Record<string, string>; |
| 14 | + default?: boolean; |
| 15 | + rest?: string; |
| 16 | + component?: AnyComponent; |
| 17 | +} |
| 18 | + |
| 19 | +interface ArbitraryProps { |
| 20 | + [prop: string]: any; |
| 21 | +} |
| 22 | + |
| 23 | +type MatchProps = KnownProps & ArbitraryProps; |
| 24 | + |
| 25 | +/** |
| 26 | + * Check if a URL path matches against a URL path pattern. |
| 27 | + * |
| 28 | + * Warning: This is largely an internal API, it may change in the future |
| 29 | + * @param url - URL path (e.g. /user/12345) |
| 30 | + * @param route - URL pattern (e.g. /user/:id) |
| 31 | + */ |
| 32 | +export function exec(url: string, route: string, matches?: MatchProps): MatchProps |
| 33 | + |
| 34 | +export function Router(props: { |
| 35 | + onRouteChange?: (url: string) => void; |
| 36 | + onLoadEnd?: (url: string) => void; |
| 37 | + onLoadStart?: (url: string) => void; |
| 38 | + children?: NestedArray<VNode>; |
| 39 | +}): VNode; |
| 40 | + |
| 41 | +interface LocationHook { |
| 42 | + url: string; |
| 43 | + path: string; |
| 44 | + query: Record<string, string>; |
| 45 | +} |
| 46 | +export const useLocation: () => LocationHook; |
| 47 | + |
| 48 | +interface RouteHook { |
| 49 | + path: string; |
| 50 | + query: Record<string, string>; |
| 51 | + params: Record<string, string>; |
| 52 | +} |
| 53 | +export const useRoute: () => RouteHook; |
| 54 | + |
| 55 | +type RoutableProps = |
| 56 | + | { path: string; default?: false; } |
| 57 | + | { path?: never; default: true; } |
| 58 | + |
| 59 | +export type RouteProps<Props> = RoutableProps & { component: AnyComponent<Props> }; |
| 60 | + |
| 61 | +export type RoutePropsForPath<Path extends string> = Path extends '*' |
| 62 | + ? { params: {}; rest: string } |
| 63 | + |
| 64 | + : Path extends `:${infer placeholder}?/${infer rest}` |
| 65 | + ? { [k in placeholder]?: string } & { params: RoutePropsForPath<rest>['params'] & { [k in placeholder]?: string } } & Omit<RoutePropsForPath<rest>, 'params'> |
| 66 | + |
| 67 | + : Path extends `:${infer placeholder}/${infer rest}` |
| 68 | + ? { [k in placeholder]: string } & { params: RoutePropsForPath<rest>['params'] & { [k in placeholder]: string } } & Omit<RoutePropsForPath<rest>, 'params'> |
| 69 | + |
| 70 | + : Path extends `:${infer placeholder}?` |
| 71 | + ? { [k in placeholder]?: string } & { params: { [k in placeholder]?: string } } |
| 72 | + |
| 73 | + : Path extends `:${infer placeholder}*` |
| 74 | + ? { [k in placeholder]?: string } & { params: { [k in placeholder]?: string } } |
| 75 | + |
| 76 | + : Path extends `:${infer placeholder}+` |
| 77 | + ? { [k in placeholder]: string } & { params: { [k in placeholder]: string } } |
| 78 | + |
| 79 | + : Path extends `:${infer placeholder}` |
| 80 | + ? { [k in placeholder]: string } & { params: { [k in placeholder]: string } } |
| 81 | + |
| 82 | + : Path extends (`/${infer rest}` | `${infer _}/${infer rest}`) |
| 83 | + ? RoutePropsForPath<rest> |
| 84 | + |
| 85 | + : { params: {} }; |
| 86 | + |
| 87 | +export function Route<Props>(props: RouteProps<Props> & Partial<Props>): VNode; |
| 88 | + |
| 89 | +declare module 'preact' { |
| 90 | + // The code below automatically adds `path` and `default` as optional props for every component |
| 91 | + // (effectively reserving those names, so no component should use those names in its own props). |
| 92 | + // These declarations extend from `RouteableProps`, which is not allowed in modern TypeScript and |
| 93 | + // causes a TS2312 error. However, the compiler does seems to honor the intent of this code, so |
| 94 | + // to avoid an API regression, let's ignore the error rather than loosening the type validation. |
| 95 | + namespace JSX { |
| 96 | + /** @ts-ignore */ |
| 97 | + interface IntrinsicAttributes extends RoutableProps {} |
| 98 | + } |
| 99 | + /** @ts-ignore */ |
| 100 | + interface Attributes extends RoutableProps {} |
| 101 | +} |
0 commit comments