UNPKG

burger-api

Version:

<p align="center"> <img src="https://github.com/user-attachments/assets/0d9b376e-1d89-479a-aa7f-e7ee3c6b2342" alt="BurgerAPI logo"/> </p>

60 lines (59 loc) 2.58 kB
import type { PageDefinition } from '@burgerTypes'; /** * PageRouter class for handling file-based page routing. * Loads pages from a directory structure and matches requests to the appropriate page handlers. * Supports dynamic segments (e.g., [id]) and uses default exports as page handlers. */ export declare class PageRouter { private pagesDir; private prefix; /** Array of loaded page definitions */ pages: PageDefinition[]; /** * Constructor for the PageRouter class. * @param pagesDir The directory path where page modules are located. * @param prefix Optional prefix to prepend to all routes (e.g., "pages" becomes "/pages/..."). */ constructor(pagesDir: string, prefix?: string); /** * Loads page modules from the specified directory and adds them to the pages array. * After loading, sorts the pages to prioritize static routes over dynamic ones based on specificity. * @returns A promise that resolves when all page modules have been loaded and sorted. */ loadPages(): Promise<void>; /** * Recursively scans the directory for page modules and adds them to the pages array. * @param dir The current directory to scan. * @param basePath The base path for constructing the route path. */ private scanDirectory; /** * Converts a file path to a route path by processing dynamic segments, * removing certain directory indicators, and applying a prefix if specified. * @param filePath The file path to convert into a route path. * @returns The converted route path string (before extension cleaning). */ private convertFilePathToRoute; /** * Cleans a route path by handling index files and removing extensions. * @param routePath The route path to clean * @returns The cleaned route path */ private cleanRoutePath; /** * Resolves the given request by finding a matching page and extracting dynamic parameters. * @param request The request to resolve. * @returns An object containing the matched page and parameters, or an empty params object if no match. */ resolve(request: Request): { page?: PageDefinition; params: Record<string, string>; }; /** * Checks if the request path matches the page path, extracting dynamic parameters if matched. * @param requestPath The request path to check. * @param pagePath The page path to match against. * @returns A record of dynamic parameters if matched, otherwise null. */ private matchRoute; }