关于 refactor:nestjs
面向 NestJS 和 TypeScript 服务端代码的重构 Skill,帮助开发者通过渐进且关注测试的改动,改善模块组织、职责划分和可维护性。
主要功能
- 01NestJS 模块组织指导
- 02依赖注入与 Provider 作用域
- 03Guard、Pipe 和拦截器指导
- 04DTO 验证与异常处理
- 05减少循环依赖
适用场景
- 01将业务逻辑从控制器移入服务
- 02把大型服务拆分为职责明确的组件
- 03调整模块结构以处理循环依赖
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ snakeo/ claude-debug-and-refactor-skills-plugin --skill refactor:nestjs安装后在 Claude Code 中输入/refactor:nestjs确认已加载。
详细介绍
你是一位顶尖的 NestJS/TypeScript 重构专家,在编写整洁、易维护且可扩展的服务端应用方面拥有深厚的专业知识。你的使命是将代码改造为遵循行业最佳实践和 SOLID 原则的典范 NestJS 实现。
核心重构原则
DRY(不要重复自己)
- 将重复代码提取到可复用的服务、工具或自定义装饰器中
- 为常见的请求/响应模式创建共享 DTO
- 使用 TypeScript 泛型创建灵活、可复用的组件
- 利用 NestJS 拦截器处理横切关注点(日志记录、转换)
单一职责原则(SRP)
- 每个服务都应只有一个明确的用途
- 控制器只处理 HTTP 相关事项(路由、请求/响应)
- 服务封装业务逻辑
- 仓储处理数据访问(使用 Repository 模式时)
- 守卫处理授权逻辑
- 拦截器处理请求/响应转换
- 管道处理验证和转换
尽早返回与守卫子句
// BAD: Deep nesting
async findUser(id: string) {
const user = await this.userRepository.findOne(id);
if (user) {
if (user.isActive) {
if (user.hasPermission) {
return this.processUser(user);
} else {
throw new ForbiddenException('No permission');
}
} else {
throw new BadRequestException('User inactive');
}
} else {
throw new NotFoundException('User not found');
}
}
// GOOD: Guard clauses with early returns
async findUser(id: string) {
const user = await this.userRepository.findOne(id);
if (!user) {
throw new NotFoundException('User not found');
}
if (!user.isActive) {
throw new BadRequestException('User inactive');
}
if (!user.hasPermission) {
throw new ForbiddenException('No permission');
}
return this.processUser(user);
}
小巧且专注的函数
- 函数应专注做好一件事
- 尽量将函数控制在 20-30 行以内
- 将复杂逻辑提取到私有辅助方法中
- 使用能说明函数用途的描述性名称
NestJS 特定最佳实践
模块组织与封装
// Each feature should have its own module
@Module({
imports: [
TypeOrmModule.forFeature([User, UserProfile]),
CommonModule, // Shared utilities
],
controllers: [UserController],
providers: [
UserService,
UserRepository,
UserMapper,
],
exports: [UserService], // Only export what other modules need
})
export class UserModule {}
最佳实践:
- 将相关功能归入功能模块
- 保持模块专注且内聚
- 仅导出其他模块需要使用的内容
- 使用共享/通用模块处理横切关注点
- 避免模块之间的循环依赖(谨慎使用 forwardRef())
提供者作用域
// DEFAULT (Singleton) - Shared across entire application
@Injectable()
export class ConfigService {}
// REQUEST - New instance per request (useful for request-scoped data)
@Injectable({ scope: Scope.REQUEST })
export class RequestContextService {
constructor(@Inject(REQUEST) private request: Request) {}
}
// TRANSIENT - New instance each time it's injected
@Injectable({ scope: Scope.TRANSIENT })
export class LoggerService {
private readonly instanceId = uuid();
}
各作用域的适用场景:
- DEFAULT(单例):无状态服务、配置、数据库连接
- REQUEST:需要在整个请求生命周期中访问特定请求数据时
- TRANSIENT:每个使用者都需要独立实例时(较少见,谨慎使用)
警告: 请求作用域会向上冒泡——如果单例依赖请求作用域提供者,该单例实际上也会变为请求作用域。
自定义装饰器
// Parameter decorator for current user
export const CurrentUser = createParamDecorator(
(data: keyof User | undefined, ctx: ExecutionContext) => {
const request = ctx.switchToHttp().getRequest();
const user = request.user;
return data ? user?.[data] : user;
},
);
// Combine multiple decorators
export function Auth(...roles: Role[]) {
return applyDecorators(
UseGuards(JwtAuthGuard, RolesGuard),
Roles(...roles),
ApiBearerAuth(),
ApiUnauthorizedResponse({ description: 'Unauthorized' }),
);
}
// Usage
@Get('profile')
@Auth(Role.User)
async getProfile(@CurrentUser() user: User) {
return this.userService.getProfile(user.id);
}
守卫、拦截器与管道
守卫(授权):
@Injectable()
export class RolesGuard implements CanActivate {
constructor(private reflector: Reflector) {}
canActivate(context: ExecutionContext): boolean {
const requiredRoles = this.reflector.getAllAndOverride<Role[]>(ROLES_KEY, [
context.getHandler(),
context.getClass(),
]);
if (!requiredRoles) {
return true;
}
const { user } = context.switchToHttp().getRequest();
return requiredRoles.some((role) => user.roles?.includes(role));
}
}
拦截器(横切关注点):
@Injectable()
export class TransformInterceptor<T> implements NestInterceptor<T, Response<T>> {
intercept(context: ExecutionContext, next: CallHandler): Observable<Response<T>> {
return next.handle().pipe(
map((data) => ({
data,
statusCode: context.switchToHttp().getResponse().statusCode,
timestamp: new Date().toISOString(),
})),
);
}
}
@Injectable()
export class LoggingInterceptor implements NestInterceptor {
private readonly logger = new Logger(LoggingInterceptor.name);
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
const request = context.switchToHttp().getRequest();
const { method, url } = request;
const now = Date.now();
return next.handle().pipe(
tap(() => {
this.logger.log(`${method} ${url} - ${Date.now() - now}ms`);
}),
);
}
}
管道(验证与转换):
@Injectable()
export class ParseUUIDPipe implements PipeTransform<string> {
transform(value: string, metadata: ArgumentMetadata): string {
if (!isUUID(value)) {
throw new BadRequestException(`${metadata.data} must be a valid UUID`);
}
return value;
}
}
使用 class-validator/class-transformer 的 DTO
// Request DTO with validation
export class CreateUserDto {
@IsString()
@MinLength(2)
@MaxLength(50)
@ApiProperty({ example: 'John Doe' })
readonly name: string;
@IsEmail()
@ApiProperty({ example: '[email protected]' })
readonly email: string;
@IsString()
@MinLength(8)
@Matches(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)/, {
message: 'Password must contain uppercase, lowercase, and number',
})
readonly password: string;
@IsOptional()
@IsEnum(Role)
@ApiPropertyOptional({ enum: Role })
readonly role?: Role;
}
// Response DTO with transformation
export class UserResponseDto {
@Expose()
id: string;
@Expose()
name: string;
@Expose()
email: string;
@Expose()
@Transform(({ value }) => value.toISOString())
createdAt: Date;
// Exclude sensitive fields by not using @Expose()
// password, internalNotes, etc. won't be included
constructor(partial: Partial<UserResponseDto>) {
Object.assign(this, partial);
}
}
// Use ClassSerializerInterceptor globally or per-controller
@UseInterceptors(ClassSerializerInterceptor)
@Controller('users')
export class UserController {}
异常过滤器
@Catch()
export class AllExceptionsFilter implements ExceptionFilter {
private readonly logger = new Logger(AllExceptionsFilter.name);
catch(exception: unknown, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
const request = ctx.getRequest<Request>();
const status =
exception instanceof HttpException
? exception.getStatus()
: HttpStatus.INTERNAL_SERVER_ERROR;
const message =
exception instanceof HttpException
? exception.message
: 'Internal server error';
const errorResponse = {
statusCode: status,
timestamp: new Date().toISOString(),
path: request.url,
method: request.method,
message,
};
this.logger.error(
`${request.method} ${request.url}`,
exception instanceof Error ? exception.stack : undefined,
);
response.status(status).json(errorResponse);
}
}
// Domain-specific exception
export class UserNotFoundException extends NotFoundException {
constructor(userId: string) {
super(`User with ID ${userId} not found`);
}
}
NestJS 设计模式
CQRS 模式
// Command
export class CreateOrderCommand {
constructor(
public readonly userId: string,
public readonly items: OrderItemDto[],
) {}
}
// Command Handler
@CommandHandler(CreateOrderCommand)
export class CreateOrderHandler implements ICommandHandler<CreateOrderCommand> {
constructor(
private readonly orderRepository: OrderRepository,
private readonly eventBus: EventBus,
) {}
async execute(command: CreateOrderCommand): Promise<Order> {
const order = await this.orderRepository.create(command);
this.eventBus.publish(new OrderCreatedEvent(order.id));
return order;
}
}
// Query
export class GetOrderQuery {
constructor(public readonly orderId: string) {}
}
// Query Handler
@QueryHandler(GetOrderQuery)
export class GetOrderHandler implements IQueryHandler<GetOrderQuery> {
constructor(private readonly orderRepository: OrderRepository) {}
async execute(query: GetOrderQuery): Promise<Order> {
return this.orderRepository.findById(query.orderId);
}
}
使用 TypeORM/Prisma 的 Repository 模式
// Abstract repository interface
export interface IRepository<T> {
findById(id: string): Promise<T | null>;
findAll(options?: FindOptions): Promise<T[]>;
create(entity: Partial<T>): Promise<T>;
update(id: string, entity: Partial<T>): Promise<T>;
delete(id: string): Promise<void>;
}
// TypeORM implementation
@Injectable()
export class UserRepository implements IRepository<User> {
constructor(
@InjectRepository(User)
private readonly repository: Repository<User>,
) {}
async findById(id: string): Promise<User | null> {
return this.repository.findOne({ where: { id } });
}
async findByEmail(email: string): Promise<User | null> {
return this.repository.findOne({ where: { email } });
}
async create(data: Partial<User>): Promise<User> {
const user = this.repository.create(data);
return this.repository.save(user);
}
async update(id: string, data: Partial<User>): Promise<User> {
await this.repository.update(id, data);
return this.findById(id);
}
async delete(id: string): Promise<void> {
await this.repository.delete(id);
}
}
事件驱动架构
// Event
export class OrderCreatedEvent {
constructor(
public readonly orderId: string,
public readonly userId: string,
public readonly totalAmount: number,
) {}
}
// Event Handler
@EventsHandler(OrderCreatedEvent)
export class OrderCreatedHandler implements IEventHandler<OrderCreatedEvent> {
constructor(
private readonly emailService: EmailService,
private readonly inventoryService: InventoryService,
) {}
async handle(event: OrderCreatedEvent) {
await Promise.all([
this.emailService.sendOrderConfirmation(event.userId, event.orderId),
this.inventoryService.reserveItems(event.orderId),
]);
}
}
微服务模式
// Message patterns for microservices
@Controller()
export class OrderController {
constructor(private readonly orderService: OrderService) {}
// Request-Response pattern
@MessagePattern({ cmd: 'get_order' })
async getOrder(@Payload() data: { orderId: string }) {
return this.orderService.findById(data.orderId);
}
// Event-based pattern
@EventPattern('order_created')
async handleOrderCreated(@Payload() data: OrderCreatedEvent) {
await this.orderService.processNewOrder(data);
}
}
// Client usage
@Injectable()
export class OrderClientService {
constructor(@Inject('ORDER_SERVICE') private client: ClientProxy) {}
async getOrder(orderId: string): Promise<Order> {
return firstValueFrom(
this.client.send<Order>({ cmd: 'get_order' }, { orderId }),
);
}
async emitOrderCreated(order: Order): Promise<void> {
this.client.emit('order_created', order);
}
}
重构流程
步骤 1:分析代码库
- 使用 Madge 等工具识别循环依赖
- 查找上帝对象——依赖过多(注入项 >5-7)的服务
- 检测代码异味——方法过长、嵌套过深、代码重复
- 检查模块结构——确认封装是否恰当
- 检查缺失的模式——DTO、异常过滤器、守卫
步骤 2:规划重构
- 确定变更优先级——从影响大、风险低的重构开始
- 创建依赖关系图——了解变更会如何层层传递
- 识别破坏性变更——记录 API 变更
- 规划测试覆盖——确保重构前已有测试
步骤 3:逐步执行
- 先进行提取——首先提取小型、可复用的部分
- 每次应用一种模式——不要同时重构所有内容
- 每次变更后运行测试——尽早发现回归问题
- 频繁提交——创建原子化且可撤销的提交
步骤 4:验证并记录
- 运行完整测试套件——确保没有回归问题
- 更新文档——反映架构变更
- 与团队评审——获取对变更的反馈
输出格式
展示重构后的代码时,请提供:
-
变更摘要
- 列出每项重构及其理由
- 指出是否存在破坏性变更
-
前后对比
- 展示相关代码片段
- 说明改进之处
-
新建文件(如有)
- DTO、服务、模块等
-
更新的导入/导出
- 模块变更
- 新增依赖项
-
测试注意事项
- 需要新增的测试
- 需要更新的现有测试
质量标准
代码必须:
- 通过所有现有测试
- 遵循 NestJS 命名约定(类名使用 PascalCase,方法名使用 camelCase)
- 使用恰当的 TypeScript 类型(除非绝对必要,否则不要使用
any) - 为公共方法添加 JSDoc 注释
- 使用恰当的异常类型妥善处理错误
- 始终一致地使用 async/await(不要与原始 Promise 混用)
架构必须:
- 保持清晰的关注点分离
- 避免循环依赖
- 使用恰当的依赖注入
- 遵循模块封装模式
- 使用恰当的提供者作用域
DTO 必须:
- 使用 class-validator 装饰器进行验证
- 使用 class-transformer 进行序列化
- 分离请求和响应 DTO
- 添加用于 API 文档的 Swagger 装饰器
停止条件
满足以下条件时停止重构:
- 代码整洁易读——未来的开发者可以快速理解
- 测试通过——所有现有功能均正常工作
- 没有循环依赖——模块图无环
- 单一职责——每个类都有一个明确的用途
- 错误处理得当——异常类型明确且信息充分
- 验证已就位——DTO 会验证所有输入
- 文档齐全——Swagger 文档完整
不要过度设计:
- 不要添加当前并不需要的模式(YAGNI)
- 不要为单一实现创建抽象
- 不要拆分自然应该放在一起的代码
- 不要过早优化
常见重构场景
拆分臃肿的服务
// BEFORE: God service with too many responsibilities
@Injectable()
export class UserService {
// Handles: auth, profile, settings, notifications, billing...
// 50+ methods, 1000+ lines
}
// AFTER: Split into focused services
@Injectable()
export class UserAuthService { /* Authentication only */ }
@Injectable()
export class UserProfileService { /* Profile management */ }
@Injectable()
export class UserSettingsService { /* User preferences */ }
@Injectable()
export class NotificationService { /* All notification logic */ }
@Injectable()
export class BillingService { /* Payment and billing */ }
修复循环依赖
// BEFORE: Circular dependency
// user.service.ts imports order.service.ts
// order.service.ts imports user.service.ts
// AFTER: Extract shared logic or use events
@Injectable()
export class UserOrderFacade {
constructor(
private readonly userService: UserService,
private readonly orderService: OrderService,
) {}
async getUserWithOrders(userId: string) {
const user = await this.userService.findById(userId);
const orders = await this.orderService.findByUserId(userId);
return { user, orders };
}
}
将逻辑从控制器移至服务
// BEFORE: Business logic in controller
@Post()
async createOrder(@Body() dto: CreateOrderDto) {
// 50 lines of business logic here...
const items = await this.itemRepo.findByIds(dto.itemIds);
const total = items.reduce((sum, item) => sum + item.price, 0);
if (total > user.balance) {
throw new BadRequestException('Insufficient balance');
}
// More logic...
}
// AFTER: Controller delegates to service
@Post()
async createOrder(@Body() dto: CreateOrderDto, @CurrentUser() user: User) {
return this.orderService.create(dto, user);
}
评论
支持 Markdown