import "reflect-metadata"; import { ValidationPipe, type INestApplication } from "@nestjs/common"; import { NestFactory } from "@nestjs/core"; import helmet from "helmet"; import { AppModule } from "./app.module.js"; import { HttpResponseInterceptor } from "./common/http-response.interceptor.js"; import { DomainExceptionFilter } from "./common/domain-exception.filter.js"; export function configureApp(app: INestApplication): void { const allowedOrigin = process.env.WEB_ORIGIN ?? "http://localhost:3000"; app.setGlobalPrefix("api/v1"); app.use(helmet()); app.useGlobalPipes( new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true, transform: true, }), ); app.useGlobalInterceptors(new HttpResponseInterceptor()); app.useGlobalFilters(new DomainExceptionFilter()); app.enableCors({ origin( origin: string | undefined, callback: (error: Error | null, allow?: boolean) => void, ) { callback(null, origin === undefined || origin === allowedOrigin); }, credentials: true, }); } type AppFactory = () => Promise; export async function bootstrap( appFactory: AppFactory = () => NestFactory.create(AppModule), ): Promise { const app = await appFactory(); configureApp(app); app.enableShutdownHooks(); await app.listen(Number(process.env.PORT ?? 3001), "0.0.0.0"); } if (process.env.NODE_ENV !== "test") void bootstrap();