126 lines
3.8 KiB
TypeScript
126 lines
3.8 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/${process.env.DB_TYPE === "ORACLE" ? "test-api-1" : process.env.DB_TYPE === "PG" ? "test-api-2" : "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);
|
|
|
|
const document = SwaggerModule.createDocument(app, config, options);
|
|
|
|
// 2. Remove all paths starting with /1 or exactly /1 if not PG
|
|
if (process.env.DB_TYPE === 'PG') {
|
|
for (const path of Object.keys(document.paths)) {
|
|
if (path === '/1' || path.startsWith('/1/')) {
|
|
delete document.paths[path];
|
|
}
|
|
}
|
|
}
|
|
|
|
if (process.env.DB_TYPE === 'ORACLE') {
|
|
for (const path of Object.keys(document.paths)) {
|
|
if (path === '/2' || path.startsWith('/2/')) {
|
|
delete document.paths[path];
|
|
}
|
|
}
|
|
}
|
|
|
|
// 3. Setup Swagger with the modified document
|
|
SwaggerModule.setup('api', app, document);
|
|
|
|
app.use('/api-json', (req, res) => {
|
|
res.setHeader('Content-Type', 'application/json');
|
|
res.send(document);
|
|
});
|
|
|
|
await app.listen(process.env.PORT ?? 3000);
|
|
}
|
|
bootstrap();
|