首页 > 解决方案 > 如何在 swagger nestjs 中手动添加摘要和正文

问题描述

我正在尝试在我的招摇文档路线中添加摘要,但我无法找到合适的装饰器来定义摘要。

有些路线我没有指定任何 DTO。因此,我想为该端点手动添加请求正文。

用户控制器.ts

@Controller('users')
@ApiTags('User')
@ApiBearerAuth()
export class UsersController {

  constructor(private readonly service: UsersService) {}

  @Get()
  async findAll() {
    const data = await this.service.findAll();

    return {
      statusCode: 200,
      message: 'Users retrieved successfully',
      data,
    };
  }
}

昂首阔步

身份验证控制器.ts

  @UseGuards(AuthGuard('local'))
  @Post('login')
  @ApiParam({
    name: 'email',
    type: 'string'
  })
  @ApiParam({
    name: 'password',
    type: 'string'
  })

  async login(@Request() req) {
    return this.authService.login(req.user);
  }

标签: swaggerswagger-uinestjsnestjs-swagger

解决方案


对于端点摘要,您可以使用@ApiOperation(). 对于架构,您可以使用@ApiResponse()

@Get()
@ApiOperation({ summary: 'summary goes here' })
@ApiResponse({ status: 200, description: 'description goes here', schema: { ...define schema here... } })
async findAll() {}

从此处的文档中阅读有关原始定义的更多信息: https ://docs.nestjs.com/recipes/swagger#raw-definitions


推荐阅读