const swaggerJsdoc = require('swagger-jsdoc'); const fs = require('fs'); const path = require('path'); const yaml = require('js-yaml'); // Fonction pour charger tous les fichiers YAML du dossier swagger const loadSwaggerFiles = () => { const swaggerDir = path.join(__dirname, '../swagger'); const files = fs.readdirSync(swaggerDir).filter(file => file.endsWith('.yaml')); const merged = { paths: {}, components: { schemas: {} } }; files.forEach(file => { try { const content = fs.readFileSync(path.join(swaggerDir, file), 'utf8'); const doc = yaml.load(content); // Fusionner les paths if (doc.paths) { Object.assign(merged.paths, doc.paths); } // Fusionner les schemas if (doc.components && doc.components.schemas) { Object.assign(merged.components.schemas, doc.components.schemas); } } catch (error) { console.error(`Erreur lors du chargement de ${file}:`, error); } }); return merged; }; const swaggerDefinitions = loadSwaggerFiles(); const options = { definition: { openapi: '3.0.0', info: { title: 'Adastra API', version: '1.0.0', description: 'Adastra API Documentation - Express.js API with Sequelize (MySQL) and Mongoose (MongoDB)', contact: { name: 'Julien Gautier', email: 'jgautier.webdev@gmail.com', }, }, servers: [ { url: 'http://localhost:3201', description: 'Development server', }, { url: 'http://localhost:3200', description: 'Local development server', }, { url: 'https://api.example.com', description: 'Production server', }, ], components: { securitySchemes: { bearerAuth: { type: 'http', scheme: 'bearer', bearerFormat: 'JWT', description: 'JWT Bearer token authentication', }, apiKeyAuth: { type: 'apiKey', in: 'header', name: 'x-api-key', description: 'API Key authentication', }, }, schemas: { Error: { type: 'object', properties: { error: { type: 'string', }, message: { type: 'string', }, statusCode: { type: 'integer', }, }, }, ...swaggerDefinitions.components.schemas, }, }, paths: swaggerDefinitions.paths, tags: [ { name: 'Articles', description: 'Article management endpoints', }, { name: 'Clans', description: 'Clan management endpoints', }, { name: 'Members', description: 'Member management endpoints', }, { name: 'Users', description: 'User management endpoints', }, { name: 'Products', description: 'Product management endpoints', }, { name: 'Hero Wars', description: 'Hero Wars game data endpoints', }, ], }, // Les définitions sont chargées depuis les fichiers YAML apis: [], }; const swaggerSpec = swaggerJsdoc(options); module.exports = swaggerSpec;