UNPKG

burger-api

Version:

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

66 lines (65 loc) 3.24 kB
import type { RouteDefinition, TrieNode } from '../types/index'; /** * ApiRouter class for handling file-based routing. * Loads routes from a directory structure and matches requests to the appropriate route handlers. * Supports dynamic segments (e.g., [id] ,(group),[...]) and HTTP method handlers. * Routes are sorted to prioritize static routes over dynamic and wildcard ones to prevent overlapping route issues. */ export declare class ApiRouter { private routesDir; private prefix; /** The root of the trie where all routes will begin*/ private root; /** * Constructor for the ApiRouter class. * @param routesDir The directory path where route modules are located. * @param prefix Optional prefix to prepend to all routes (e.g., "api" becomes "/api/..."). * @throws {Error} If the routes directory path is not provided. */ constructor(routesDir: string, prefix?: string); /** * Getter for the routes trie. * @returns The root of the trie. */ get routes(): TrieNode; /** * Loads all routes from the routes directory into the trie structure. * @returns {Promise<void>} A promise that resolves when all routes are loaded. * @throws {Error} If the routes directory is not found or if an error occurs during route loading. */ loadRoutes(): Promise<void>; /** * Inserts a route definition into the trie structure. * @param routeDef The route definition to insert, containing path, handlers, middleware, schema, and OpenAPI metadata. * @throws {Error} If the route definition is invalid or causes a conflict in the trie. */ private insertRoute; /** * Recursively scans a directory for route modules (route.ts files) and loads them into the trie. * @param dir The current directory path to scan. * @param basePath The base path used to construct route paths from the directory structure. * @returns {Promise<void>} A promise that resolves when the directory scan is complete. * @throws {Error} If multiple dynamic folders are found at the same level or if directory access fails. */ private scanDirectory; /** * Loads a route module from a file and inserts it into the trie. * @param entryPath The full file system path to the route module (route.ts). * @param relativePath The relative path used to construct the route path. * @returns {Promise<void>} A promise that resolves when the module is loaded and inserted. * @throws {Error} If the route module fails to load or is invalid. */ private loadRouteModule; /** * Resolves a request to a route definition and its parameters. * @param pathname The normalized request pathname. * @param method The uppercase HTTP method. * @returns {{ route?: RouteDefinition; params: Record<string, string>; wildcardParams?: string[] }} An object containing the matched route (if any), extracted parameters, and wildcard segments (if applicable). * @throws {Error} If the request URL is malformed. */ resolve(pathname: string, method: string): { route?: RouteDefinition; params: Record<string, string>; wildcardParams?: string[]; }; }