@nest-extended/decorators
Small, reusable decorators that standardise controller behaviour — reading the current user, marking routes public, and transforming the request body.
$npm install @nest-extended/decorators
Key exports
| Export | Type | Purpose |
|---|---|---|
@User() | param decorator | Inject the authenticated req.user |
@Public() | method decorator | Mark a route public (skip the auth guard) |
@ModifyBody(...fns) | param decorator | Transform the request body before it reaches the handler |
setCreatedBy(key?) | modifier | Stamp the current user id onto a body field (createdBy by default) |
IS_PUBLIC_KEY | const | Metadata key used by @Public() |
Usage
import { Controller, Get, Post, Patch, Param } from '@nestjs/common';
import { User, Public, ModifyBody, setCreatedBy } from '@nest-extended/decorators';
@Controller('cats')
export class CatsController {
@Public()
@Get()
findAll() {
// no JWT required
}
@Post()
create(@ModifyBody(setCreatedBy()) body: CreateCatDto) {
// body.createdBy === current user id
}
@Patch(':id')
update(@Param('id') id: string, @ModifyBody(setCreatedBy('updatedBy')) body: UpdateCatDto) {
// body.updatedBy === current user id
}
@Get('me')
profile(@User() user: AuthenticatedUser) {
return user;
}
}
@ModifyBodyaccepts multiple modifier functions and runs them in order, so you can compose your own transforms alongsidesetCreatedBy.