79 lines
2.6 KiB
TypeScript
79 lines
2.6 KiB
TypeScript
import { NestFactory } from '@nestjs/core';
|
|
import { AppModule } from './app.module';
|
|
import { ValidationPipe, Logger, BadRequestException } from '@nestjs/common';
|
|
import helmet from 'helmet';
|
|
import { KeycloakExceptionFilter } from './filters/keycloak-exception.filter';
|
|
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
|
|
import { useContainer } from 'class-validator';
|
|
|
|
async function bootstrap() {
|
|
const app = await NestFactory.create(AppModule);
|
|
const logger = new Logger('dcb-user-service');
|
|
|
|
useContainer(app.select(AppModule), { fallbackOnErrors: true });
|
|
|
|
// Middlewares de sécurité
|
|
app.use(helmet());
|
|
app.enableCors({
|
|
origin: process.env.ALLOWED_ORIGINS?.split(',') || '*',
|
|
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH'],
|
|
allowedHeaders: ['Content-Type', 'Authorization']
|
|
});
|
|
|
|
// Gestion globale des erreurs et validation
|
|
app.useGlobalFilters(new KeycloakExceptionFilter());
|
|
|
|
// ValidationPipe CORRIGÉ
|
|
app.useGlobalPipes(new ValidationPipe({
|
|
whitelist: true,
|
|
forbidNonWhitelisted: true,
|
|
transform: true,
|
|
transformOptions: {
|
|
enableImplicitConversion: true,
|
|
},
|
|
exceptionFactory: (errors) => {
|
|
const messages = errors.map(error => {
|
|
// Détails complets de l'erreur
|
|
const constraints = error.constraints ? Object.values(error.constraints) : ['Unknown validation error'];
|
|
return {
|
|
field: error.property,
|
|
errors: constraints,
|
|
value: error.value,
|
|
children: error.children
|
|
};
|
|
});
|
|
|
|
console.log('🔴 VALIDATION ERRORS:', JSON.stringify(messages, null, 2));
|
|
|
|
return new BadRequestException({
|
|
message: 'Validation failed',
|
|
errors: messages,
|
|
details: 'Check the errors array for specific field validation issues'
|
|
});
|
|
}
|
|
}));
|
|
|
|
// Préfixe global de l'API
|
|
app.setGlobalPrefix('api/v1');
|
|
|
|
// Configuration Swagger
|
|
const config = new DocumentBuilder()
|
|
.setTitle('DCB User Service API')
|
|
.setDescription('API de gestion des utilisateurs pour le système DCB')
|
|
.setVersion('1.0')
|
|
.addTag('users', 'Gestion des Utilisateurs')
|
|
.addTag('partners', 'Gestion des Partenaires/Marchants')
|
|
.addBearerAuth()
|
|
.build();
|
|
|
|
const document = SwaggerModule.createDocument(app, config);
|
|
SwaggerModule.setup('api-docs', app, document);
|
|
|
|
// Démarrage du serveur
|
|
const port = process.env.PORT || 3000;
|
|
await app.listen(port);
|
|
|
|
logger.log(`Application running on http://localhost:${port}`);
|
|
logger.log(`Swagger documentation available at http://localhost:${port}/api-docs`);
|
|
}
|
|
bootstrap(); |