OpenAPI #

HTTP API references tend to share three problems: field descriptions sit apart from the example payloads, so readers keep looking back and forth; every operation is an isolated card, so readers piece the call order together themselves; and what changed between versions is buried in a changelog.

@kawapress/plugin-openapi reads an OpenAPI 3.0 or 3.1 spec at build time and generates the reference pages, with no .md files to generate and commit:

  • The payload is the documentation: request and response bodies are shown as example JSON, with the type and constraints at the end of each line; click a line for the full description. A field list and TypeScript declarations are two more views of the same data.
  • A resource map: the overview is a resource × action grid, so what the API can and cannot do shows at a glance.
  • Journeys: Arazzo workflows connect operations into a flow, mark how values move between the steps, and run with mocks in one click.
  • Try it: by default the request is checked against the schema in the browser and answered with the example response. No server, no CORS.
  • API changes: a snapshot per version lists breaking, added and deprecated changes, and fields show the version they appeared in.

The sample API of this site, for a made-up coffee shop, is generated by this plugin.

Install #

Shell
npm install --save-dev @kawapress/plugin-openapi
TypeScript
import openapiPlugin from '@kawapress/plugin-openapi'
import { nagi } from 'kawapress/nagi'

export default nagi({
  plugins: [
    openapiPlugin({
      spec: './openapi.yaml',
      workflows: './order-flow.arazzo.yaml',
    }),
  ],
})

You get these pages:

Text
/openapi/                            Resource map: resources × actions, journeys, models
/openapi/orders/                     A resource and its operations
/openapi/orders/create-order         An operation (the operationId in kebab-case)
/openapi/webhooks/order-paid         A webhook
/openapi/models/Order                A model, and the operations that use it
/openapi/journeys/order-and-pay      A journey
/openapi/changes                     API changes (with two snapshots or more)

A resource is the operation's first tag, or the first path segment without one. Every locale has its own copy, such as /en/openapi/; the fixed texts are in Chinese or English by page language, and the spec's own texts are shared by all locales.

These are ordinary Markdown pages: search, the sitemap, llms.txt and the per-page .md have them. The automatic sidebar sorts by path; for a "resource → operation" sidebar, write it in themeConfig.sidebar.

Options #

TypeScript
interface OpenApiPluginOptions {
  /** OpenAPI 3.0 / 3.1 document, JSON or YAML, relative to the site root. */
  spec: string
  /** Arazzo 1.x workflow files. */
  workflows?: string | string[]
  /** Where the pages are mounted, `openapi/` by default. */
  route?: string
  /** Only generate pages for these locales; all by default. */
  locales?: string[]
  /** Snapshot folder, `openapi-snapshots` by default. */
  snapshotDir?: string
  /** "Try it": mock only by default; `server` adds a real server; `false` turns it off. */
  tryIt?: false | { server?: string }
  /** Languages and order of the code samples, `['curl', 'javascript', 'python']` by default. */
  samples?: ('curl' | 'javascript' | 'python')[]
}

$ref must point inside the document (#/components/...). Bundle a spec split over several files first, for example with redocly bundle.

Operation pages #

Each operation has a page: method and path (copy as curl, open "Try it"), description, parameters, request body, responses and code samples.

Bodies open in the payload view:

  • The example comes from the spec's example / examples. Without one, the plugin generates one from the schema and says so above the payload; the build lists the operations that lack examples.
  • The grey text at the end of each line is the type and constraints, with required in red; deprecated fields are struck through; fields with a since version carry it as a badge.
  • Click a line (or press Enter on it) to open its path, type, constraints, default and description.
  • Arrays show their first item and fold the rest into "N more".

Switch to the field list (nested fields by path; a named model expands where it first appears and links to its page after that) or to the TypeScript declarations in the corner.

Try it #

The "Try it" panel lets readers edit the path and query parameters and the body, then send:

  • Mock (default): checks the request against the schema in the browser, listing every problem with required fields, types, ranges and lengths; a valid request gets the example of the first 2xx response. Nothing goes over the network.
  • Server: with tryIt: { server: 'https://sandbox.example.com' }, the request really goes to that URL. The token stays in the page; CORS is up to that server.

Journeys #

Real integrations rarely call one operation. List Arazzo 1.x files in workflows, and each workflow gets a journey page:

YAML
workflows:
  - workflowId: orderAndPay
    summary: Order and pay
    steps:
      - stepId: pickDrink
        operationId: listMenuItems
        outputs:
          itemId: $response.body#/data/0/id
      - stepId: createOrder
        operationId: createOrder
        requestBody:
          payload:
            items:
              - menu_item_id: $steps.pickDrink.outputs.itemId
                quantity: 2

The page shows each step's request and response. A value referenced as $steps.<step>.outputs.<name> has the same colour in the response that produces it and the request that uses it, with "← itemId from step 1" underneath; $inputs values get a dashed outline. "Run with mocks" lights the steps up in order without sending anything.

A step names its operation with operationId (also as $sourceDescriptions.<name>.<operationId>) or operationPath; steps that call another workflow are not supported yet. An unknown operation, or an output of a later step, fails the build.

References in articles #

Guides can refer to operations and models directly:

Markdown
Call :api{op=createOrder} first; amounts are :api{schema=Money}.

<OpenApiOperation op="createOrder" />

Call POST/ordersCreate an orderCreates an order waiting for payment at one store. The payurl in the response is valid for 15 minutes; after that the order is cancelled. first; amounts are MoneyMoneyAn amount in the smallest unit of a currency..

POST/ordersCreate an order
Creates an order waiting for payment at one store. The pay_url in the response is valid for 15 minutes; after that the order is cancelled.
store_idstring · required
itemsLineItem[] · required · 1–20 items
Details →
  • :api{op=<operationId>}, :api{schema=<model>} and :api{journey=<workflowId>} are inline references with a hover card.
  • <OpenApiOperation op="..." /> is an operation card listing the required request fields; fields="store_id,pickup_at" picks the fields instead.
  • <OpenApiSchema name="..." /> embeds a model's payload view.

A reference to an operation, model or journey that does not exist fails the build and names the page. Spec edits update the pages in the dev server.

API changes #

The plugin keeps the shape of each version's API as a snapshot: per operation, the parameters, request fields, response statuses and fields with their types and whether they are required. Descriptions are left out, so editing prose is not an API change.

Shell
npx kawapress-openapi snapshot --spec ./openapi.yaml

Run it after releasing a version (with info.version updated); the snapshot goes to openapi-snapshots/<version>.json and is committed with the code. An existing snapshot of the same version is kept unless you pass --force.

With two snapshots or more, the changes page lists, newest version first:

  • Breaking: removed operations, fields or response statuses, changed field types, new required request inputs, optional inputs that became required;
  • Added: new operations, new optional request fields, new response fields or statuses;
  • Deprecated: operations or fields newly marked deprecated;
  • Compatible changes: everything else, such as required inputs that became optional.

A field without x-since gets the version of the first snapshot it appears in, shown in the payload view and the resource map. The build warns when the spec differs from the latest snapshot.

Edit this pageLast updated:

Comments