103 lines
3.0 KiB
TypeScript
103 lines
3.0 KiB
TypeScript
import { NestFactory } from '@nestjs/core';
|
|
import { AppModule } from './app.module';
|
|
import {
|
|
ValidationError,
|
|
ValidationPipe,
|
|
} from '@nestjs/common';
|
|
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
|
|
import { SwaggerDocumentOptions } from '@nestjs/swagger';
|
|
import { BadRequestException } from './exceptions/badRequest.exception';
|
|
import * as cookieParser from 'cookie-parser';
|
|
import { AuthExceptionFilter } from './filters/auth-exception.filter';
|
|
|
|
async function bootstrap() {
|
|
const app = await NestFactory.create(AppModule, {
|
|
logger: ['error', 'warn'],
|
|
cors: {
|
|
// origin: 'https://dev.alphaomegainfosys.com/',
|
|
origin: [
|
|
'http://localhost:3000',
|
|
'http://localhost:5173',
|
|
'https://policy.alphaomegainfosys.com',
|
|
'https://sp.alphaomegainfosys.com',
|
|
'https://client.alphaomegainfosys.com'
|
|
],
|
|
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
|
|
preflightContinue: false,
|
|
optionsSuccessStatus: 204,
|
|
credentials: true,
|
|
},
|
|
});
|
|
|
|
app.use(cookieParser());
|
|
|
|
app.useGlobalFilters(new AuthExceptionFilter());
|
|
|
|
|
|
function extractConstraints(validationErrors: ValidationError[]) {
|
|
const constraints: { [key: string]: string }[] = [];
|
|
|
|
function traverse(errors: ValidationError[]) {
|
|
for (const error of errors) {
|
|
if (error.constraints) {
|
|
constraints.push(error.constraints);
|
|
}
|
|
if (error.children && error.children.length > 0) {
|
|
traverse(error.children);
|
|
}
|
|
}
|
|
}
|
|
|
|
traverse(validationErrors);
|
|
return constraints;
|
|
}
|
|
|
|
const customExceptionFactory = (validationErrors: ValidationError[]) => {
|
|
const res = extractConstraints(validationErrors);
|
|
|
|
const newResult = res
|
|
.map((x) => {
|
|
const errorMessage = x[Object.keys(x)[0]];
|
|
|
|
return { message: errorMessage };
|
|
})
|
|
.filter(Boolean);
|
|
|
|
throw new BadRequestException(`Bad Request${newResult[0].message !== 'Bad Request' ? " : " + newResult[0].message : ""}`);
|
|
};
|
|
|
|
app.useGlobalPipes(
|
|
new ValidationPipe({
|
|
transform: true,
|
|
exceptionFactory: customExceptionFactory,
|
|
whitelist: true,
|
|
stopAtFirstError: true,
|
|
forbidNonWhitelisted: true,
|
|
}),
|
|
);
|
|
|
|
// app.useGlobalPipes(new ValidationPipe({ exceptionFactory:customExceptionFactory, whitelist: true, forbidNonWhitelisted: true, transform: true }));
|
|
const config = new DocumentBuilder()
|
|
.setTitle('API')
|
|
.setDescription('API description')
|
|
.setVersion('1.0')
|
|
.addServer(
|
|
'https://dev.alphaomegainfosys.com/test-api',
|
|
'Development Server 2',
|
|
)
|
|
.addServer('http://localhost:3000', 'Development Server 1')
|
|
.build();
|
|
|
|
const options: SwaggerDocumentOptions = { autoTagControllers: false };
|
|
const documentFactory = () => SwaggerModule.createDocument(app, config, options);
|
|
SwaggerModule.setup('api', app, documentFactory);
|
|
|
|
app.use('/api-json', (req, res) => {
|
|
res.setHeader('Content-Type', 'application/json');
|
|
res.send(document);
|
|
});
|
|
|
|
await app.listen(process.env.PORT ?? 3000);
|
|
}
|
|
bootstrap();
|