NestJS 공식문서: https://docs.nestjs.com/openapi/introduction
main.ts
config가 swagger의 option으로 Title, Description, Version등을 설정할 수 있다.
setup()으로 swagger endpoint를 설정할 수 있다.
localhost/8000/api 로 접속하면 swagger 문서를 볼 수 있다.
Nest가 자동으로 controller를 분석해서 해당하는 api를 보여준다.
또한 해당 Api에 대한 설명을 제공하고자 할 때는 해당 controller 파일에서 @ApiOperation()을 추가하면 된다.
cats.controller.ts
또한 request를 보낼 때 어떤 정보를 담아야하는지에 대한 정보를 제공하기 위해서는 CatRequestDto를 정의한 파일에서 @ApiProperty 데코레이터를 추가해주면 된다.
dto > cats.request.dto.ts
response에 대한 정보는 controller 파일에서 @ApiResponse 데코레이터로 설정할 수 있다.
cats.controller.ts
status 200으로 성공하는 경우 response type을 Dto를 사용해서 설정할 수 있다.
response dto를 생성해서 사용한다.
> dto > cat.dto.ts
id, email, name은 다 schema에서 정의한 내용이지만 cat.controller.ts나 cat.dto.ts에서 다시 정의하여 사용하고 있다.
재사용성을 높이기 위해서 schema에서 @ApiProperty를 추가해서 정의하고 이를 재사용하는 방향으로 리팩토링을 하면 좋을 것 같다.
cats.schema.ts
cat.dto.ts
Response는 password를 반환해주지 않는다.
PickType(Cat, ['email', 'name'] as const)을 이용하면 Cat이라는 클래스에서 필요한 부분만 가져올 수 있다.
cats.request.dto.ts
Cat 스키마를 상속받는다.