Skip to content

defineRoute

Creates a typed route definition for a single path and HTTP method. Use it to describe routes before passing a collection to compileRoutes or createServer.

The returned object is frozen, so route definitions are treated as static configuration after creation.

Importing

ts
import { defineRoute } from '@almighty-shogun/bun-server';

Usage

ts
import { defineRoute, HttpMethod } from '@almighty-shogun/bun-server';

const route = defineRoute('/users/:id', HttpMethod.Get, (request, response) => {
    return response.json({ id: request.params.id });
});

Parameters

path: Path
Route path passed to Bun's typed request handler.

method: Method
HTTP method handled by the route.

handler: RouteHandler<Path, WebSocketData>
Handler that receives the Bun request, response factory, and Bun server.

Returns

An immutable route definition that can be included in a route collection.

Type signature

ts
declare function defineRoute<
    const Path extends string,
    const Method extends HttpMethod,
    WebSocketData = undefined
>(
    path: Path,
    method: Method,
    handler: RouteHandler<Path, WebSocketData>
): RouteDefinition<Path, Method, WebSocketData>;

All packages are released under the MIT License.