Files
nest-template/src/main.ts
fiamanillah e04b14265b feat: initialize NestJS application with global error handling and response standardization
- Added nest-cli.json for NestJS configuration.
- Created package.json with dependencies and scripts for building, testing, and linting.
- Implemented AppController and AppService with a basic "Hello World!" response.
- Introduced ContextModule and ContextService for managing request-scoped context.
- Developed custom decorators for response messages and standardized error handling.
- Created global exception filter to handle various error types and format responses.
- Implemented response standardization interceptor to wrap successful responses.
- Added middleware for generating unique request IDs.
- Established DTOs for API responses and error handling.
- Configured TypeScript settings for the project.
- Set up Jest for testing with end-to-end test cases.
2025-11-18 06:11:40 +06:00

39 lines
1.5 KiB
TypeScript

import { HttpAdapterHost, NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ValidationPipe } from '@nestjs/common';
import { AllExceptionsFilter } from './common/filters/all-exceptions.filter';
import { ContextService } from './common/context/context.service';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
// --- 1. Enable Global Validation Pipe ---
// This is part of Feature #5, but required for the filter to catch
// class-validator errors.
app.useGlobalPipes(
new ValidationPipe({
whitelist: true, // Strip properties not in DTO
transform: true, // Automatically transform payloads to DTO instances
forbidNonWhitelisted: true, // Throw error on non-whitelisted properties
transformOptions: {
enableImplicitConversion: true, // Convert query/path params
},
}),
);
// --- 2. Register Global Exception Filter ---
// We MUST get these dependencies from the app instance
// *after* it has been created.
const httpAdapterHost = app.get(HttpAdapterHost);
const contextService = app.get(ContextService);
app.useGlobalFilters(
new AllExceptionsFilter(httpAdapterHost, contextService),
);
// Note: We are instantiating it here instead of using APP_FILTER
// because the filter *depends* on other providers (HttpAdapterHost)
// that are only available *after* the app is created.
await app.listen(process.env.PORT ?? 3000);
}
bootstrap();