Server/src/main.ts
2024-12-06 01:15:40 +01:00

60 lines
1.5 KiB
TypeScript

import {
ClassSerializerInterceptor,
HttpStatus,
ValidationPipe,
} from "@nestjs/common";
import { HttpAdapterHost, NestFactory, Reflector } from "@nestjs/core";
import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger";
import { PrismaClientExceptionFilter } from "nestjs-prisma";
import { AppModule } from "./app.module";
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.enableCors({
origin: "*",
});
const { httpAdapter } = app.get(HttpAdapterHost);
app.useGlobalFilters(
new PrismaClientExceptionFilter(httpAdapter, {
P2000: {
errorMessage: "A required field is missing",
statusCode: HttpStatus.BAD_REQUEST,
},
P2002: {
errorMessage:
"A ressource with the same unique fields already exists",
statusCode: HttpStatus.CONFLICT,
},
P2025: {
errorMessage: "The requested ressource does not exist",
statusCode: HttpStatus.NOT_FOUND,
},
}),
);
app.useGlobalPipes(new ValidationPipe());
app.useGlobalInterceptors(
new ClassSerializerInterceptor(app.get(Reflector), {
excludeExtraneousValues: true,
}),
);
const config = new DocumentBuilder()
.setTitle("Toogether API")
.addBearerAuth()
.build();
const documentFactory = () => SwaggerModule.createDocument(app, config);
SwaggerModule.setup("documentation", app, documentFactory, {
swaggerOptions: {
tryItOutEnabled: true,
persistAuthorization: true,
},
});
await app.listen(process.env.PORT ?? 3000, "0.0.0.0");
}
bootstrap();