swagger - 如何在 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);
}
解决方案
对于端点摘要,您可以使用@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
推荐阅读
- r - R删除所有以字符串开头的列都有NA的行
- python - 通过 Python 中的节俭服务器 JDBC 在 AWS EMR Spark 集群上执行 SQL?
- python - 文件操作和优化python代码
- php - 搜索具有既定标准 php 的文件
- javascript - 在 PHP while 循环中唯一标识 Javascript 模态
- c++ - 使用 cout 时,我可以设置一个变量来改变浮点域吗?
- c - 只返回空格和字母的函数
- php - 在 wordpress 插件类中将钩子从构造函数更改为 init - 不再工作
- npm - npm -S 标志是什么意思?
- javascript - 在 vuejs 中提交表单,我应该使用表单标签吗?