개념

<aside> 🍎 Expression which returns a function and can take a target, name and property descriptor as arguments.

</aside>

사용 방법

<aside> 🍎 You apply it by prefixing the decorator with an @ character and placing this at the very top of what you are trying to decorate.

</aside>

사용 목적

<aside> 🍎 Decorators can be defined for either a class, a method or a property.

</aside>

Custom Decorator

// user.decorator.ts

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

export const User = createParamDecorator(
  (data: unknown, ctx: ExecutionContext) => {
    const request = ctx.switchToHttp().getRequest();
    return request.user;
  },
);
@Get()
async findOne(@User() user: UserEntity) {
  console.log(user);
}