Swagger接口文档

安装依赖

npm install --save @nestjs/swagger swagger-ui-express

使用

// main.ts

import { NestFactory } from "@nestjs/core";
import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger";
import { AppModule } from "./app.module";

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  //swagger
  const config = new DocumentBuilder()
    .setTitle("Nest GO API Doc")
    .setDescription("硬吃Nest.js")
    .setVersion("1.0")
    .addTag("Tag")
    .build();
  const document = SwaggerModule.createDocument(app, config);
  SwaggerModule.setup("doc", app, document);

  await app.listen(3000);
  console.log(`Application is running on: ${await app.getUrl()}`);
}
bootstrap();

这里访问http://localhost:3000/doc就可以请求到接口文档了

相对应的json数据请求地址为http://localhost:3000/doc-json

自定义配置

其实这里的document就是json文件

配置装饰器

swagger的配置装饰器都是以@api开头

1.ApiTags装饰器,让对应的模块分类到对应的标签当中

2. ApiQuery、ApiBody、ApiParam、ApiHeader、ApiHeaders

3. ApiHeaders需要的对象只有三个参数

4. dto的参数配置ApiProperty

5. ApiResponse

6. 文件上传的文档

最后更新于