49 lines
1.4 KiB
TypeScript
49 lines
1.4 KiB
TypeScript
import { NestFactory } from '@nestjs/core';
|
|
import { AppModule } from './app.module';
|
|
import { BadRequestException, Logger, ValidationPipe } from '@nestjs/common';
|
|
import { ValidationError } from 'class-validator';
|
|
|
|
async function bootstrap() {
|
|
const app = await NestFactory.create(AppModule);
|
|
|
|
const logger = new Logger('bootstrap');
|
|
const origin = ['https://shows.everest.tailscale/admin/'];
|
|
if (process.env.NODE_ENV == 'production') {
|
|
logger.log('In production mode');
|
|
} else {
|
|
logger.log('In development mode');
|
|
origin.push('http://localhost:4200');
|
|
}
|
|
app.enableCors({
|
|
credentials: true,
|
|
origin,
|
|
});
|
|
app.useGlobalPipes(
|
|
new ValidationPipe({
|
|
transform: true,
|
|
// https://stackoverflow.com/questions/75581669/customize-error-message-in-nest-js-using-class-validator
|
|
exceptionFactory: (validationErrors: ValidationError[] = []) => {
|
|
const errors = validationErrors.map((error) => ({
|
|
error: Object.values(error.constraints).join(', '),
|
|
}));
|
|
|
|
let errorMessage: string;
|
|
errors.forEach((value, index) => {
|
|
if (index == 0) {
|
|
errorMessage = value.error;
|
|
} else {
|
|
errorMessage = errorMessage.concat(', ', value.error);
|
|
}
|
|
});
|
|
|
|
return new BadRequestException({
|
|
status: 'Validation Error',
|
|
message: errorMessage,
|
|
});
|
|
},
|
|
}),
|
|
);
|
|
|
|
await app.listen(process.env.PORT);
|
|
}
|
|
bootstrap();
|