如何在swagger nestjs中手动添加摘要和正文
我试图在我的swagger文档路线中添加摘要,但是我找不到合适的装饰器来定义摘要 有些路由我没有指定任何DTO。因此,我想手动添加该端点的请求主体 user.controller.ts如何在swagger nestjs中手动添加摘要和正文,swagger,swagger-ui,nestjs,nestjs-swagger,Swagger,Swagger Ui,Nestjs,Nestjs Swagger,我试图在我的swagger文档路线中添加摘要,但是我找不到合适的装饰器来定义摘要 有些路由我没有指定任何DTO。因此,我想手动添加该端点的请求主体 user.controller.ts @Controller('users') @ApiTags('User') @ApiBearerAuth() export class UsersController { constructor(private readonly service: UsersService) {} @Get() a
@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,
};
}
}
@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);
}
auth.controller.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,
};
}
}
@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…}})
异步findAll(){}
阅读此处文档中有关原始定义的更多信息:我建议为所有端点(resp和req)创建DTO 下面是如何使用DTOs+@ApiProperty decorator将摘要添加到模式(在屏幕截图中,单击红色圆圈区域中的“模式”)
import { ApiProperty } from '@nestjs/swagger';
export class ExampleRedditDTO {
@ApiProperty({
type: String,
description: "The target subreddit"
})
targetSub!: string;
@ApiProperty({
type: Number,
description: "The number of top posts you want back"
})
postCount!: number;
}