router.d.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /// <reference types="node" />
  2. /// <reference types="node" />
  3. /// <reference types="node" />
  4. import type { NextConfig } from './config';
  5. import type { ParsedUrlQuery } from 'querystring';
  6. import type { BaseNextRequest, BaseNextResponse } from './base-http';
  7. import type { RouteMatch, Params } from '../shared/lib/router/utils/route-matcher';
  8. import type { RouteHas } from '../lib/load-custom-routes';
  9. import { NextUrlWithParsedQuery } from './request-meta';
  10. declare type RouteResult = {
  11. finished: boolean;
  12. pathname?: string;
  13. query?: ParsedUrlQuery;
  14. };
  15. export declare type Route = {
  16. match: RouteMatch;
  17. has?: RouteHas[];
  18. missing?: RouteHas[];
  19. type: string;
  20. check?: boolean;
  21. statusCode?: number;
  22. name: string;
  23. matchesBasePath?: true;
  24. matchesLocale?: true;
  25. matchesLocaleAPIRoutes?: true;
  26. matchesTrailingSlash?: true;
  27. internal?: true;
  28. fn: (req: BaseNextRequest, res: BaseNextResponse, params: Params, parsedUrl: NextUrlWithParsedQuery, upgradeHead?: Buffer) => Promise<RouteResult> | RouteResult;
  29. };
  30. export declare type DynamicRoutes = Array<{
  31. page: string;
  32. match: RouteMatch;
  33. }>;
  34. export declare type PageChecker = (pathname: string) => Promise<boolean>;
  35. export default class Router {
  36. catchAllMiddleware: ReadonlyArray<Route>;
  37. private readonly headers;
  38. private readonly fsRoutes;
  39. private readonly redirects;
  40. private readonly rewrites;
  41. private readonly catchAllRoute;
  42. private readonly pageChecker;
  43. private dynamicRoutes;
  44. private readonly useFileSystemPublicRoutes;
  45. private readonly nextConfig;
  46. private compiledRoutes;
  47. private needsRecompilation;
  48. /**
  49. * context stores information used by the router.
  50. */
  51. private readonly context;
  52. constructor({ headers, fsRoutes, rewrites, redirects, catchAllRoute, catchAllMiddleware, dynamicRoutes, pageChecker, useFileSystemPublicRoutes, nextConfig, }: {
  53. headers: ReadonlyArray<Route>;
  54. fsRoutes: ReadonlyArray<Route>;
  55. rewrites: {
  56. beforeFiles: ReadonlyArray<Route>;
  57. afterFiles: ReadonlyArray<Route>;
  58. fallback: ReadonlyArray<Route>;
  59. };
  60. redirects: ReadonlyArray<Route>;
  61. catchAllRoute: Route;
  62. catchAllMiddleware: ReadonlyArray<Route>;
  63. dynamicRoutes: DynamicRoutes | undefined;
  64. pageChecker: PageChecker;
  65. useFileSystemPublicRoutes: boolean;
  66. nextConfig: NextConfig;
  67. });
  68. private checkPage;
  69. get locales(): string[];
  70. get basePath(): string;
  71. setDynamicRoutes(dynamicRoutes: DynamicRoutes): void;
  72. setCatchallMiddleware(catchAllMiddleware: ReadonlyArray<Route>): void;
  73. addFsRoute(fsRoute: Route): void;
  74. private compileRoutes;
  75. private checkFsRoutes;
  76. execute(req: BaseNextRequest, res: BaseNextResponse, parsedUrl: NextUrlWithParsedQuery, upgradeHead?: Buffer): Promise<boolean>;
  77. }
  78. export {};