# @chax-at/swagger-helpers-nest 中文文档

规范地址：https://npmcn.com/package/%40chax-at/swagger-helpers-nest/

版本：0\.3\.1
npm 元数据更新时间：2025-09-24T11:13:27.170Z
中文译文更新时间：2026-11-09T13:25:44.653Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40chax-at/swagger-helpers-nest
原文对照：https://npmcn.com/package/%40chax-at/swagger-helpers-nest/?lang=original
项目主页：https://github.com/chax-at/swagger-helpers-nest#readme
源码仓库：https://github.com/chax-at/swagger-helpers-nest

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：20.9 kB，13 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- Node.js 要求：\>= 18
- 运行依赖：0 个
- peer 依赖：@nestjs/common 6 - 11，@nestjs/swagger 7 - 11
- 首次发布：2025-02-27T13:00:23.144Z
- 最新版本发布：2025-05-28T11:39:44.183Z
- 近 30 天下载量：2562（2026-10-10 至 2026-11-08，npm 官方下载统计）
- 已发布版本数：4（近 12 个月 0 个）

## 中文简介

一组工具及装饰器, 以使用 @nestjs/ swagger 来方便生成 OpenAPI( swagger) 文件

## 安装

```sh
npm install @chax-at/swagger-helpers-nest
```

也可以使用：`pnpm add @chax-at/swagger-helpers-nest`、`yarn add @chax-at/swagger-helpers-nest`、`bun add @chax-at/swagger-helpers-nest`

## README

# @ chax-at/ swagger- helpers-nest (帮助者)

这个软件包提供了额外的生成工具 [打开API](https://swagger.io/specification/) 文档 [雀巢JS/斯瓦格](https://github.com/nestjs/swagger) 简单点.

## 使用量

### 先决条件

首先,通过运行安装软件包

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

### 装饰者

为了有更好的时间生成有用的OpenAPI(swagger) doc文件,这个包提供了一套将@nestjs/swagger装饰器组合在一起的装饰器.

以下是一些例子.

#### DTO 属性

```ts
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;
}
```

#### 回应

```ts
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();
  }
}
```

### 员额处理

有时候装潢师只是不切 之后你想做出改变 `@nestjs/swagger`是因为 `buildSwaggerDocument()` 结束了.

对于此, 此软件包提供了一个倒转功能 `traverseDocument` 可以配置访问者.

示例

```ts
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 是什么？

一组工具及装饰器, 以使用 @nestjs/ swagger 来方便生成 OpenAPI\( 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 和提交记录判断。

