Communitygithub.com

NestJS 패턴

모듈, 컨트롤러, 프로바이더, DTO 검증, 가드, 인터셉터, 설정 및 프로덕션급 TypeScript 백엔드를 위한 NestJS 아키텍처 패턴입니다. NestJS 백엔드를 구축하거나 리뷰할 때 사용하세요.

NestJS 패턴란 무엇인가요?

이 스킬은 프로덕션 환경에 적합한 NestJS 백엔드 개발을 위한 포괄적인 아키텍처 템플릿 및 모범 사례를 제공합니다. 도메인 로직을 기능 모듈 내에 유지하고, 공통 관심사(필터, 가드, 인터셉터)를 `common/` 디렉토리로 분리하며, DTO를 소유 모듈 근처에 배치하는 프로젝트 구조를 제안합니다. 주요 패턴에는 글로벌 유효성 검사 파이프 설정, thin 컨트롤러와 인젝터블 서비스 분리, `class-validator`를 사용한 DTO 검증, JWT 및 역할 기반 접근 제어를 위한 가드 사용, 일관된 오류 응답을 위한 예외 필터, 환경 변수 검증 등이 포함됩니다. 또한 Prisma나 TypeORM 같은 ORM과의 통합, 단위 테스트 및 HTTP 엔드포인트 테스트 방법, 프로덕션 환경을 위한 구조적 로깅, 비율 제한, 헬스 체크 등의 기본 설정도 다룹니다. 이 스킬은 Claude Code, Cursor, Codex 등 다양한 AI 에이전트 플랫폼에서 사용할 수 있도록 설계되었으며, NestJS 프로젝트를 처음부터 구축하거나 기존 코드를 리뷰할 때 일관된 고품질 아키텍처를 보장합니다.

지원 대상~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/affaan-m/everything-claude-code/tree/main/skills/nestjs-patterns

즐겨 사용하는 AI에게 물어보기

이 에이전트 스킬이 미리 로드된 새 채팅을 엽니다.

문서

NestJS 개발 패턴

모듈식 TypeScript 백엔드를 위한 프로덕션급 NestJS 패턴입니다.

활성화 시기

  • NestJS API 또는 서비스 구축
  • 모듈, 컨트롤러 및 프로바이더 구조화
  • DTO 검증, 가드, 인터셉터 또는 예외 필터 추가
  • 환경 인식 설정 및 데이터베이스 통합 구성
  • NestJS 단위 또는 HTTP 엔드포인트 테스트

프로젝트 구조

src/
├── app.module.ts
├── main.ts
├── common/
│   ├── filters/
│   ├── guards/
│   ├── interceptors/
│   └── pipes/
├── config/
│   ├── configuration.ts
│   └── validation.ts
├── modules/
│   ├── auth/
│   │   ├── auth.controller.ts
│   │   ├── auth.module.ts
│   │   ├── auth.service.ts
│   │   ├── dto/
│   │   ├── guards/
│   │   └── strategies/
│   └── users/
│       ├── dto/
│       ├── entities/
│       ├── users.controller.ts
│       ├── users.module.ts
│       └── users.service.ts
└── prisma/ 또는 database/
  • 도메인 코드는 기능 모듈 내에 유지하세요.
  • 크로스커팅 필터, 데코레이터, 가드, 인터셉터는 common/에 넣으세요.
  • DTO는 해당 모듈 근처에 두세요.

부트스트랩 및 글로벌 검증

async function bootstrap() {
  const app = await NestFactory.create(AppModule, { bufferLogs: true });

  app.useGlobalPipes(
    new ValidationPipe({
      whitelist: true,
      forbidNonWhitelisted: true,
      transform: true,
      transformOptions: { enableImplicitConversion: true },
    }),
  );

  app.useGlobalInterceptors(new ClassSerializerInterceptor(app.get(Reflector)));
  app.useGlobalFilters(new HttpExceptionFilter());

  await app.listen(process.env.PORT ?? 3000);
}
bootstrap();
  • 공개 API에서는 항상 whitelist와 forbidNonWhitelisted를 활성화하세요.
  • 라우트마다 검증 구성을 반복하는 대신 하나의 글로벌 검증 파이프를 사용하는 것이 좋습니다.

모듈, 컨트롤러 및 프로바이더

@Module({
  controllers: [UsersController],
  providers: [UsersService],
  exports: [UsersService],
})
export class UsersModule {}

@Controller('users')
export class UsersController {
  constructor(private readonly usersService: UsersService) {}

  @Get(':id')
  getById(@Param('id', ParseUUIDPipe) id: string) {
    return this.usersService.getById(id);
  }

  @Post()
  create(@Body() dto: CreateUserDto) {
    return this.usersService.create(dto);
  }
}

@Injectable()
export class UsersService {
  constructor(private readonly usersRepo: UsersRepository) {}

  async create(dto: CreateUserDto) {
    return this.usersRepo.create(dto);
  }
}
  • 컨트롤러는 얇게 유지하세요: HTTP 입력을 파싱하고, 프로바이더를 호출하고, 응답 DTO를 반환합니다.
  • 비즈니스 로직은 인젝터블 서비스에 넣고 컨트롤러에는 넣지 마세요.
  • 다른 모듈이 실제로 필요한 프로바이더만 내보내세요.

DTO 및 검증

export class CreateUserDto {
  @IsEmail()
  email!: string;

  @IsString()
  @Length(2, 80)
  name!: string;

  @IsOptional()
  @IsEnum(UserRole)
  role?: UserRole;
}
  • 모든 요청 DTO는 class-validator로 검증하세요.
  • ORM 엔티티를 직접 반환하는 대신 전용 응답 DTO 또는 시리얼라이저를 사용하세요.
  • 비밀번호 해시, 토큰 또는 감사 컬럼과 같은 내부 필드가 유출되지 않도록 하세요.

인증, 가드 및 요청 컨텍스트

@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('admin')
@Get('admin/report')
getAdminReport(@Req() req: AuthenticatedRequest) {
  return this.reportService.getForUser(req.user.id);
}
  • 인증 전략과 가드는 진정으로 공유되지 않는 한 모듈 로컬로 유지하세요.
  • 가드에서 거친 접근 규칙을 인코딩하고, 서비스에서 리소스별 권한 부여를 수행하세요.
  • 인증된 요청 객체에는 명시적 요청 타입을 선호하세요.

예외 필터 및 오류 형태

@Catch()
export class HttpExceptionFilter implements ExceptionFilter {
  catch(exception: unknown, host: ArgumentsHost) {
    const response = host.switchToHttp().getResponse<Response>();
    const request = host.switchToHttp().getRequest<Request>();

    if (exception instanceof HttpException) {
      return response.status(exception.getStatus()).json({
        path: request.url,
        error: exception.getResponse(),
      });
    }

    return response.status(500).json({
      path: request.url,
      error: 'Internal server error',
    });
  }
}
  • API 전반에 걸쳐 일관된 오류 봉투를 유지하세요.
  • 예상되는 클라이언트 오류에는 프레임워크 예외를 던지고, 예상치 못한 실패는 중앙에서 로깅하고 래핑하세요.

설정 및 환경 변수 검증

ConfigModule.forRoot({
  isGlobal: true,
  load: [configuration],
  validate: validateEnv,
});
  • 환경 변수는 부트 시점에 검증하고, 첫 번째 요청 시 지연 검증하지 마세요.
  • 설정 접근은 타입이 지정된 헬퍼나 설정 서비스 뒤에 유지하세요.
  • 기능 코드 전체에서 분기하지 않고 설정 팩토리에서 개발/스테이징/프로덕션 구성을 분리하세요.

지속성 및 트랜잭션

  • 리포지토리/ORM 코드는 도메인 언어를 사용하는 프로바이더 뒤에 유지하세요.
  • Prisma 또는 TypeORM의 경우, 작업 단위를 소유하는 서비스에서 트랜잭션 워크플로를 격리하세요.
  • 컨트롤러가 다단계 쓰기를 직접 조정하지 못하게 하세요.

테스트

describe('UsersController', () => {
  let app: INestApplication;

  beforeAll(async () => {
    const moduleRef = await Test.createTestingModule({
      imports: [UsersModule],
    }).compile();

    app = moduleRef.createNestApplication();
    app.useGlobalPipes(new ValidationPipe({ whitelist: true, transform: true }));
    await app.init();
  });
});
  • 프로바이더는 모킹된 종속성으로 격리하여 단위 테스트하세요.
  • 가드, 검증 파이프 및 예외 필터에 대한 요청 수준 테스트를 추가하세요.
  • 프로덕션에서 사용하는 것과 동일한 글로벌 파이프/필터를 테스트에서도 재사용하세요.

프로덕션 기본값

  • 구조적 로깅 및 요청 상관관계 ID를 활성화하세요.
  • 환경 변수/구성이 잘못된 경우 부분적으로 부팅하지 말고 종료하세요.
  • 명시적 헬스 체크와 함께 DB/캐시 클라이언트에 대해 비동기 프로바이더 초기화를 선호하세요.
  • 백그라운드 작업 및 이벤트 컨슈머는 HTTP 컨트롤러 내부가 아닌 자체 모듈에 유지하세요.
  • 공개 엔드포인트에 대해 비율 제한, 인증 및 감사 로깅을 명시적으로 설정하세요.

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

accessibility

Design, implement, and audit inclusive digital products using WCAG 2.2 Level AA. Use when building or auditing UI that must meet WCAG 2.2 Level AA, or when reviewing a change for keyboard, contrast, or screen-reader support.

affaan-m/claude-api

Anthropic Claude API patterns for Python and TypeScript. Covers Messages API, streaming, tool use, vision, extended thinking, batches, prompt caching, and Claude Agent SDK. Use when building applications with the Claude API or Anthropic SDKs.

affaan-m/everything-claude-code

End-to-end marketing campaign planning and execution. Covers audience research, positioning, campaign angle definition, landing page copy, email sequences, social posts, ad copy, short-form video scripts, and content calendars. Use as the orchestration layer for multi-channel product launches. Use when planning or executing a multi-channel product launch, or producing landing page, email, social, or ad copy.

affaan-m/everything-claude-code

Development conventions and patterns for everything-claude-code. JavaScript project with conventional commits.

affaan-m/everything-claude-code-conventions

Development conventions and patterns for everything-claude-code. JavaScript project with conventional commits.

affaan-m/frontend-design

Create distinctive, production-grade frontend interfaces with high design quality. Use when the user asks to build web components, pages, or applications and the visual direction matters as much as the code quality.

affaan-m/gget

gget CLI and Python workflow for quick genomic database queries, sequence lookup, BLAST-style searches, enrichment checks, and reproducible bioinformatics evidence logs.

affaan-m/literature-review

Systematic literature-review workflow for academic, biomedical, technical, and scientific topics, including search planning, source screening, synthesis, citation checks, and evidence logging.

affaan-m/motion-ui

Production-ready UI motion system for React/Next.js. Use when implementing animations, transitions, or motion patterns.

affaan-m/project-guidelines-example

Example project-specific skill template based on a real production application.

affaan-m/pubmed-database

Direct PubMed and NCBI E-utilities search workflows for biomedical literature, MeSH queries, PMID lookup, citation retrieval, and API-backed literature monitoring.

affaan-m/scholar-evaluation

Structured scholarly-work evaluation for papers, proposals, literature reviews, methods sections, evidence quality, citation support, and research-writing feedback.

affaan-m/uspto-database

USPTO patent and trademark data workflow for official record lookup, PatentSearch queries, TSDR checks, assignment data, and reproducible IP research logs.

agent-architecture-audit

Full-stack diagnostic for agent and LLM applications. Audits the 12-layer agent stack for wrapper regression, memory pollution, tool discipline failures, hidden repair loops, and rendering corruption. Produces severity-ranked findings with code-first fixes. Essential for developers building agent applications, autonomous loops, or any LLM-powered feature. Use when an agent or LLM feature misbehaves and the failing layer is unknown, or before shipping an agent stack.

agent-eval

Head-to-head comparison of coding agents (Claude Code, Aider, Codex, etc.) on custom tasks with pass rate, cost, time, and consistency metrics. Use when choosing between coding agents, or when a change to an agent setup needs measured pass rate, cost, and time rather than an impression.

agent-harness-construction

Design and optimize AI agent action spaces, tool definitions, and observation formatting for higher completion rates. Use when defining or revising an agent

agentic-engineering

Operate as an agentic engineer using eval-first execution, decomposition, and cost-aware model routing. Use when planning or executing engineering work that agents will carry out end to end.

agentic-os

Build persistent multi-agent operating systems on Claude Code. Covers kernel architecture, specialist agents, slash commands, file-based memory, scheduled automation, and state management without external databases. Use when building a persistent multi-agent system on Claude Code with its own memory, commands, and scheduling.

agent-introspection-debugging

Structured self-debugging workflow for AI agent failures using capture, diagnosis, contained recovery, and introspection reports. Use when an agent run fails and you need a reproducible diagnosis instead of a retry.

agent-payment-x402

Add x402 payment execution to AI agents with per-task budgets, spending controls, and non-custodial wallets. Supports Base through agentwallet-sdk and X Layer through OKX Payments / OKX Agent Payments Protocol. Use when an agent must pay for something itself and needs per-task budgets, spending controls, and a non-custodial wallet.

관련 스킬