diff --git a/be/src/baseresponse.ts b/be/src/baseresponse.ts new file mode 100644 index 00000000..634e9948 --- /dev/null +++ b/be/src/baseresponse.ts @@ -0,0 +1,7 @@ +export class BaseResponse { + constructor( + public data: T, + public message: string, + public statusCode: number + ) {} +} diff --git a/be/src/error.filter.ts b/be/src/error.filter.ts new file mode 100644 index 00000000..e34d9b5a --- /dev/null +++ b/be/src/error.filter.ts @@ -0,0 +1,21 @@ +import { + ArgumentsHost, + Catch, + ExceptionFilter, + HttpException, +} from "@nestjs/common"; +import { BaseResponse } from "./baseResponse"; +import { Response } from "express"; + +@Catch(HttpException) +export class HttpExceptionFilter implements ExceptionFilter { + catch(exception: HttpException, host: ArgumentsHost) { + const ctx = host.switchToHttp(); + const response = ctx.getResponse(); + const status = exception.getStatus(); + + response + .status(status) + .json(new BaseResponse(null, exception.message, status)); + } +} diff --git a/be/src/main.ts b/be/src/main.ts index d755f0d8..3a7c0205 100644 --- a/be/src/main.ts +++ b/be/src/main.ts @@ -1,10 +1,14 @@ import { NestFactory } from "@nestjs/core"; import { SwaggerModule, DocumentBuilder } from "@nestjs/swagger"; import { AppModule } from "./app.module"; +import { TransformInterceptor } from "./response.interceptor"; +import { HttpExceptionFilter } from "./error.filter"; async function bootstrap() { const app = await NestFactory.create(AppModule); app.setGlobalPrefix("api"); + app.useGlobalFilters(new HttpExceptionFilter()); + app.useGlobalInterceptors(new TransformInterceptor()); const config = new DocumentBuilder() .setTitle("Example API") @@ -14,7 +18,6 @@ async function bootstrap() { .build(); const document = SwaggerModule.createDocument(app, config); SwaggerModule.setup("api", app, document); - await app.listen(3000); } bootstrap(); diff --git a/be/src/response.interceptor.ts b/be/src/response.interceptor.ts new file mode 100644 index 00000000..341a181e --- /dev/null +++ b/be/src/response.interceptor.ts @@ -0,0 +1,25 @@ +import { + Injectable, + NestInterceptor, + ExecutionContext, + CallHandler, +} from "@nestjs/common"; +import { Observable } from "rxjs"; +import { map } from "rxjs/operators"; +import { BaseResponse } from "./baseResponse"; + +@Injectable() +export class TransformInterceptor + implements NestInterceptor> +{ + intercept( + context: ExecutionContext, + next: CallHandler + ): Observable> { + return next + .handle() + .pipe( + map((data) => ({ data: data, message: "Success", statusCode: 200 })) + ); + } +} diff --git a/be/src/user/entities/user.entity.ts b/be/src/user/entities/user.entity.ts index 676811c4..eec0cdc9 100644 --- a/be/src/user/entities/user.entity.ts +++ b/be/src/user/entities/user.entity.ts @@ -1,42 +1,41 @@ import { - Entity, - PrimaryGeneratedColumn, - Column, - CreateDateColumn, - UpdateDateColumn, - DeleteDateColumn - } from 'typeorm'; - - @Entity() - export class User { - @PrimaryGeneratedColumn('increment') - id: number; - - @Column({ type: 'varchar', length: 20, unique : true }) - nickName: string; - - @Column({ type: 'varchar', length: 50 }) - email: string; - - @Column({ type: 'int' }) - age: number; - - @Column({ type: 'boolean' }) - gender: boolean; - - @Column({ type: 'varchar', length: 50, nullable: true }) - password: string | null; - - @Column({ type: 'varchar', length: 20, nullable: true }) - social_provider: string | null; - - @CreateDateColumn({ type: 'timestamp' }) - created_at: Date; - - @DeleteDateColumn({ type: 'timestamp', nullable: true}) - deleted_at: Date | null; - - @UpdateDateColumn({ type: 'timestamp'}) - updated_at: Date; - } - \ No newline at end of file + Entity, + PrimaryGeneratedColumn, + Column, + CreateDateColumn, + UpdateDateColumn, + DeleteDateColumn, +} from "typeorm"; + +@Entity() +export class User { + @PrimaryGeneratedColumn("increment") + id: number; + + @Column({ type: "varchar", length: 20, unique: true }) + nickName: string; + + @Column({ type: "varchar", length: 50 }) + email: string; + + @Column({ type: "int" }) + age: number; + + @Column({ type: "boolean" }) + gender: boolean; + + @Column({ type: "varchar", length: 50, nullable: true }) + password: string | null; + + @Column({ type: "varchar", length: 20, nullable: true }) + social_provider: string | null; + + @CreateDateColumn({ type: "timestamp" }) + created_at: Date; + + @DeleteDateColumn({ type: "timestamp", nullable: true }) + deleted_at: Date | null; + + @UpdateDateColumn({ type: "timestamp" }) + updated_at: Date; +} diff --git a/be/src/user/user.controller.ts b/be/src/user/user.controller.ts index c800a2ef..ee71b732 100644 --- a/be/src/user/user.controller.ts +++ b/be/src/user/user.controller.ts @@ -6,17 +6,17 @@ import { UsePipes, ValidationPipe, } from "@nestjs/common"; -import { ApiBody, ApiOperation, ApiResponse } from "@nestjs/swagger"; +import { ApiOperation, ApiResponse } from "@nestjs/swagger"; import { UserInfoDto } from "./dto/userInfo.dto"; import { UserService } from "./user.service"; @Controller("user") export class UserController { - constructor(private userService: UserService) { } + constructor(private userService: UserService) {} @Post() @ApiOperation({ summary: "유저 회원가입" }) - @ApiResponse({ status: 200, description: "회원가입 성공", type: UserInfoDto }) + @ApiResponse({ status: 200, description: "회원가입 성공" }) @ApiResponse({ status: 400, description: "부적절한 요청" }) @UsePipes(new ValidationPipe()) singup(@Body() userInfoDto: UserInfoDto) { diff --git a/be/src/user/user.repository.ts b/be/src/user/user.repository.ts index cc10ee18..c43b3b56 100644 --- a/be/src/user/user.repository.ts +++ b/be/src/user/user.repository.ts @@ -1,7 +1,7 @@ -import { DataSource, Repository } from 'typeorm'; -import { User } from './entities/user.entity'; -import { UserInfoDto } from './dto/userInfo.dto'; -import { ConflictException, Injectable } from '@nestjs/common'; +import { DataSource, Repository } from "typeorm"; +import { User } from "./entities/user.entity"; +import { UserInfoDto } from "./dto/userInfo.dto"; +import { ConflictException, Injectable } from "@nestjs/common"; @Injectable() export class UserRepository extends Repository { @@ -12,12 +12,11 @@ export class UserRepository extends Repository { const newUser = this.create(userinfoDto); try { await this.save(newUser); - } - catch (err) { + } catch (err) { if (err.code === "23505") { throw new ConflictException("Duplicated Value"); } } return; } -} \ No newline at end of file +} diff --git a/be/src/user/user.service.ts b/be/src/user/user.service.ts index 15908da5..53056bc4 100644 --- a/be/src/user/user.service.ts +++ b/be/src/user/user.service.ts @@ -1,17 +1,15 @@ import { Injectable } from "@nestjs/common"; import { UserInfoDto } from "./dto/userInfo.dto"; -import { InjectRepository } from '@nestjs/typeorm'; -import { Repository } from 'typeorm'; -import { User } from './entities/user.entity'; +import { InjectRepository } from "@nestjs/typeorm"; import { UserRepository } from "./user.repository"; @Injectable() export class UserService { - constructor( - @InjectRepository(UserRepository) - private usersRepository: UserRepository, - ) { } - signup(userInfoDto: UserInfoDto) { - return this.usersRepository.createUser(userInfoDto); - } + constructor( + @InjectRepository(UserRepository) + private usersRepository: UserRepository + ) {} + signup(userInfoDto: UserInfoDto) { + return this.usersRepository.createUser(userInfoDto); + } }