apiRoute
Defines an API route module.
Defines an API route module. It is an identity function with validation: it checks that every
key is an HTTP method (or csrf / middleware) and that handlers are functions, so a typo such
as Get fails at startup instead of answering 405 in production.
Import#
ts
import { apiRoute } from "@shadow-js/dusk/server";
Signature#
ts
function apiRoute<const M extends ApiModule>(module: M): Readonly<M>
Parameters#
| Name | Description |
|---|---|
module |
The methods of the route. |
Returns#
The same module, frozen.
Throws#
- TypeError for an unknown key or a non-function handler.
Examples#
ts
export default apiRoute({
GET: (event) => json({ id: event.params.id }),
POST: (event) => json({ created: true }, { status: 201 }),
});