AgentSkillsCN

nestjs

提供 NestJS 框架开发的标准与架构模式。确保以领域为中心的架构设计,合理运用依赖注入与装饰器模式。专注于模块化设计、Provider 与 Service 的使用、中间件与守卫机制、拦截器与管道、自定义装饰器,以及微服务架构的构建与应用。 适用场景:开发 NestJS 应用程序、设计模块结构(@Module)、创建控制器(@Controller)与服务(@Injectable)、实现 REST 或 GraphQL API、配置依赖注入、构建中间件与守卫、创建自定义装饰器、实施身份验证与授权、利用 NestJS 设计微服务,或与 TypeORM/Prisma 集成以实现数据库访问。

SKILL.md
--- frontmatter
name: nestjs
description: |
  Provides NestJS framework development standards and architectural patterns. Ensures domain-centric architecture, proper dependency injection, and decorator pattern utilization. Specializes in modular design, providers and services, middleware and guards, interceptors and pipes, custom decorators, and microservices architecture.
  Use when: developing NestJS applications, designing module structure (@Module), creating controllers (@Controller) and services (@Injectable), implementing REST or GraphQL APIs, configuring dependency injection, building middleware and guards, creating custom decorators, implementing authentication/authorization, designing microservices with NestJS, or integrating with TypeORM/Prisma for database access.

NestJS Development Standards

Module Organization Principles

Domain-Centric Modularization

Organize modules by business domain, not by function.

  • ❌ Bad: controllers/, services/, repositories/
  • ✅ Good: users/, products/, orders/

Single Responsibility Module

Each module is responsible for only one domain.

  • Separate common functionality into common/ or shared/ modules
  • Inter-domain communication must go through Services only

Dependency Injection Rules

Constructor Injection Only

Property injection (@Inject) is forbidden.

typescript
// ✅ Good
constructor(private readonly userService: UserService) {}

// ❌ Bad
@Inject() userService: UserService;

Provider Registration Location

Providers are registered only in the module where they are used.

  • Minimize global providers
  • Use forRoot/forRootAsync only in AppModule

Decorator Usage Rules

Prioritize Custom Decorators

Abstract repeated decorator combinations into custom decorators.

typescript
// Create custom decorator when combining 3+ decorators
@Auth() // Integrates @UseGuards + @ApiBearerAuth + @CurrentUser

Decorator Order

Arrange in execution order from top to bottom.

  1. Metadata decorators (@ApiTags, @Controller, @Resolver)
  2. Guards/Interceptors (@UseGuards, @UseInterceptors)
  3. Route decorators (@Get, @Post, @Query, @Mutation)
  4. Parameter decorators (@Body, @Param, @Args)

DTO/Entity Rules

DTO is Pure Data Transfer

Business logic is forbidden; only validation is allowed.

typescript
// ✅ Good: Validation only
class CreateUserDto {
  @IsEmail()
  email: string;
}

// ❌ Bad: Contains business logic
class CreateUserDto {
  toEntity(): User {} // Forbidden
}

Separate Entity and DTO

Never return Entity directly; always convert to DTO.

  • Request: CreateInput, UpdateInput (GraphQL) / CreateDto, UpdateDto (REST)
  • Response: Type definition or plain object

Error Handling

Domain-Specific Exception Filter

Each domain has its own Exception Filter.

typescript
@Module({
  providers: [
    {
      provide: APP_FILTER,
      useClass: UserExceptionFilter,
    },
  ],
})

Explicit Error Throwing

Always throw Exception explicitly in all error situations.

  • REST: Use HttpException series
  • GraphQL: Use GraphQLError or custom error
  • Forbid implicit null/undefined returns
  • Error messages should be understandable by users