UNPKG

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