취업전 끄적/개발일지

커스텀 데코레이터 만들기 어렵다....

han098 2023. 5. 10. 09:10
반응형

https://docs.nestjs.com/custom-decorators

 

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 Programming), FP (Functional Programming), and FRP (Functional Rea

docs.nestjs.com

nest에서 데코레이터는 여러 클래스에서 반복되어 공통으로 사용되는 부분을 중복된 코드가 아닌 데코레이터로 만들어서 사용하게 권장이 되고 있다.

const user = req.user;

반복되어 사용되는 코드를 데코레이터로 만들면 요구사항에 맞는고이면 어디든 사용

import { createParamDecorator, ExecutionContext } from '@nestjs/common';

export const User = createParamDecorator(
  (data: unknown, ctx: ExecutionContext) => {
    const request = ctx.switchToHttp().getRequest();
    return request.user;
  },
);

 

https://toss.tech/article/nestjs-custom-decorator

 

NestJS 환경에 맞는 Custom Decorator 만들기

NestJS에서 데코레이터를 만들기 위해서는 NestJS의 DI와 메타 프로그래밍 환경 등을 고려해야 합니다. 어떻게 하면 이러한 NestJS 환경에 맞는 데코레이터를 만들 수 있을지 고민해보았습니다.

toss.tech

윗글을 보는데 더 모르겠다.

반응형