bundles/backend/skills/nestjs-expert/SKILL.md
NestJS architecture, modules, DI, guards, interceptors, pipes, MongoDB/Mongoose integration, auth, and production patterns. Use when building NestJS APIs, designing module structure, implementing auth, handling errors, writing DTOs, or debugging NestJS-specific issues.
npx skillsauth add shipshitdev/library nestjs-expertInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Stack: NestJS + MongoDB/Mongoose + TypeScript strict mode.
Every feature is a self-contained module. No cross-module direct imports — use exported providers.
src/
├── app.module.ts # Root — imports feature modules only
├── common/ # Shared guards, pipes, filters, interceptors
│ ├── filters/
│ ├── guards/
│ ├── interceptors/
│ └── pipes/
├── config/ # ConfigModule setup
└── {feature}/
├── {feature}.module.ts
├── {feature}.controller.ts
├── {feature}.service.ts
├── {feature}.repository.ts # optional, wraps Mongoose model
├── dto/
│ ├── create-{feature}.dto.ts
│ └── update-{feature}.dto.ts
├── schemas/
│ └── {feature}.schema.ts
└── {feature}.types.ts
@Injectable({ scope: Scope.DEFAULT }) (singleton) unless you need request-scopedforwardRef only as last resortTest.createTestingModule — always mock external services@Controller('resources')
@UseGuards(JwtAuthGuard)
@UseInterceptors(ResponseTransformInterceptor)
export class ResourceController {
constructor(private readonly resourceService: ResourceService) {}
@Get()
async findAll(@Query() query: PaginationQueryDto) {
return this.resourceService.findAll(query);
}
@Post()
@HttpCode(HttpStatus.CREATED)
async create(@Body() dto: CreateResourceDto, @CurrentUser() user: UserDocument) {
return this.resourceService.create(dto, user._id);
}
}
Rules:
@Body(), @Query(), @Param() with DTOs@CurrentUser() custom decorator, never @Req()import { IsString, IsEnum, IsOptional, MinLength, MaxLength } from 'class-validator';
import { Transform } from 'class-transformer';
export class CreateResourceDto {
@IsString()
@MinLength(1)
@MaxLength(255)
name: string;
@IsEnum(ResourceStatus)
status: ResourceStatus;
@IsOptional()
@IsString()
@Transform(({ value }) => value?.trim())
description?: string;
}
Global validation pipe in main.ts:
app.useGlobalPipes(new ValidationPipe({
whitelist: true, // strip unknown props
forbidNonWhitelisted: true,
transform: true, // auto-transform primitives
transformOptions: { enableImplicitConversion: true },
}));
// schema
@Schema({ timestamps: true, versionKey: false })
export class Resource {
@Prop({ required: true, index: true })
name: string;
@Prop({ type: Types.ObjectId, ref: 'User', required: true, index: true })
userId: Types.ObjectId;
@Prop({ enum: ResourceStatus, default: ResourceStatus.ACTIVE })
status: ResourceStatus;
}
export const ResourceSchema = SchemaFactory.createForClass(Resource);
export type ResourceDocument = Resource & Document;
// service
@Injectable()
export class ResourceService {
constructor(
@InjectModel(Resource.name) private readonly model: Model<ResourceDocument>,
) {}
async findAll(userId: Types.ObjectId, query: PaginationQueryDto) {
const { page = 1, limit = 20 } = query;
return this.model
.find({ userId, deletedAt: null })
.sort({ createdAt: -1 })
.skip((page - 1) * limit)
.limit(limit)
.lean()
.exec();
}
}
Rules:
.lean() for read queries (plain objects, ~30% faster).exec() to get a real PromiseTypes.ObjectId not string for references in service layerdeletedAt: Date | null, never hard delete user data// JWT strategy
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(configService: ConfigService) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: configService.get<string>('JWT_SECRET'),
ignoreExpiration: false,
});
}
async validate(payload: JwtPayload): Promise<UserDocument> {
// return value is injected as req.user
return { _id: payload.sub, email: payload.email };
}
}
// custom decorator
export const CurrentUser = createParamDecorator(
(data: unknown, ctx: ExecutionContext) => ctx.switchToHttp().getRequest().user,
);
@Injectable()
export class ResourceOwnerGuard implements CanActivate {
constructor(private readonly resourceService: ResourceService) {}
async canActivate(context: ExecutionContext): Promise<boolean> {
const { user, params } = context.switchToHttp().getRequest();
const resource = await this.resourceService.findById(params.id);
return resource?.userId.equals(user._id) ?? false;
}
}
@Catch()
export class GlobalExceptionFilter implements ExceptionFilter {
private readonly logger = new Logger(GlobalExceptionFilter.name);
catch(exception: unknown, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
if (exception instanceof HttpException) {
return response.status(exception.getStatus()).json({
statusCode: exception.getStatus(),
message: exception.message,
});
}
this.logger.error('Unhandled exception', exception instanceof Error ? exception.stack : exception);
return response.status(500).json({ statusCode: 500, message: 'Internal server error' });
}
}
// config/app.config.ts
export default registerAs('app', () => ({
port: parseInt(process.env.PORT ?? '3000', 10),
jwtSecret: process.env.JWT_SECRET,
mongoUri: process.env.MONGO_URI,
}));
// access in service
constructor(private config: ConfigService) {}
const port = this.config.get<number>('app.port');
Never use process.env directly outside config files.
lean() on all read queriesfind() filter or sort(){ userId: 1, createdAt: -1 }select() to project only needed fields on large documents@nestjs/cache-manager for expensive reads| Wrong | Right |
|-------|-------|
| Business logic in controller | Move to service |
| any type anywhere | Define interface/DTO |
| console.log | new Logger(ClassName.name) |
| req.user directly | @CurrentUser() decorator |
| Hard-coding env vars | ConfigService |
| .find() without .lean() on reads | Always .lean().exec() |
| string for ObjectId refs | Types.ObjectId |
nestjs-queue-architect — BullMQ async job patternsmongodb-migration-expert — schema migrationserror-handling-expert — global error strategydevelopment
TypeScript refactoring and modernization guidelines from a principal specialist perspective. This skill should be used when refactoring, reviewing, or modernizing TypeScript code to ensure type safety, compiler performance, and idiomatic patterns. Triggers on tasks involving TypeScript type architecture, narrowing, generics, error handling, or migration to modern TypeScript features.
tools
Resolves TypeScript and JavaScript problems across type-level programming, performance, monorepo management, migration, and modern tooling. Invoke when diagnosing "type instantiation excessively deep" errors, migrating JS to TS, configuring strict tsconfig, debugging module resolution, or choosing between Biome/ESLint/Turborepo/Nx.
tools
Turborepo monorepo build system guidance. Triggers on: `turbo.json`, task pipelines, `dependsOn`, caching, remote cache, the `turbo` CLI, `--filter`, `--affected`, CI optimization, environment variables, internal packages, monorepo structure, and package boundaries. Use when the user configures tasks or workflows, creates packages, sets up a monorepo, shares code between apps, runs changed packages, debugs cache behavior, or works in an `apps/` plus `packages/` workspace.
tools
Provides Tailwind CSS v4 performance optimization and best practices guidelines. Triggers when writing, reviewing, or refactoring Tailwind CSS v4 code; when working with Tailwind configuration, @theme directive, utility classes, responsive design, dark mode, container queries, or CSS generation optimization.