728x90
반응형
swagger에 관한 자세한 내용은 아래 링크에서 확인 가능합니다.
https://docs.nestjs.com/openapi/introduction#bootstrap
Documentation | NestJS - A progressive Node.js framework
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Progamming), FP (Functional Programming), and FRP (Functional Reac
docs.nestjs.com
먼저 Swagger를 설치해주자.
$ npm install --save @nestjs/swagger swagger-ui-express
main.ts 파일에 swagger를 추가해준다.
main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import * as cookieParser from 'cookie-parser';
import { ValidationPipe } from '@nestjs/common';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.use(cookieParser());
const config = new DocumentBuilder()
.setTitle('User example')
.setDescription('The user API description')
.setVersion('1.0')
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('swagger', app, document);
app.useGlobalPipes(
new ValidationPipe({
whitelist : true,
forbidNonWhitelisted : true,
transform : true
})
)
await app.listen(3001);
}
bootstrap();
그 다음 UserController와 EmailController를 바꿔준다.
user.controller.ts
import { Controller, Request, Response, Get, Post, Body, Patch, Param, Delete, Res, Req, UseGuards } from '@nestjs/common';
import { UserService } from './user.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
//auth
import { LocalAuthGuard } from '../auth/local-auth.guard';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { AuthService } from '../auth/auth.service';
//swagger
import { ApiCreatedResponse, ApiOperation, ApiTags } from '@nestjs/swagger';
@Controller('user')
@ApiTags('유저 API')
export class UserController {
constructor(
private readonly userService: UserService,
private authService: AuthService
) {}
@Get(':id')
@ApiOperation({summary : '유저 확인 API', description: '유저를 찾는다.' })
@ApiCreatedResponse({description: '유저를 찾는다'})
findOne(@Param('id') id: string) {
return this.userService.findOne(id);
}
@Post('signup')
@ApiOperation({summary : '회원가입 API', description: '회원가입' })
@ApiCreatedResponse({description: '회원가입을 한다'})
async signUp(@Res({ passthrough: true}) res : any, @Req() req : any) {
return this.userService.signUp(req.body.userInfo);
}
@UseGuards(LocalAuthGuard)
@Post('login')
@ApiOperation({summary : '로그인 API', description: '로그인' })
@ApiCreatedResponse({description: '로그인을 한다'})
async login(@Request() req, @Res({ passthrough: true }) res : any) {
if(req.user.result === "success") { // ID,PW 둘다 확인 됐을 경우
return this.authService.login(req.user.user, res);
}
else {
return {result : req.user.result}; // 둘 중 하나 이상이 틀릴 경우
}
}
@UseGuards(JwtAuthGuard)
@Get('profile')
@ApiOperation({summary : '토큰확인 API', description: '토큰확인' })
@ApiCreatedResponse({description: '토큰확인을 한다'})
getProfile(@Request() req) {
return req.user;
}
}
email.controller.ts
import { Controller, Get, Post, Body, Patch, Param, Delete, Req, Res } from '@nestjs/common';
import { EmailService } from './email.service';
import { CreateEmailDto } from './dto/create-email.dto';
import { UpdateEmailDto } from './dto/update-email.dto';
//swagger
import { ApiCreatedResponse, ApiOperation, ApiTags } from '@nestjs/swagger';
@Controller('email')
@ApiTags('이메일 API')
export class EmailController {
constructor(private readonly emailService: EmailService) {}
@Get(':id')
@ApiOperation({summary : '이메일 확인 API', description: '이메일로 유저를 찾는다.' })
@ApiCreatedResponse({description: '이메일로 유저를 찾는다'})
findOne(@Param('id') id: string) {
return this.emailService.findOne(id);
}
@Post('send')
@ApiOperation({summary : '이메일 전송 API', description: '이메일을 전송한다.' })
@ApiCreatedResponse({description: '이메일을 전송한다.'})
async emailsend(@Res({ passthrough: true }) res : any, @Req() req : any) {
return await this.emailService.emailSend(req.body.email,res);
}
@Post('cert')
@ApiOperation({summary : '이메일 인증 API', description: '인증번호를 확인한다.' })
@ApiCreatedResponse({description: '인증번호를 확인한다.'})
async emailCert(@Res({ passthrough: true}) res : any, @Req() req : any) {
return await this.emailService.emailCert(req);
}
}
swagger 화면(localhost:3001/swagger)으로 접속해본다.
정상적으로 뜨는 것을 확인했다.
728x90
반응형
'Nest - Next' 카테고리의 다른 글
Nest - Next | n2server | React SideBar with Bootstrap, SCSS (0) | 2021.11.02 |
---|---|
Nest - Next | n2server | Passport , JWT | decode in Client (0) | 2021.10.27 |
Nest - Next | n2server | Passport , JWT (0) | 2021.10.26 |
Nest - Next | n2server | Login (0) | 2021.10.25 |
Nest - Next | n2server | Signup | Complete (0) | 2021.10.25 |