UNPKG

bunway

Version:

Express-style routing toolkit built natively for Bun.

51 lines 2.07 kB
import { type BodyParserOptions, type ResolvedBodyParserOptions } from "../config"; import { WayContext, type NextFunction } from "./context"; /** * Signature for bunWay middleware/handlers. * Matches express' `(ctx, next)` form but works with Fetch primitives. */ export type Handler = (ctx: WayContext, next: NextFunction) => Promise<void | Response> | void | Response; /** Optional configuration supplied when instantiating a Router. */ export interface RouterOptions { bodyParser?: BodyParserOptions; } /** * BunWay router implementation. * * Usage mirrors Express: * ```ts * const router = new Router(); * router.use(cors()); * router.get("/health", (ctx) => ctx.res.text("OK")); * router.post("/users", async (ctx) => ctx.res.created(await ctx.req.parseBody())); * ``` * * Internally the router still works with {@link Request}/{@link Response}, which * means handlers can choose to return a native Response or use `ctx.res` helpers. * Middleware-supplied extras (CORS headers, etc.) are merged in the finalizer so * behaviour stays consistent regardless of handler style. */ export declare class Router { private routes; private children; private middlewares; private bodyParserConfig; private autoBodyParser; constructor(options?: RouterOptions); get(path: string, ...handlers: Handler[]): void; post(path: string, ...handlers: Handler[]): void; put(path: string, ...handlers: Handler[]): void; delete(path: string, ...handlers: Handler[]): void; patch(path: string, ...handlers: Handler[]): void; options(path: string, ...handlers: Handler[]): void; use(handler: Handler): void; use(prefix: string, router: Router): void; setBodyParser(options: BodyParserOptions): void; configureBodyParser(options: BodyParserOptions): void; /** Snapshot of the current resolved parser configuration. */ getBodyParserConfig(): ResolvedBodyParserOptions; handle(req: Request): Promise<Response>; private add; private pathToRegex; } //# sourceMappingURL=router.d.ts.map