prevision / apps /prevision-api /src /app.setup.ts
github-actions[bot]
Automated Deploy: 23110475c742a5d4f03930b55bca09042215c1dd
1fe7075
Raw History Blame Contribute Delete
1.3 kB
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<AppConfig>(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;
}