UNPKG

2.25 kBTypeScriptView Raw
1/// <reference types="node" />
2import { IncomingMessage, ServerResponse } from 'http';
3import { UrlWithParsedQuery } from 'url';
4import { RouteHas } from '../../lib/load-custom-routes';
5export declare const route: (path: string) => (pathname: string | null | undefined, params?: any) => any;
6export declare type Params = {
7 [param: string]: any;
8};
9export declare type RouteMatch = (pathname: string | null | undefined) => false | Params;
10declare type RouteResult = {
11 finished: boolean;
12 pathname?: string;
13 query?: {
14 [k: string]: string;
15 };
16};
17export declare type Route = {
18 match: RouteMatch;
19 has?: RouteHas[];
20 type: string;
21 check?: boolean;
22 statusCode?: number;
23 name: string;
24 requireBasePath?: false;
25 internal?: true;
26 fn: (req: IncomingMessage, res: ServerResponse, params: Params, parsedUrl: UrlWithParsedQuery) => Promise<RouteResult> | RouteResult;
27};
28export declare type DynamicRoutes = Array<{
29 page: string;
30 match: RouteMatch;
31}>;
32export declare type PageChecker = (pathname: string) => Promise<boolean>;
33export default class Router {
34 basePath: string;
35 headers: Route[];
36 fsRoutes: Route[];
37 redirects: Route[];
38 rewrites: {
39 beforeFiles: Route[];
40 afterFiles: Route[];
41 fallback: Route[];
42 };
43 catchAllRoute: Route;
44 pageChecker: PageChecker;
45 dynamicRoutes: DynamicRoutes;
46 useFileSystemPublicRoutes: boolean;
47 locales: string[];
48 constructor({ basePath, headers, fsRoutes, rewrites, redirects, catchAllRoute, dynamicRoutes, pageChecker, useFileSystemPublicRoutes, locales, }: {
49 basePath: string;
50 headers: Route[];
51 fsRoutes: Route[];
52 rewrites: {
53 beforeFiles: Route[];
54 afterFiles: Route[];
55 fallback: Route[];
56 };
57 redirects: Route[];
58 catchAllRoute: Route;
59 dynamicRoutes: DynamicRoutes | undefined;
60 pageChecker: PageChecker;
61 useFileSystemPublicRoutes: boolean;
62 locales: string[];
63 });
64 setDynamicRoutes(routes?: DynamicRoutes): void;
65 addFsRoute(fsRoute: Route): void;
66 execute(req: IncomingMessage, res: ServerResponse, parsedUrl: UrlWithParsedQuery): Promise<boolean>;
67}
68export {};