NestJS Swagger

108 阅读1分钟

官网地址 swagger.io/docs/specif…

image.png

yarn add @nestjs/swagger swagger-ui-express -S


//如果使用fastify,则必须安装fastify-swagger而不是swagger-ui-express
yarn add @nestjs/swagger fastify-swagger -S
启动配置 main.ts
import { DocumentBuilder,SwaggerModule } from '@nestjs/swagger'

const options = new DocumentBuilder()
    .setTitle('xxxx')
    .setDescription('接口文档 sb yxk')
    .setVersion('1.0')
    .build();
const document = SwaggerModule.createDocument(app, options);
SwaggerModule.setup('api-docs', app, document);

image.png

分组 controller

@ApiTags("guard模块")

image.png

接口说明 @ApiOperation

  @Get('g')
  @Role('admin')
  @ApiOperation({summary:"测试权限"})
  @ApiParam({name:'userName',description:"用户名",required:true})
  @ApiParam({name:'password',description:"密码",required:true})
  findAll() {
    return this.guardService.findAll();
  }

image.png

image.png