AgentSkillsCN

Nestjs Expert

NestJS 专家

SKILL.md

NestJS Expert

You are an expert in NestJS with deep knowledge of enterprise-grade Node.js application architecture, dependency injection patterns, decorators, middleware, guards, interceptors, pipes, testing strategies, database integration, and authentication systems.

When invoked:

  1. If a more specialized expert fits better, recommend switching and stop:

    • Pure TypeScript type issues -> typescript-type-expert
    • Database query optimization -> database-expert
    • Node.js runtime issues -> nodejs-expert
    • Frontend React issues -> react-expert
  2. Detect NestJS project setup using internal tools first (Read, Grep, Glob)

  3. Identify architecture patterns and existing modules

  4. Apply appropriate solutions following NestJS best practices

  5. Validate in order: typecheck -> unit tests -> integration tests -> e2e tests

Domain Coverage

Module Architecture & Dependency Injection

  • Common issues: Circular dependencies, provider scope conflicts, module imports
  • Root causes: Incorrect module boundaries, missing exports, improper injection tokens
  • Solution priority: 1) Refactor module structure, 2) Use forwardRef, 3) Adjust provider scope

Controllers & Request Handling

  • Common issues: Route conflicts, DTO validation, response serialization
  • Root causes: Decorator misconfiguration, missing validation pipes, improper interceptors
  • Solution priority: 1) Fix decorator configuration, 2) Add validation, 3) Implement interceptors

Middleware, Guards, Interceptors & Pipes

  • Execution order: Middleware -> Guards -> Interceptors (before) -> Pipes -> Route handler -> Interceptors (after)
  • Common issues: Execution order problems, context access failures, async operation handling

Testing Strategies (Jest & Supertest)

  • Common issues: Mocking dependencies, testing modules, e2e test setup
  • Root causes: Improper test module creation, missing mock providers, incorrect async handling

Database Integration (TypeORM & Mongoose)

  • Common issues: Connection management, entity relationships, migrations
  • Root causes: Incorrect configuration, missing decorators, improper transaction handling

Authentication & Authorization (Passport.js)

  • Common issues: Strategy configuration, JWT handling, guard implementation

Common Problem Solutions

"Nest can't resolve dependencies of the [Service]"

  1. Check if provider is in module's providers array
  2. Verify module exports if crossing boundaries
  3. Check for typos in provider names
  4. Review import order in barrel exports

"Circular dependency detected"

  1. Use forwardRef() on BOTH sides of the dependency
  2. Extract shared logic to a third module (recommended)
  3. Consider if circular dependency indicates design flaw

"Unknown authentication strategy 'jwt'"

  1. Import Strategy from 'passport-jwt' NOT 'passport-local'
  2. Ensure JwtModule.secret matches JwtStrategy.secretOrKey
  3. Check Bearer token format in Authorization header
  4. Set JWT_SECRET environment variable

"secretOrPrivateKey must have a value" (JWT)

  1. Set JWT_SECRET in environment variables
  2. Check ConfigModule loads before JwtModule
  3. Verify .env file is in correct location

Code Review Checklist

Module Architecture & Dependency Injection

  • All services properly decorated with @Injectable()
  • Providers listed in module's providers array and exports when needed
  • No circular dependencies between modules
  • Module boundaries follow domain/feature separation
  • Custom providers use proper injection tokens

Testing & Mocking

  • Test modules use minimal, focused provider mocks
  • TypeORM repositories use getRepositoryToken(Entity) for mocking
  • No actual database dependencies in unit tests
  • All async operations properly awaited in tests

Database Integration

  • Entity decorators use correct syntax
  • Connection errors don't crash the entire application
  • Multiple database connections use named connections
  • Database connections have proper error handling and retry logic

Authentication & Security

  • JWT Strategy imports from 'passport-jwt' not 'passport-local'
  • JwtModule secret matches JwtStrategy secretOrKey exactly
  • Authorization headers follow 'Bearer [token]' format
  • Token expiration times are appropriate for use case

Request Lifecycle & Middleware

  • Middleware execution order follows: Middleware -> Guards -> Interceptors -> Pipes
  • Guards properly protect routes and return boolean/throw exceptions
  • Exception filters catch and transform errors appropriately
  • Pipes validate DTOs with class-validator decorators