Skip to Content
Senri

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 documentFrom
Paths and methodsThe table keys (:id → {id})
Parameters and the request bodyThe params, query and body schemas. For routes that take forms, bodyType: "form"
ResponsesOnly what you write in response (200) and responses (per status)
summary, tags and so onopenapi

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

On this page