Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在swagger nestjs中手动添加摘要和正文_Swagger_Swagger Ui_Nestjs_Nestjs Swagger - Fatal编程技术网

如何在swagger nestjs中手动添加摘要和正文

如何在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

我试图在我的swagger文档路线中添加摘要,但是我找不到合适的装饰器来定义摘要

有些路由我没有指定任何DTO。因此,我想手动添加该端点的请求主体

user.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);
  }

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;
}