bun-routes
Version:
Bun Routes is a lightweight, type-safe router built for Bun with zero dependencies
18 lines (17 loc) • 736 B
JavaScript
import { composeMiddlewares } from "./middleware";
/**
* Creates a route tuple with the provided options and final handler.
* If the route is not exposed, returns a default 404 handler.
*
* @param options - Route configuration options.
* @param handler - The final route handler.
* @returns A tuple containing the route path and a RouteHandlerObject.
*/
export function route(options, handler) {
const { method, path, expose = true, middlewares = [] } = options;
if (!expose) {
return [path, { [method]: () => new Response(null, { status: 404 }) }];
}
const composedHandler = middlewares.length > 0 ? composeMiddlewares(middlewares, handler) : handler;
return [path, { [method]: composedHandler }];
}