Response를 반환할 때, 필요한 값만 반환한다.
// user.entity.ts
import { Entity, Column, PrimaryGeneratedColumn, AfterInsert, AfterUpdate, AfterRemove } from 'typeorm'
import { Exclude } from 'class-transformer';
@Entity()
export class User {
@PrimaryGeneratedColumn()
id : number;
@Column()
email : string;
@Column()
@Exclude()
password : string;
// 비밀번호는 반환을 하지 않아야 하므로 Exclude 데코레이터를 붙임.
...
}
//user.controller.ts
import { Body, Controller, Delete, Get, Param, Post, Query, Patch, NotFoundException, UseInterceptors, ClassSerializerInterceptor } from '@nestjs/common';
// UseInterceptor, ClassSerializerInterceptor를 import
...
@UseInterceptors(ClassSerializerInterceptor)
@Get('/:id')
async findUser(@Param('id') id : string) {
const user = await this.userService.findOne(parseInt(id))
if(!user) {
throw new NotFoundException('user not found')
}
return user
}
NestJs가 추천하는 방식은 권한에 따른 데이터 조회 처리를 하기가 적절하지 않다. 그래서 커스텀 DTO를 만들어서, 처리한다.