nestjs-expert
构建企业级 NestJS 应用
NestJS 开发者面临复杂的架构挑战,包括依赖注入、模块组织和认证设置。本技能提供 NestJS 模式、测试策略和常见问题解决的专业指导。
下載技能 ZIP
在 Claude 中上傳
前往 設定 → 功能 → 技能 → 上傳技能
開啟並開始使用
測試它
正在使用「nestjs-expert」。 如何修复「无法解析 UserService 依赖」错误?
預期結果:
此错误通常意味着模块中缺少一个提供者。检查:1) 依赖是否在模块的 providers 数组中?2) 如果跨模块使用,是否已导出?3) 检查提供者名称中的拼写错误。如果两个服务之间存在循环依赖,请使用 forwardRef()。
正在使用「nestjs-expert」。 使用 Passport 设置 JWT 认证
預期結果:
1) 安装 @nestjs/passport、@nestjs/jwt、passport、passport-jwt。2) 创建扩展 PassportStrategy(Strategy) 的 JwtStrategy。3) 在 forRoot() 中配置带密钥的 JwtModule。4) 使用 JwtAuthGuard 创建 AuthGuard。5) 在受保护路由上应用 @UseGuards(AuthGuard)。
安全審計
安全All static findings are false positives. The skill provides documentation and CLI command references for Nest.js development - standard patterns for a framework expert skill. No malicious code execution, credential exfiltration, or cryptographic vulnerabilities exist. External commands are documented CLI tools, network URLs are official documentation links, and environment variable references are legitimate configuration guidance.
嚴重問題 (1)
高風險問題 (2)
低風險問題 (2)
品質評分
你能建構什麼
修复 NestJS 依赖注入错误
通过识别缺失的提供者、错误的模块导出或循环依赖来解析「无法解析依赖」错误。
设置 JWT 认证
使用 JWT 配置 Passport.js 策略、正确的守卫实现和令牌验证。
编写全面测试
使用 mocks 创建单元测试、使用测试数据库创建集成测试、使用 Supertest 创建端到端测试。
試試這些提示
我在 NestJS 应用程序中的 UserService 和 AuthService 之间遇到「检测到循环依赖」错误。请帮助我使用 forwardRef() 或重构架构来解决此问题。
我需要使用 Passport.js 在我的 NestJS 应用中实现 JWT 认证。向我展示如何配置 JwtStrategy、JwtModule 和 AuthGuard。
帮助我使用 Jest 为 UserService 编写单元测试。它依赖于 UserRepository 和 JwtService。向我展示正确的 mocking。
我有一个与 Posts 和 Comments 关联的 User 实体。向我展示如何在 NestJS 中使用 TypeORM 配置 OneToMany 和 ManyToOne 关系。
最佳實務
- 使用依赖注入保持服务松散耦合和可测试
- 遵循模块化架构:每个域实体一个功能模块
- 始终使用 class-validator 和 class-transformer 管道验证 DTO
避免
- 避免直接将模块导入控制器 - 使用服务注入
- 不要跳过验证管道 - 始终验证传入的请求数据
- 避免模块之间紧密耦合 - 使用导出和接口