UNPKG

2.36 kBTypeScriptView Raw
1import type { StaticPath, StaticPropsContext } from './utils/router';
2import type { createPrefetch } from './utils/prefetch';
3import { ComponentProps, JSX, ComponentType } from "preact";
4declare type RestParam<S extends string> = S extends `...${infer A}` ? A : never;
5declare type StandardParam<S extends string> = S extends `...${infer A}` ? never : S;
6declare type ExtractParams<S extends string> = S extends `[${infer A}]` ? A : never;
7declare type TupleToUnion<T extends any[]> = T[number];
8declare type Split<S extends string> = string extends S ? string[] : S extends '' ? [] : S extends `${infer T}/${infer U}` ? [T, ...Split<U>] : [
9 S
10];
11declare type NormalizePath<S extends string> = S extends `/${infer T}` ? T : S;
12declare type AllPathParams<S extends string, P extends string = ExtractParams<TupleToUnion<Split<NormalizePath<S>>>>> = {
13 [param in P]: string | string[];
14};
15declare type RestParams<S extends string, Base extends string = keyof AllPathParams<S>> = {
16 [a in RestParam<Base>]: string[];
17};
18declare type StandardParams<S extends string, Base extends string = keyof AllPathParams<S>> = {
19 [a in StandardParam<Base>]: string;
20};
21export declare type PathParams<S extends string> = RestParams<S> & StandardParams<S>;
22export interface GetStaticPaths<Path extends string, P extends PathParams<Path>> {
23 (ctx: {
24 prefetch?: ReturnType<typeof createPrefetch>;
25 }): Promise<{
26 paths: StaticPath<P>[];
27 } | string | null>;
28}
29export interface GetStaticProps<T extends ComponentType<any> | keyof JSX.IntrinsicElements, Path extends string, P extends PathParams<Path>> {
30 (ctx: StaticPropsContext<P> & {
31 prefetch?: ReturnType<typeof createPrefetch>;
32 }): Promise<{
33 props: ComponentProps<T>;
34 } | string | null>;
35}
36export interface Page<T extends ComponentType<any> | keyof JSX.IntrinsicElements, Path extends string, P extends PathParams<Path>> {
37 path?: Path;
38 getStaticPaths?: GetStaticPaths<Path, P>;
39 getStaticProps?: GetStaticProps<T, Path, P>;
40}
41export declare function definePage<T extends ComponentType<any> | keyof JSX.IntrinsicElements, Path extends string, P extends PathParams<Path>>(Component: T, page?: Page<T, Path, P>): {
42 path?: Path;
43 getStaticPaths?: GetStaticPaths<Path, P>;
44 getStaticProps?: GetStaticProps<T, Path, P>;
45 Component: T;
46};
47export {};