I do have 2 decorators: @CurrentUser()
and @MinimumRole(Role.manager)
- The
@CurrentUser
takes the user from the context and set it to the decorated parameter. - The
@MinimumRole(Role.manager)
check if the user in the context has the minimum specified role, throw if this is not the case.
The code of the second decorator is as follow:
export const MinimumRole = createParamDecorator(
(minimumRole: Role, executionContext: ExecutionContext) => {
const user = getCurrentUserFromContext(executionContext);
assertHasMinimumRole(user.role, minimumRole); // throw if the condition is not met
return undefined;
});
Now, I would like the @MinimumRole
to work like a transparent decorator i.e. without changing the decorated value. Because right now if I do so:
@CurrentUser() @MinimumRole(Role.manager) user: User;
it works (@CurrentUser is last to be called and therefore last to return the value)
while
@MinimumRole(Role.manager) @CurrentUser() user: User
; will set undefined
to the decorated value (as my decorator returns undefined) and therefore override the users value.
I do see 2 solutions but I need some help:
Either there is a way to get the target's value in the
createParamDecorator
, in which case I would be able to return this value.Moving the decorator so that it decorates the whole method instead of a parameter's only. But I would then need to know how to get the context from a method decorator in nestjs.