@chax-at/swagger-helpers-nest

中文文档
0.3.1发布于 2025/05/28MITTypeScriptCommonJS零依赖构建来源已验证近 30 天下载 2562

A set of tools and decorators to have an easier time generating OpenAPI (swagger) docs with @nestjs/swagger

原始使用文档
文档目录4 节
  1. Usage
  2. Prerequisites
  3. Decorators
  4. Post processing

@chax-at/swagger-helpers-nest

This package provides additional tools to make generating OpenAPI documentations with NestJS/Swagger easier.

Usage

Prerequisites

First, install the package by running

npm i @chax-at/swagger-helpers-nest

Decorators

To have a better time generating useful OpenAPI (swagger) doc files, this package provides a set of decorators that combine @nestjs/swagger decorators.

Here are some examples.

DTO properties

class MugResponse {
  // if a property can have one of multiple types, use
  @ApiPropertyOneOf(TeaDto, CoffeeDto)
  public contents!: TeaDto | CoffeeDto;
}

class CarResponse {
  // if a property is an array of one or more non-primitive types, use
  @ApiPropertyArrayOf(WheelDto)
  public wheels!: WheelDto[];
}

class SuccessResponse {
  // if a property must be one specific value rather than a type, use
  @ApiPropertyOfValue(true)
  public success!: true;
}

class StatusResponse {
  // if a property's value is an enum, use the following to export the enum itself into the docs too
  @ApiPropertyEnum({ Status })
  public status!: Status;
}

Responses

class DrinksController {
  @Get()
  // if a route can have one of multiple response types, use
  @ApiResponseOneOf([TeaResponse, CoffeeResponse])
  public getDrink(): Promise<TeaResponse | CoffeeResponse> {
    return this.drinkService.getDrinkResponse();
  }

  @Post()
  // if a route can have one of multiple response types and the status should be 201, use
  @ApiCreatedResponseOneOf([TeaResponse, CoffeeResponse])
  public createDrink(): Promise<TeaResponse | CoffeeResponse> {
    return this.drinkService.createDrink();
  }
}

Post processing

Sometimes decorators just don't cut it and you want to make alterations after @nestjs/swagger's buildSwaggerDocument() is done.

For this, this package provides a traversal utility traverseDocument which can be configured with visitors.

Example:

const MyVisitor: SchemaVisitor = (schema) => {
  if (matchesSomeCondition(schema)) {
    // modify schema in place
  }
};

const document = buildSwaggerDocument(app);
traverseDocument(document, {
  propertyVisitors: [
    Length1AllOfToOneOfVisitor, // convert allOf's with one entry to oneOf's
    MoveNullableToOneOfVisitor, // move a nullable:true into a sibling oneOf
    MyVisitor, // your own
  ],
  operationVisitors: [
    // an example visitor that adds an operation's method as a tag
    (op, method) => {
      op.tags.push(method);
    },
    // an example visitor that removes operations based on a tag
    (op) => {
      if (op.tags?.include('DoNotExpose')) {
        return 'delete';
      }
    },
  ],
});

版本与发布记录

@chax-at/swagger-helpers-nest 共发布 4 个版本,首次发布于 2025/02/27;过去 12 个月发布了 0 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.3.1latest2025/05/281.5 年前
0.3.02025/05/281.5 年前
0.2.02025/03/311.6 年前
0.1.02025/02/271.7 年前

关于 @chax-at/swagger-helpers-nest 的常见问题

@chax-at/swagger-helpers-nest 是什么?

A set of tools and decorators to have an easier time generating OpenAPI (swagger) docs with @nestjs/swagger

如何安装 @chax-at/swagger-helpers-nest?

在项目目录运行 npm install @chax-at/swagger-helpers-nest,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@nestjs/common(6 - 11)、@nestjs/swagger(7 - 11)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@chax-at/swagger-helpers-nest 支持 TypeScript 吗?

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

@chax-at/swagger-helpers-nest 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@chax-at/swagger-helpers-nest 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@chax-at/swagger-helpers-nest 可以商用吗?

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

@chax-at/swagger-helpers-nest 的下载量有多少?

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

@chax-at/swagger-helpers-nest 还在维护吗?

最新版本 0.3.1 发布于 2025/05/28(1.5 年前);至今共发布 4 个版本,其中过去 12 个月发布了 0 个;首次发布于 2025/02/27。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。