@dunx/openapi

中文文档
3.10.6发布于 2026/10/30MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 4467

OpenAPI 3.1 documents for dunx controllers, generated from the schemas the routes already validate, with Swagger UI or Scalar mounted over them

原始使用文档
文档目录5 节
  1. Install
  2. Usage
  3. What is here
  4. Notes
  5. License

@dunx/openapi

An OpenAPI 3.1 document generated from the routes an app already has, with Swagger UI or Scalar served from the same Bun.serve as everything else.

The difference from a reflection-based generator is what it reads. A route's schemas are the objects the request path validates against. Its security metadata (@Public(), @Roles()) is what its guards enforce. Both are already on the route, so the document is not a second description that can drift from the first.

Install

bun add @dunx/openapi zod

zod is a peer dependency. Route validation targets Standard Schema, so Valibot and ArkType work for validation too, but the OpenAPI document needs JSON Schema, and only zod has z.toJSONSchema. A non-zod schema validates at runtime and appears as a permissive entry in the document, with a warning at generation time.

The documentation UI is opt-in, one optional peer per subpath. Install the one you mount, or neither:

ImportRendererPeer to install
@dunx/openapi/swaggerSwaggerRendererswagger-ui-dist
@dunx/openapi/scalarScalarRenderer@scalar/api-reference

Usage

import { HttpFactory } from '@dunx/http';
import { OpenApiModule } from '@dunx/openapi';
import { SwaggerRenderer } from '@dunx/openapi/swagger';

const app = await HttpFactory.create(
  OpenApiModule.forRoot({
    title: 'Payments',
    version: '1.4.0',
    root: AppModule, // the graph to document, and the graph that gets imported
    // Omit for the document alone: no `renderer`, no page route.
    renderer: new SwaggerRenderer({ docExpansion: 'list' }),
  }),
);
app.setGlobalPrefix('api');
await app.listen(3000);
// GET /api/docs          the explorer
// GET /api/openapi.json  the document

forRootAsync takes a factory that may inject, so an app can read its title off ConfigService or contribute another library's schema.

What is here

The OpenAPI guide is canonical.

PieceWhat it does
OpenApiModuleWraps the root it documents, mounts the JSON and a renderer's page
DocsRendererThe page contract. PackageAssets serves a renderer's files
@ApiDocSummary, description, tags and deprecation on a route or a class
@ApiHiddenA real route kept out of the document. Lives in @dunx/http
describeRoutesThe routes as data, constructing nothing
generateDocumentThe document from those routes
betterAuthDocumentbetter-auth's own paths merged in. Lives in @dunx/auth
bunx dunx-openapiThe document written to a file, with no container and no server

Security comes from the same @Public() and @Roles() metadata the guards read at runtime, so there is no second annotation for the document to disagree with. A response schema is documentation only: the verb decorators hold the handler's return type to it at compile time instead of validating every response.

authorize decides who may see the document, the page and the page's assets. It takes the same Authorize as @dunx/dashboard. A refused request gets a 404. If authorize returns a Response, for example a redirect to sign in, that response is sent as is.

Notes

  • Add .meta({ id }) to a zod schema to put it in components/schemas. Without an id it is inlined wherever it is used. .strict() after .meta() drops the metadata, so call .meta() last.
  • Prose belongs in description. An explorer labels a schema by title, which @dunx/openapi fills with the component name.
  • The page embeds the document rather than fetching it, and loads the renderer's files same-origin from your own install. Nothing reaches a CDN, unless you turn Scalar's withDefaultFonts back on.
  • renderer sits beside root rather than in forRootAsync's factory: the controller declares its routes before there is a container to run one.

License

MIT

版本与发布记录

@dunx/openapi 共发布 83 个版本,首次发布于 2026/08/02;过去 12 个月发布了 83 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.10.6latest2026/10/3014 天前
3.10.52026/10/2222 天前
3.10.42026/10/2024 天前
3.10.32026/10/1430 天前
3.10.22026/10/081 个月前
3.10.12026/09/261 个月前

关于 @dunx/openapi 的常见问题

@dunx/openapi 是什么?

OpenAPI 3.1 documents for dunx controllers, generated from the schemas the routes already validate, with Swagger UI or Scalar mounted over them

如何安装 @dunx/openapi?

在项目目录运行 npm install @dunx/openapi,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@dunx/core(^3.10.6)、@dunx/http(^3.10.6)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。它还提供命令行工具 dunx-openapi,可以用 npx dunx-openapi 直接运行。

@dunx/openapi 支持 TypeScript 吗?

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

@dunx/openapi 支持 ESM 还是 CommonJS?

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

@dunx/openapi 可以商用吗?

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

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

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

@dunx/openapi 还在维护吗?

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