Simple router that handles both eager- and lazy-loaded route handlers to keep your bundle sizes
small. Both default and named module exports are supported with a concise syntax. Appends
Server-Timing
values for route loading duration to the request headers; can be disabled via the
constructor.
Path matches follow glob rules by using Bun.Glob
; see the
documentation for Bun.Glob
for details.
import { Router } from '@mangs/bun-utils/router';
const router = new Router();
router
.get('/*', { default: () => import('./routes/pageRoute.mts') }) // Lazy-loaded, default module export route handler
.post('/submit', { submitRoute: () => import('./routes/submitRoute.mts') }) // Lazy-loaded, named module export route handler
.get('/**', () => new Response('404 page', { status: 404 })) // Eagerly-loaded route handler
.all('/**', () => new Response('', { headers: { allow: 'GET' }, status: 405 })); // Eagerly-loaded route handler
new Router(
usesServerTiming
):Router
Constructor that creates an empty array for route definitions.
Parameter | Type | Default value | Description |
---|---|---|---|
usesServerTiming |
boolean |
true |
Boolean indicating if Server-Timing headers are appended to the request object. |
Property | Modifier | Type | Description |
---|---|---|---|
#routes |
private |
Routes |
Array of route tuples. |
#usesServerTiming |
private |
boolean |
Whether or not Server-Timing headers are appended to the Request object. |
private
#handleMethod(path
,routeHandler
,method
):Router
Register a route handler for the specified HTTP request method.
Parameter | Type | Description |
---|---|---|
path |
string |
A path-like string that will be used to match against the incoming request's path. |
routeHandler |
RouteHandler |
The function that will execute if this route handler is matched. Eagerly-loaded route handlers pass functions in directly; lazy-loaded ones pass in an object whose key corresponds to a module's named export or 'default' for default export. |
method |
"ALL" | "DELETE" | "GET" | "HEAD" | "OPTIONS" | "PATCH" | "POST" | "PUT" |
A valid HTTP method. |
A reference to the instantiated instance (this
) so route handler definitions can be chained.
all(
path
,routeHandler
):Router
Register a route handler that matches all HTTP request methods.
Parameter | Type | Description |
---|---|---|
path |
string |
A path-like string that will be used to match against the incoming request's path. |
routeHandler |
RouteHandler |
The function that will execute if this route handler is matched. Eagerly-loaded route handlers pass functions in directly; lazy-loaded ones pass in an object whose key corresponds to a module's named export or 'default' for default export. |
A reference to the instantiated instance (this
) so route handler definitions can be chained.
router.all('/**', () => new Response('', { headers: { allow: 'GET' }, status: 405 }));
delete(
path
,routeHandler
):Router
Register a route handler that matches the DELETE
HTTP request method.
Parameter | Type | Description |
---|---|---|
path |
string |
A path-like string that will be used to match against the incoming request's path. |
routeHandler |
RouteHandler |
The function that will execute if this route handler is matched. Eagerly-loaded route handlers pass functions in directly; lazy-loaded ones pass in an object whose key corresponds to a module's named export or 'default' for default export. |
A reference to the instantiated instance (this
) so route handler definitions can be chained.
router.delete('/item', { deleteItemRoute: () => import('./routes/deleteItemRoute.mts') })
get(
path
,routeHandler
):Router
Register a route handler that matches the GET
HTTP request method.
Parameter | Type | Description |
---|---|---|
path |
string |
A path-like string that will be used to match against the incoming request's path. |
routeHandler |
RouteHandler |
The function that will execute if this route handler is matched. Eagerly-loaded route handlers pass functions in directly; lazy-loaded ones pass in an object whose key corresponds to a module's named export or 'default' for default export. |
A reference to the instantiated instance (this
) so route handler definitions can be chained.
router.get('/*', { pageRoute: () => import('./routes/pageRoute.mts') })
handleRequest(
request
):Promise
<Response
>
Handles the incoming request after all route definitions have been made.
Parameter | Type | Description |
---|---|---|
request |
Request |
The Request object for the incoming request. |
Promise
<Response
>
A Response
object to build the response sent to the requester.
head(
path
,routeHandler
):Router
Register a route handler that matches the HEAD
HTTP request method.
Parameter | Type | Description |
---|---|---|
path |
string |
A path-like string that will be used to match against the incoming request's path. |
routeHandler |
RouteHandler |
The function that will execute if this route handler is matched. Eagerly-loaded route handlers pass functions in directly; lazy-loaded ones pass in an object whose key corresponds to a module's named export or 'default' for default export. |
A reference to the instantiated instance (this
) so route handler definitions can be chained.
router.head('/*', { pageRoute: () => import('./routes/pageRoute.mts') })
options(
path
,routeHandler
):Router
Register a route handler that matches the OPTIONS
HTTP request method.
Parameter | Type | Description |
---|---|---|
path |
string |
A path-like string that will be used to match against the incoming request's path. |
routeHandler |
RouteHandler |
The function that will execute if this route handler is matched. Eagerly-loaded route handlers pass functions in directly; lazy-loaded ones pass in an object whose key corresponds to a module's named export or 'default' for default export. |
A reference to the instantiated instance (this
) so route handler definitions can be chained.
router.options('/item', { deleteItemRoute: () => import('./routes/deleteItemRoute.mts') })
patch(
path
,routeHandler
):Router
Register a route handler that matches the PATCH
HTTP request method.
Parameter | Type | Description |
---|---|---|
path |
string |
A path-like string that will be used to match against the incoming request's path. |
routeHandler |
RouteHandler |
The function that will execute if this route handler is matched. Eagerly-loaded route handlers pass functions in directly; lazy-loaded ones pass in an object whose key corresponds to a module's named export or 'default' for default export. |
A reference to the instantiated instance (this
) so route handler definitions can be chained.
router.patch('/item', { patchItemRoute: () => import('./routes/patchItemRoute.mts') })
post(
path
,routeHandler
):Router
Register a route handler that matches the POST
HTTP request method.
Parameter | Type | Description |
---|---|---|
path |
string |
A path-like string that will be used to match against the incoming request's path. |
routeHandler |
RouteHandler |
The function that will execute if this route handler is matched. Eagerly-loaded route handlers pass functions in directly; lazy-loaded ones pass in an object whose key corresponds to a module's named export or 'default' for default export. |
A reference to the instantiated instance (this
) so route handler definitions can be chained.
router.post('/item', { postItemRoute: () => import('./routes/postItemRoute.mts') })
put(
path
,routeHandler
):Router
Register a route handler that matches the PUT
HTTP request method.
Parameter | Type | Description |
---|---|---|
path |
string |
A path-like string that will be used to match against the incoming request's path. |
routeHandler |
RouteHandler |
The function that will execute if this route handler is matched. Eagerly-loaded route handlers pass functions in directly; lazy-loaded ones pass in an object whose key corresponds to a module's named export or 'default' for default export. |
A reference to the instantiated instance (this
) so route handler definitions can be chained.
router.put('/item', { putItemRoute: () => import('./routes/putItemRoute.mts') })
HttpRequestMethod: typeof
httpRequestMethods
[number
]
RouteEntry:
Parameters
<InstanceType
<typeofRouter
>["get"
]>
RouteHandler:
RouteHandlerFunction
|RouteHandlerLazyLoaded
RouteHandlerFunction: (
request
) =>Response
|Promise
<Response
>
Parameter | Type |
---|---|
request |
Request |
Response
| Promise
<Response
>
RouteHandlerLazyLoaded:
Record
<string
, () =>Promise
<Record
<string
,RouteHandlerFunction
>>>
Routes: [
HttpRequestMethod
,RouteEntry
][]
const
httpRequestMethods: readonly ["ALL"
,"DELETE"
,"GET"
,"HEAD"
,"OPTIONS"
,"PATCH"
,"POST"
,"PUT"
]