회원가입 API를 통해 클라이언트에게 응답을 보내줘야 할 때, 엔티티의 패스워드 속성은 숨기고 싶을 때가 있습니다.
우선 class-validator와 class-transformer를 설치합니다. 저는 pnpm을 사용한 프로젝트이므로 pnpm으로 설치합니다.
$ pnpm i --save class-validator class-transformer
응답시 숨기고 싶은 엔티티의 속성에 @Exclude 데코레이터를 삽입합니다.
import { Exclude } from 'class-transformer';
@Entity()
export class User extends BaseEntity {
...
@Column({ nullable: false })
@Exclude()
password: string;
...
}
응답시에 속성을 숨기고 싶은 컨트롤러또는 핸들러에서 @UseInterceptor 데코레이터로 사용합니다.
import { ClassSerializerInterceptor, Controller, UseInterceptors } from '@nestjs/common';
@Controller('auth')
@UseInterceptors(ClassSerializerInterceptor)
export class AuthController {
...
}
'개발 공부 > NestJS' 카테고리의 다른 글
인 메모리 데이터베이스, Redis (2편) (0) | 2023.03.26 |
---|---|
인 메모리 데이터베이스, Redis (1편) (0) | 2023.03.26 |
인터셉터 Interceptor (0) | 2023.03.19 |
파이프 Pipe (0) | 2023.03.19 |
예외 필터 Exception filter (0) | 2023.03.19 |