⚠️ 사전 준비 명령어
npm install --save @nestjs/swagger swagger-ui-express
npm install --save @nestjs/swagger
// 둘 다 했는데도 안되면
npm install --save @nestjs/swagger fastify-swagger
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
// 저는 자동 import가 안되서 그냥 복붙했어요
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
declare const module: any;
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const port = process.env.PORT || 3000;
const config = new DocumentBuilder() // 여기서부터 ---------
.setTitle('animalNest Api')
.setDescription('동물병원 예약 시나리오 개발을 위한 API문서')
.setVersion('1.0')
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api', app, document); // 여기까지 ---------
await app.listen(port);
console.log(`listening on port ${port}`)
if (module.hot) {
module.hot.accept();
module.hot.dispose(() => app.close());
}
}
bootstrap();
여기까지 설정하고 http://localhost:3000/api 들어가면 아래 화면 처럼 나옵니다
import { Body, Controller, Get, Post, UseGuards } from '@nestjs/common';
import { PaymentService } from './payment.service';
import { AuthGuard } from '../auth/auth.guard';
import { PaymentInfo } from './paymentDatabase';
import { ApiOperation } from '@nestjs/swagger'; // import 추가
import { PaymentRequestDto } from './dto/payment.request.dto';
@Controller('api/payment')
export class PaymentController {
constructor(private readonly paymentService: PaymentService) {
}
@Get()
getHello() {
return this.paymentService.getHello();
}
@ApiOperation({summary : '결제정보 저장'}) // 컨트롤러단에서 이름 정하기
@Post()
savePaymentInfo(@Body() paymentInfo: PaymentRequestDto) {
return this.paymentService.savePaymentInfo(paymentInfo)
}
}
import { CardCompany } from "../model";
import { ApiProperty} from '@nestjs/swagger' // import하기
export class PaymentRequestDto {
@ApiProperty({
example: '1',
description: '결제할 유저의 pk 넘버',
required: true
})
userId: number;
@ApiProperty({
example: '1234123412341234',
description: '카드번호',
required: true
})
cardNum: number;
@ApiProperty({
example: '2412',
description: 'yy/mm 카드 유효기간',
required: true
})
endDate: string;
@ApiProperty({
example: '123',
description: '카드 뒷면 cvc 넘버',
required: true
})
cvc: number;
@ApiProperty({ example: 'kookmin', description: '카드 회사', required: true,
enum: CardCompany, enumName: 'CardCompany' })
cardCompany: CardCompany.Kookmin
@ApiProperty({
example: '10000',
description: '결제 금액',
required: true
})
price: number;
}