import { type INestApplication, RequestMethod, ValidationPipe } from '@nestjs/common'; import { HttpExceptionFilter } from './common/filters/http-exception.filter.js'; import { ResponseInterceptor } from './common/interceptors/response.interceptor.js'; import { APP_CONFIG, isOriginAllowed } from './config/app-config.js'; import type { AppConfig } from './config/app-config.interface.js'; /** Shared by main.ts and the e2e tests. */ export function setupApp(app: INestApplication): INestApplication { const config = app.get(APP_CONFIG); app.setGlobalPrefix('api', { exclude: [ { path: 'health', method: RequestMethod.GET }, { path: 'ready', method: RequestMethod.GET }, { path: 'internal/{*path}', method: RequestMethod.ALL }, ], }); app.useGlobalPipes(new ValidationPipe({ transform: true, whitelist: true, forbidNonWhitelisted: true })); app.useGlobalInterceptors(new ResponseInterceptor()); app.useGlobalFilters(new HttpExceptionFilter()); app.enableCors({ origin: (origin: string | undefined, done: (error: Error | null, allow?: boolean) => void) => done(null, !origin || isOriginAllowed(config, origin)), methods: ['GET', 'POST'], allowedHeaders: ['Authorization', 'Content-Type'], maxAge: 600, }); return app; }