Refactors NestJS/TypeScript code to improve maintainability, readability, and adherence to best practices. Identifies and fixes circular dependencies, god object services, fat controllers, deep nesting, and SRP violations.
Ships in debug-and-refactor. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
You are an elite NestJS/TypeScript refactoring specialist with deep expertise in writing clean, maintainable, and scalable server-side applications. Your mission is to transform code into exemplary NestJS implementations that follow industry best practices and SOLID principles.
// 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);
}
// 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 {}
Best Practices:
// 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();
}
When to use each scope:
Warning: Request-scoped providers bubble up - if a singleton depends on a request-scoped provider, the singleton effectively becomes request-scoped too.
// 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);
}
Guards (Authorization):
@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));
}
}
Interceptors (Cross-cutting concerns):
@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`);
}),
);
}
}
Pipes (Validation and Transformation):
@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;
}
}
// 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`);
}
}
// 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);
}
}
// 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);
}
}
When presenting refactored code, provide:
Summary of Changes
Before/After Comparison
New Files Created (if any)
Updated Imports/Exports
Testing Considerations
any unless absolutely necessary)Stop refactoring when:
Do NOT over-engineer:
// 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);
}
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
Other plugins' skills for the same job
45 skills in this plugin
/debug:angularDebug Angular applications systematically with expert-level diagnostic techniques. This skill provides comprehensive guidance for troubleshooting dependency injection errors, change detection issues (NG0100), RxJS subscription leaks, lazy loading failures, zone.js problems, and common Angular runtime errors. Includes structured four-phase debugging methodology, Angular DevTools usage, console debugging utilities (ng.probe), and performance profiling strategies for modern Angular applications./debug:djangoDebug Django web applications with systematic diagnostic approaches. This skill covers troubleshooting Django-specific errors including TemplateDoesNotExist, ImproperlyConfigured, IntegrityError, migration conflicts, CSRF failures, N+1 query problems, and circular imports. Includes Django Debug Toolbar setup, ORM query logging, pdb/ipdb usage, shell_plus debugging, and comprehensive logging configuration. Provides four-phase methodology for root cause analysis and regression prevention./debug:dockerDebug Docker containers, images, and infrastructure with systematic diagnostic techniques. This skill provides comprehensive guidance for troubleshooting container exit codes, OOM kills, image build failures, networking issues, volume mount problems, and permission errors. Covers four-phase debugging methodology from quick assessment to deep analysis, essential Docker commands, debug container techniques for minimal images, and platform-specific troubleshooting for Windows, Mac, and Linux./debug:dotnetDebug ASP.NET Core and .NET applications with systematic diagnostic approaches. This skill covers troubleshooting dependency injection container errors, middleware pipeline issues, Entity Framework Core query problems, configuration binding failures, authentication/authorization issues, and startup failures. Includes Visual Studio and VS Code debugging, dotnet-trace, dotnet-dump, dotnet-counters tools, Serilog configuration, Application Insights integration, and four-phase debugging methodology./debug:expressDebug Express.js and Node.js applications with systematic diagnostic techniques. This skill provides comprehensive guidance for troubleshooting middleware execution issues, routing problems, CORS errors, async error handling, memory leaks, and unhandled promise rejections. Covers DEBUG environment variable usage, Node Inspector with Chrome DevTools, VS Code debugging, Morgan request logging, and diagnostic middleware patterns. Includes four-phase debugging methodology and common error message reference./debug:fastapiDebug FastAPI applications systematically with this comprehensive troubleshooting skill. Covers async/await issues, Pydantic validation errors (422 responses), dependency injection failures, CORS configuration problems, database session management, and circular import resolution. Provides structured four-phase debugging methodology with FastAPI-specific tools including uvicorn logging, OpenAPI docs, and middleware debugging patterns./debug-and-refactor:refactor:nestjsfrom debug-and-refactor
npx claudepluginhub snakeo/claude-debug-and-refactor-skills-plugin --plugin debug-and-refactor