@alxia/openapi

中文文档
0.6.14发布于 2026/11/02MITTypeScriptES Module零依赖近 30 天下载 3787

OpenAPI spec first for @alxia/core: the routes of an app bound to the operations generated from its document, and checked against them

原始使用文档
文档目录10 节
  1. Spec first, end to end
  2. API docs
  3. Every operation has a route
  4. Only the operations
  5. Under a prefix
  6. Routes with middlewares
  7. Traps
  8. Coming from @alxia/openapi 0.3 or @alxia/openapi-routes
  9. API
  10. Documentation

@alxia/openapi

OpenAPI spec first for @alxia/core. The OpenAPI document is the source: @nxgt/openapi-codegen's alxia option generates each operation as { method, path, schema }, @alxia/core's app.route(operation, ...middlewares, handler) binds a handler to it, and this package's implemented and matchesSpec check that the app routes every operation of the document (and, with strict: true, only those). A route the document declares and nobody wrote fails a test, not a client.

bun add -d @alxia/openapi typescript
bun add -d --exact @nxgt/openapi-codegen
bun add zod

@alxia/core and typescript are its peers; @alxia/core is the app's own dependency. @nxgt/openapi-codegen writes the operations, and the code it writes imports zod (4.5.4 or later) at runtime. For a check at startup rather than in a test, install @alxia/openapi without -d.

bun create @alxia my-api --template api starts a project wired this way.

Spec first, end to end

Write openapi.yaml — here with a listTodos and a createTodo operation — then point the generator at it:

// openapi-codegen.config.ts
import { defineConfig } from '@nxgt/openapi-codegen';

export default defineConfig({
    input: 'openapi.yaml',
    output: 'src/generated',
    alxia: true, // writes src/generated/alxia.ts
    validationErrors: false, // alxia sends its own 400: declare it in the spec
});
bunx nxgt-openapi generate # writes src/generated/, alxia.ts included

Bind each operation to its handler, with the middlewares it needs:

// src/app.ts
import { alxia, defineMiddleware } from '@alxia/core';
import { operations } from './generated/alxia';
import type { Todo } from './generated/types';

const requireKey = defineMiddleware(({ request, reply }, next) =>
    request.headers.get('x-api-key') === Bun.env['API_KEY']
        ? next()
        : reply(401, { error: 'unauthorized' as const }),
);

const todos: Todo[] = [];

export const app = alxia()
    .route(operations.listTodos, ({ reply }) => reply.ok(todos))
    .route(operations.createTodo, requireKey, ({ body, reply }) => {
        const todo = { id: todos.length + 1, title: body.title, done: false };
        todos.push(todo);
        return reply.created(todo);
    });

And check the app against the same operations:

// src/app.spec.ts
import { test } from 'bun:test';
import { matchesSpec } from '@alxia/openapi';
import { app } from './app';
import { operations } from './generated/alxia';

test('routes every operation of openapi.yaml', () => {
    matchesSpec(app, operations);
});

The spec-first guide walks through every step: the document, alxia's own 400, middlewares, committing the generated files, and a client from the same document.

API docs

apiDocs serves the document and an interactive page for it, with no configuration. The page loads from a CDN, with a pinned version and a subresource integrity hash, so nothing is added to your dependencies.

import { alxia } from '@alxia/core';
import { apiDocs } from '@alxia/openapi';
import spec from '../openapi.yaml'; // Bun imports YAML, and bundles it

const app = alxia().plugin(apiDocs({ spec }));
// GET /docs                  the page (Scalar)
// GET /docs/openapi.yaml     the document
// GET /docs/openapi.json     the same, as JSON

spec is the document as an object — imported, as above, so bun build bundles it and an image that holds dist/ alone still serves it — or a path to a YAML or JSON file, read once at startup from the working directory. Options: path (/docs), ui ('scalar' or 'swagger'), title, servers (replaces the document's) and enabled. The document is public unless you guard it: to serve it in development alone, read the environment at runtime, with Bun.env — apiDocs({ spec, enabled: Bun.env.NODE_ENV === 'development' }) — never process.env.NODE_ENV, which bun build replaces with the mode of the build; to keep it behind a login, give the guard to use('/docs', …) before the plugin. The page sets its own Content-Security-Policy, which secureHeaders keeps, and matchesSpec leaves its routes out. The guide has the options, the policy, a guarded page, and apiDocs beside a GraphQL endpoint.

Every operation has a route

import { implemented } from '@alxia/openapi';

implemented(app, operations); // routes the app serves beside the spec are fine

When some are missing, it throws, naming each by method, path and operation id:

TypeError: implemented(): 2 operations have no route: GET /pets/:petId (getPet), QUERY /employees (searchEmployees)

Only the operations

import { matchesSpec } from '@alxia/openapi';

const { extra } = matchesSpec(app, operations);

matchesSpec throws as implemented does: on each operation with no route, and so on a route of another method or path. A route no operation declares (a proxied one, a health check, a hand-written one) does not fail; it is returned as extra, [{ method, path }], so a test can assert on it. An app.all(path, …) route serves no operation, and is listed as { method: 'ALL', path }.

For a spec that must be exhaustive, pass strict: true: each route no operation declares fails too, exclude aside. The routes of apiDocs() and the probes of @alxia/core's health() are left out already:

matchesSpec(app, operations, {
    strict: true,
    exclude: (route) => route.path === '/metrics',
});
TypeError: matchesSpec(): 1 operation has no route: GET /pets/:petId (getPet); 1 route has no operation: POST /admin/reset

Upgrading from before this check was lenient: to keep the old check, pass strict: true.

Under a prefix

implemented(app, operations, { prefix: '/api' });

For details about prefixes and how routes are matched, see matching.md. The checks read app.routes and nothing else: they send no request, and check no schema.

Routes with middlewares

app.route(operation, ...middlewares, handler) validates the request and checks the handler's reply just before the handler, or where validate(operation) and responds(operation) stand. A middleware's own reply, such as an auth's 401, is sent as it is. The checks match such a route as any other, by method and path:

import { alxia, validate } from '@alxia/core';
import { operations } from './generated/alxia';

// requireKey, todos: as above
export const app = alxia()
    .route(operations.listTodos, ({ reply }) => reply.ok(todos))
    .route(
        operations.createTodo,
        requireKey, // first: an anonymous client gets its 401 before the body is read
        validate(operations.createTodo),
        ({ body, reply }) => reply.created({ id: todos.length + 1, title: body.title, done: false }),
    );

Put the key check before validate(...): auth first, so an anonymous client gets no body parsed, up to bodyLimit, and no validation issues back, which would reveal the schema.

Traps

  • validationErrors defaults to true, which declares in the client-facing files a 400 alxia never sends: set validationErrors: false and declare alxia's { error: 'validation', issues } in the spec.
  • The generator does not turn security into a middleware: write one with defineMiddleware and give it to route(operation, auth, handler).
  • In @nxgt/openapi-codegen 0.11.0, a cookie parameter is validated as cookies by alxia.ts alone: the client files leave it out with an ignored warning, since a client does not set cookies. A cookie that is a list or an object fails the run.

Coming from @alxia/openapi 0.3 or @alxia/openapi-routes

@alxia/openapi 0.1 to 0.3 was another package: it wrote a document from the app's routes (openapi, docs, toJsonSchema). It is retired, since the document now comes first, and 0.4 has none of its exports. Save the document it served, the app's /openapi.json, as the starting point of your own (the generator reads JSON as well as YAML), generate the operations from it, and bind the routes with app.route().

@alxia/openapi-routes was this package under its former name, and is removed from the repository. Move to @alxia/openapi: the same functions, options and messages, except that exactly and ExactlyOptions, its deprecated aliases of matchesSpec and MatchesSpecOptions, are gone at 0.5.

bun remove @alxia/openapi-routes
bun add -d @alxia/openapi
// before
import { implemented, matchesSpec } from '@alxia/openapi-routes';
// after
import { implemented, matchesSpec } from '@alxia/openapi';

The core's side of the move is in its upgrading notes.

API

export
implemented(app, operations, options?), ImplementedOptionsthrows a TypeError listing each operation with no route, or one with the core's reason for an operation path no route may be declared at. prefix
matchesSpec(app, operations, options?), MatchesSpecOptions, MatchesSpecReportthe same, and returns { extra }, the routes no operation declares; apiDocs()'s and @alxia/core's health() probes left out; under strict: true the rest throw instead. prefix, strict, exclude
apiDocs(options), ApiDocsOptions, DocsUi, DocsServera plugin: the page at path, the document at path/openapi.yaml and .json. spec, path, ui, title, servers, enabled
isApiDocsRoute(route)whether apiDocs declared a route, given any { handler } (Pick<RouteDefinition, 'handler'>, as core's isHealthRoute); matchesSpec leaves them out already
Operationswhat both take: an object of core's RouteOperation, or a list of them

Documentation

版本与发布记录

@alxia/openapi 共发布 23 个版本,首次发布于 2026/10/04;过去 12 个月发布了 23 个版本。

版本发布日期距今
0.6.14latest2026/11/025 天前
0.6.132026/11/016 天前
0.6.122026/10/2910 天前
0.6.112026/10/2612 天前
0.6.102026/10/2216 天前
0.6.92026/10/2216 天前

关于 @alxia/openapi 的常见问题

@alxia/openapi 是什么?

OpenAPI spec first for @alxia/core: the routes of an app bound to the operations generated from its document, and checked against them

如何安装 @alxia/openapi?

在项目目录运行 npm install @alxia/openapi,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:typescript(^6.0.3 || ^7.0.0)、@alxia/core(^0.19.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@alxia/openapi 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@alxia/openapi 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@alxia/openapi 可以商用吗?

@alxia/openapi 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@alxia/openapi 的下载量有多少?

根据 npm 官方下载统计,@alxia/openapi 在 2026/10/06 – 2026/11/04 这 30 天内被下载 3,787 次,日均约 126 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@alxia/openapi 还在维护吗?

最新版本 0.6.14 发布于 2026/11/02(5 天前);至今共发布 23 个版本,其中过去 12 个月发布了 23 个;首次发布于 2026/10/04。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。