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>

96 lines (95 loc) 3.24 kB
import type { ServerOptions } from './types/index'; export declare class Burger { private options; /** * The server instance */ private server; /** * The API router instance */ private apiRouter?; /** * The page router instance */ private pageRouter?; /** * The global middleware */ private globalMiddleware; /** * The OpenAPI document */ private openApiDoc; /** * The routes object */ private routes; /** * Pre-computed responses for reuse */ private readonly METHOD_NOT_ALLOWED; /** * The not found response */ private readonly NOT_FOUND; /** * The OpenAPI error response */ private readonly OPENAPI_ERROR; /** * Constructor for the Burger class. * @param options - The options for the server and router. * The options object should contain the following properties: * - port: The port number to listen on. * - apiDir: The directory path to load API routes from. * - pageDir: The directory path to load page routes from. * - middleware: An array of global middleware functions. */ constructor(options: ServerOptions); /** * Process the page routes and add them to the routes object * @returns A promise that resolves to a boolean */ private processPageRoutes; /** * Process the API routes and add them to the routes object * @returns A promise that resolves to a boolean */ private processApiRoutes; /** * Process single middleware * @param request - The request object * @param middleware - The middleware function * @param handler - The handler function * @returns A promise that resolves to a response */ private processSingleMiddleware; /** * Process middleware and handler * * How it works: * 1. Run each middleware in order * 2. If middleware returns Response → stop and send that response (but still apply "after" functions) * 3. If middleware returns undefined → continue to next middleware * 4. If middleware returns function → save it to transform the final response later * 5. After all middlewares, run the handler * 6. Apply all saved "after" functions to the response (in reverse order) * * Performance optimizations: * - Pre-allocated array for "after" functions (avoids dynamic resizing) * - Fast paths for 0, 1, and 2 middlewares (most common cases) * - Manual loop unrolling for small counts * - Minimal branching in hot path */ private processMiddleware; /** * Starts the server and begins listening for incoming requests. * @param port - The port number to listen on. Defaults to `4000`. * @param cb - An optional cb function to be executed when the server is listening. * @returns A Promise that resolves when the server has started listening. */ serve(port?: number, cb?: () => void): Promise<void>; } export { setDir } from './utils/index'; export type { ServerOptions, RequestHandler, BurgerRequest, BurgerNext, Middleware, openapi, RouteDefinition, PageDefinition, } from './types/index';