NestJS patterns -- modules, DI, exception filters, validation pipes, guards, interceptors, testing, config
98
89%
Does it follow best practices?
Impact
100%
1.36xAverage score across 12 eval scenarios
Passed
No known issues
A SaaS company has a UsersService for their NestJS backend that handles user registration and look-up. The service is currently untestable because it reads process.env directly and its dependency on an EmailService is hard-coded with new.
The team lead has asked you to:
UsersService to use proper NestJS dependency injection and configurationUsersController with POST /users/register and GET /users/:id endpointsFollow NestJS best practices throughout.
Produce files in users-api/src/:
main.ts — bootstrap with proper global configurationapp.module.ts — root moduleusers/users.controller.ts — controller with endpointsusers/users.service.ts — the refactored serviceusers/users.service.spec.ts — unit testsusers/users.module.ts — feature moduleusers/dto/register-user.dto.ts — DTO for registration (email: non-empty string, name: non-empty string)common/filters/ — error handlingThe following files are provided as inputs. Extract them before beginning.
=============== FILE: inputs/users.service.original.ts =============== import { Injectable } from '@nestjs/common'; import { EmailService } from './email.service';
@Injectable() export class UsersService { private emailService = new EmailService();
async register(email: string, name: string) { const maxAttempts = parseInt(process.env.MAX_LOGIN_ATTEMPTS || '5'); const templateId = process.env.WELCOME_EMAIL_TEMPLATE_ID || 'default-template';
const user = { id: Date.now().toString(), email, name, loginAttempts: 0, maxAttempts };
await this.emailService.send(templateId, email, { name });
return user;}
async findById(id: string) { // Stub: return a placeholder user return { id, email: 'user@example.com', name: 'Test User' }; } }
=============== FILE: inputs/email.service.ts =============== import { Injectable } from '@nestjs/common';
@Injectable()
export class EmailService {
async send(templateId: string, to: string, variables: Record<string, string>): Promise<void> {
// Stub: pretend email was sent
console.log(Sending template ${templateId} to ${to}, variables);
}
}
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
skills
nestjs-best-practices
verifiers