burger-api
Version:
<p align="center"> <img src="https://github.com/user-attachments/assets/0d9b376e-1d89-479a-aa7f-e7ee3c6b2342" alt="BurgerAPI logo"/> </p>
71 lines (70 loc) • 3.42 kB
TypeScript
import type { RouteDefinition, TrieNode } from '@burgerTypes';
/**
* 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]) and HTTP method handlers.
* Routes are sorted to prioritize static routes over dynamic 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;
/**
* Converts a file path to a route path by processing dynamic segments and applying the prefix.
* @param filePath The file path to convert (e.g., "users/[id]/route.ts").
* @returns {string} The converted route path (e.g., "/users/:id").
*/
private convertFilePathToRoute;
/**
* 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> }} An object containing the matched route (if any) and extracted parameters.
* @throws {Error} If the request URL is malformed.
*/
resolve(pathname: string, method: string): {
route?: RouteDefinition;
params: Record<string, string>;
};
}