| name | nestjs |
| description | NestJS architecture including modules, dependency injection, guards, interceptors, and microservices patterns. |
| allowed-tools | Read, Write, Edit, Bash, Glob, Grep |
| graph | {"domains":["domain:web-development"],"specializations":["specialization:web-development"],"skillAreas":["skill-area:backend-api-design","skill-area:middleware-design"],"roles":["role:backend-engineer"],"topics":["topic:dependency-injection","topic:design-patterns"]} |
NestJS Skill
Expert assistance for building enterprise Node.js applications with NestJS.
Capabilities
- Design modular NestJS applications
- Implement dependency injection patterns
- Create guards, pipes, and interceptors
- Build microservices architectures
- Configure Swagger/OpenAPI documentation
- Set up testing with Jest
Usage
Invoke this skill when you need to:
- Build enterprise-grade APIs
- Implement microservices
- Create modular architecture
- Add authentication/authorization
- Generate API documentation
Inputs
| Parameter | Type | Required | Description |
|---|
| moduleName | string | Yes | Module name |
| features | array | No | guards, pipes, interceptors |
| database | string | No | prisma, typeorm, mongoose |
| microservices | boolean | No | Enable microservices |
Architecture Patterns
Module Structure
import { Module } from '@nestjs/common';
import { UsersController } from './users.controller';
import { UsersService } from './users.service';
import { PrismaModule } from '../prisma/prisma.module';
@Module({
imports: [PrismaModule],
controllers: [UsersController],
providers: [UsersService],
exports: [UsersService],
})
export class UsersModule {}
Controller with DTOs
import {
Controller,
Get,
Post,
Put,
Delete,
Body,
Param,
Query,
UseGuards,
HttpCode,
HttpStatus,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth, ApiResponse } from '@nestjs/swagger';
import { UsersService } from './users.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { UserResponseDto } from './dto/user-response.dto';
import { PaginationDto } from '../common/dto/pagination.dto';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { RolesGuard } from '../auth/guards/roles.guard';
{ } ;
{ } ;
()
()
(, )
()
{
() {}
()
({ : })
({ : , : [] })
() {
..(pagination);
}
()
({ : })
({ : , : })
() {
..(id);
}
()
()
({ : })
({ : , : })
() {
..(createUserDto);
}
()
({ : })
() {
..(id, updateUserDto);
}
()
()
(.)
({ : })
() {
..(id);
}
}
{ , , , , } ;
{ } ;
{
({ : })
()
: ;
({ : })
()
: ;
({ : })
()
()
: ;
({ : , : [, ] })
()
([, ])
?: ;
}
Service with Dependency Injection
import { Injectable, NotFoundException, ConflictException } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { PaginationDto } from '../common/dto/pagination.dto';
import * as bcrypt from 'bcryptjs';
@Injectable()
export class UsersService {
constructor(private readonly prisma: PrismaService) {}
async findAll(pagination: PaginationDto) {
const { page = 1, limit = 10, search } = pagination;
const skip = (page - 1) * limit;
const [users, total] = await Promise.all([
...({
: search ? { : { : search, : } } : ,
skip,
: limit,
: {
: ,
: ,
: ,
: ,
: ,
},
}),
...({
: search ? { : { : search, : } } : ,
}),
]);
{
: users,
: {
total,
page,
limit,
: .(total / limit),
},
};
}
() {
user = ...({
: { id },
: {
: ,
: ,
: ,
: ,
: ,
},
});
(!user) {
();
}
user;
}
() {
existing = ...({
: { : dto. },
});
(existing) {
();
}
hashedPassword = bcrypt.(dto., );
...({
: {
...dto,
: hashedPassword,
},
: {
: ,
: ,
: ,
: ,
: ,
},
});
}
() {
.(id);
(dto.) {
dto. = bcrypt.(dto., );
}
...({
: { id },
: dto,
: {
: ,
: ,
: ,
: ,
: ,
},
});
}
() {
.(id);
...({ : { id } });
}
}
Guards and Decorators
import { Injectable, ExecutionContext } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { Reflector } from '@nestjs/core';
import { IS_PUBLIC_KEY } from '../decorators/public.decorator';
@Injectable()
export class JwtAuthGuard extends AuthGuard('jwt') {
constructor(private reflector: Reflector) {
super();
}
canActivate(context: ExecutionContext) {
const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [
context.getHandler(),
context.getClass(),
]);
if (isPublic) {
return true;
}
return super.canActivate(context);
}
}
{ , , } ;
{ } ;
{ } ;
()
{
() {}
(: ): {
requiredRoles = ..<[]>(, [
context.(),
context.(),
]);
(!requiredRoles) {
;
}
{ user } = context.().();
requiredRoles.(user.);
}
}
{ } ;
= ;
= () => (, roles);
{ createParamDecorator, } ;
= (
{
request = ctx.().();
request.;
},
);
Interceptors
import {
Injectable,
NestInterceptor,
ExecutionContext,
CallHandler,
} from '@nestjs/common';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
export interface Response<T> {
data: T;
meta?: Record<string, any>;
}
@Injectable()
export class TransformInterceptor<T> implements NestInterceptor<T, Response<T>> {
intercept(context: ExecutionContext, next: CallHandler): Observable<Response<T>> {
return next.handle().pipe(
map((data) => {
if (data?.data && data?.meta) {
return data;
}
return { data };
}),
);
}
}
Best Practices
- Use modules for feature encapsulation
- Leverage dependency injection
- Implement DTOs for validation
- Use guards for authorization
- Generate OpenAPI documentation
Target Processes
- enterprise-api-development
- microservices-architecture
- backend-development
- nestjs-application