AgentSkillsCN

nestjs

借助 NestJS 的模块化 API 架构,结合控制器、服务、守卫、中间件以及依赖注入等模式,轻松构建各类 API 端点、实现服务逻辑、创建守卫与拦截器、处理身份认证与权限控制,或在 NestJS 中高效完成各类后端业务逻辑的开发。

SKILL.md
--- frontmatter
name: nestjs
description: |
  NestJS modular API architecture with controllers, services, guards, middleware, and dependency injection patterns.
  Use when: building API endpoints, implementing services, creating guards/interceptors, handling authentication/authorization, or any backend business logic with NestJS
allowed-tools: Read, Edit, Write, Glob, Grep, Bash

NestJS Skill

NestJS provides a structured, Angular-inspired architecture for Node.js backends. It enforces separation of concerns through modules, controllers, and services with built-in dependency injection. Use decorators for routing, validation, and metadata.

Quick Start

Module Structure

typescript
// src/products/products.module.ts
@Module({
  imports: [TypeOrmModule.forFeature([Product])],
  controllers: [ProductsController],
  providers: [ProductsService],
  exports: [ProductsService],
})
export class ProductsModule {}

Controller Pattern

typescript
// src/products/products.controller.ts
@Controller('products')
export class ProductsController {
  constructor(private readonly productsService: ProductsService) {}

  @Get()
  findAll(@Query() query: FindProductsDto) {
    return this.productsService.findAll(query);
  }

  @Post()
  @UseGuards(JwtAuthGuard)
  create(@Body() dto: CreateProductDto, @Request() req) {
    return this.productsService.create(dto, req.user.id);
  }
}

Service Pattern

typescript
// src/products/products.service.ts
@Injectable()
export class ProductsService {
  constructor(
    @InjectRepository(Product)
    private readonly productRepo: Repository<Product>,
  ) {}

  async findAll(query: FindProductsDto): Promise<Product[]> {
    return this.productRepo.find({ where: query });
  }
}

Key Concepts

ConceptUsageExample
ModuleFeature boundary, DI container@Module({ providers: [...] })
ControllerHTTP routing, request handling@Controller('users')
ServiceBusiness logic, injectable@Injectable()
GuardAuth/authorization checks@UseGuards(AuthGuard)
PipeValidation, transformation@UsePipes(ValidationPipe)
InterceptorResponse transformation, logging@UseInterceptors(LoggingInterceptor)

Common Patterns

Global Validation

typescript
// main.ts
app.useGlobalPipes(new ValidationPipe({
  whitelist: true,
  forbidNonWhitelisted: true,
  transform: true,
}));

Exception Filter

typescript
@Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter {
  catch(exception: HttpException, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse<Response>();
    const status = exception.getStatus();
    
    response.status(status).json({
      statusCode: status,
      message: exception.message,
      timestamp: new Date().toISOString(),
    });
  }
}

See Also

  • routes - Controller routing patterns
  • services - Service layer and DI
  • database - TypeORM/Prisma integration
  • auth - Guards and authentication
  • errors - Exception handling

Related Skills

  • See the typescript skill for strict typing patterns
  • See the prisma skill for database ORM alternative to TypeORM
  • See the postgresql skill for database design
  • See the zod skill for runtime validation
  • See the jest skill for unit testing NestJS services