OpenAPI
Make an OpenAPI document from a route table with @senrijs/senri/openapi
import { } from "@senrijs/senri/openapi";
export const = ({
"GET /users/:id": (
{
: .({ : ..() }),
: , // the 200 body
: { 404: }, // other statuses
: { : "Get a user", : ["users"] },
},
({ }) => (),
),
});
const = (, { : { : "My API", : "1.0.0" } });
.("/openapi.json", () => );It makes an OpenAPI 3.1 document by default.
Where each part comes from
| In the document | From |
|---|---|
| Paths and methods | The table keys (:id → {id}) |
| Parameters and the request body | The params, query and body schemas. For routes that take forms, bodyType: "form" |
| Responses | Only what you write in response (200) and responses (per status) |
summary, tags and so on | openapi |
The handler's return type is not known at run time, so it is not in the responses. Routes outside the table, such as those added with app.get(), are not included either.
Turning schemas into JSON Schema
Schemas are converted with Standard JSON Schema (Zod 4.2 and ArkType 2.1.28 or later).
- Wrap Valibot schemas with
@valibot/to-json-schema - For other schemas, pass a converter as
toJSONSchema - With neither, the schema becomes an empty one and a warning is printed
In development mode, when a handler's return value does not match its response schema, a warning is printed in the terminal. The response is not changed, and nothing is checked in production.
Options
Prop
Type