자세한 준비물은 공식문서 참고하면 된다.
main.ts
const config = new DocumentBuilder()
.setTitle('Cats example')
.setDescription('The cats API description').setVersion('1.0').addTag('cats').build(); // swagger 세팅
const document = SwaggerModule.createDocument(app, config); // swagger 세팅
SwaggerModule.setup('docs', app, document); // swagger 세팅
controller
@ApiOperation({ summary: '회원가입' }) //swagger에서 api summary
@ApiResponse({ status: 500, description: 'Server error' }) // swagger에서 response 케이스 표출
@ApiResponse({ status: 200, description: 'success', type: ReadOnlyCatDto }) // swagger에서 response 케이스 표출
@Post()
// DTO(Data Transfer Object): 계층 간 데이터 교환을 위한 데이터 규격을 의미하는 객체
// request를 받고, 각 계층 간 통신 시 데이터의 validation을 위해 이용된다.
async signup(@Body() body: CatRequestDto) {
// body는 @nestjs/swagger에서 인식하여 보여준다.
return await this.catsService.signup(body);
}
-------------------
schema
@ApiProperty({
// swagger에서 각 값에 대한 설명 추가
example: 'example@naver.com',
description: 'email',
required: true,
})
@Prop({
//스키마에 들어가는 각 데이터들의 옵션 설정
required: true,
unique: true,
})
@IsEmail()
@IsNotEmpty()
email: string;
exrpess-basic-auth
이다.
main.ts
asnyc function bootstrap() {
// swagger uri인 /docs에 계정 권한 설정
app.use(
['/docs', '/docs-json'],
basicAuth({
challenge: true,
users: {
[process.env.swaggerUser]: process.env.swaggerPassword,
},
}),
);
const config = new DocumentBuilder().setTitle('Cats example').setDescription('The cats API description').setVersion('1.0').addTag('cats').build(); // swagger 세팅
const document = SwaggerModule.createDocument(app, config); // swagger 세팅
SwaggerModule.setup('docs', app, document); // swagger 세팅
}