Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add summary and body manually in swagger nestjs

I am trying to add summary in my swagger documentation routes but I am not able to find the appropriate decorator for defining the summary.

There are some routes in which I have not specified any DTO's. So, I would like to manually add request body for that endpoint.

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

Swagger

auth.controller.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);
  }
like image 329
Istiyak Tailor Avatar asked Feb 07 '20 12:02

Istiyak Tailor


People also ask

What is Swaggermodule?

This module is for the Play 1. x series only. Creates a self-documenting meta-description for REST APIs which allows for code-gen, UI-sandbox, and test framework. See more at http://swagger.wordnik.com and at http://developer.wordnik.com/docs.


1 Answers

For Endpoint Summary, you can use @ApiOperation(). For schema, you can use @ApiResponse()

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

Read more about Raw Definitions from the documentations here: https://docs.nestjs.com/recipes/swagger#raw-definitions

like image 191
Chau Tran Avatar answered Dec 31 '22 10:12

Chau Tran