SwaggerModule의 장점

The SwaggerModule searches for all @Body()@Query(), and @Param() decorators in route handlers to generate the API document. It also creates corresponding model definitions by taking advantage of reflection. Consider the following code:

→ SwaggerModule은 자동으로 Body, Query, Param decorators를 검색해서 API 문서를 만들어준다. 또한 reflection을 활용해서 해당 모델 정의를 생성해준다.

<aside> 💡 Dto를 알아서 판별해서 해당 모델을 생성해준다.

</aside>

@Post()
async create(@Body() createCatDto: CreateCatDto) {
  this.catsService.create(createCatDto);
}

Untitled

그러나 DTO에서 가져올 때, ApiProperty를 등록해주지 않으면 DTO 자체만 가져오고, 해당하는 Property는 가져오지 않는다.

따라서 DTO를 생성하고, 해당하는 Property에 ApiProperty를 등록해주자.

import { ApiProperty } from '@nestjs/swagger';

export class SignupReqDto {
  @ApiProperty({ required: true, example: '[email protected]' })
  email: string;

  @ApiProperty({ required: true, example: 'Password1!' })
  password: string;

  @ApiProperty({ required: true, example: 'Password1!' })
  passwordConfirm: string;
}

export class SigninReqDto {
  @ApiProperty({ required: true, example: '[email protected]' })
  email: string;

  @ApiProperty({ required: true, example: 'Password1!' })
  password: string;
}
@ApiProperty({
  description: 'The age of a cat',
  minimum: 1,
  default: 1,
})
age: number;

또한 Type도 지정 가능하다.

@ApiProperty({
  type: Number,
})
age: number;

@ApiProperty({ type: [String] })
names: string[];