Nestjs expert

From findscripter/everything-skills · View interactive version

Nestjs expert is an AI agent skill from the GitHub repository findscripter/everything-skills. 当用 NestJS 构建或排障企业级 Node.js 后端时使用;做模块/依赖注入设计、守卫拦截器管道、TypeORM/Mongoose 集成、Passport+JWT 鉴权、Jest/Supertest 测试与常见报错诊断并产出生产级代码;不适用于纯 TS 类型、纯 SQL 调优、前端或非 NestJS 框架。触发词:NestJS、依赖注入、forwardRef、Guard、Interceptor、TypeORM、Passport JWT

How to use this skill

Skill file

何时使用

适用:

不该用(负边界):

步骤

  1. 若有更专精的领域更合适,先建议切换并停止(见上「负边界」)。
  2. 先用内部工具探测项目(Read / Grep / Glob):确认 NestJS 版本、模块结构、ORM、鉴权与测试栈。
  3. 识别既有架构模式与命名约定,匹配项目风格而非另起一套。
  4. 按 NestJS 最佳实践给出方案。
  5. 按顺序验证:类型检查 → 单元测试 → 集成测试 → e2e 测试。

探测命令(一次性诊断,避免 watch/serve 长驻进程):

test -f nest-cli.json && echo "Nest CLI 项目"
grep -q "@nestjs/core" package.json && echo "已装 NestJS"
grep "@nestjs/core" package.json    # 看版本
grep -q "@nestjs/typeorm" package.json && echo "TypeORM"
grep -q "@nestjs/mongoose" package.json && echo "Mongoose"
grep -q "@prisma/client" package.json && echo "Prisma"
grep -q "@nestjs/passport" package.json && echo "Passport 鉴权"
find src -name "*.module.ts" -type f | head -5

验证命令:

nest info               # 模块依赖概览
npm run build           # 1. 先类型检查
npm run test            # 2. 单元测试
npm run test:e2e        # 3. e2e(按需)

指令

示例

特性模块(Feature Module)模式:

@Module({
  imports: [CommonModule, DatabaseModule],
  controllers: [FeatureController],
  providers: [FeatureService, FeatureRepository],
  exports: [FeatureService], // 导出 Service,供其他模块用
})
export class FeatureModule {}

组合装饰器(鉴权 + 角色):

export const Auth = (...roles: Role[]) =>
  applyDecorators(
    UseGuards(JwtAuthGuard, RolesGuard),
    Roles(...roles),
  );

单测骨架(Mock 依赖):

beforeEach(async () => {
  const module = await Test.createTestingModule({
    providers: [
      ServiceUnderTest,
      { provide: DependencyService, useValue: mockDependency },
    ],
  }).compile();
  service = module.get<ServiceUnderTest>(ServiceUnderTest);
});

异常过滤器:

@Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter {
  catch(exception: HttpException, host: ArgumentsHost) {
    // 自定义错误处理
  }
}

动态模块 forRoot 模式:

@Module({})
export class ConfigModule {
  static forRoot(options: ConfigOptions): DynamicModule {
    return {
      module: ConfigModule,
      providers: [{ provide: 'CONFIG_OPTIONS', useValue: options }],
    };
  }
}

选型速查:

注意事项

互见


采编自 sickn33/antigravity-awesome-skills(MIT 许可)。

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.