<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>
// 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);
}