legendaryjs
Version:
LegendaryJS – The ultimate backend framework for speed, power, and simplicity.
61 lines (51 loc) • 1.77 kB
JavaScript
const swaggerUi = require('swagger-ui-express');
const fs = require('fs');
require('dotenv').config();
const config = require('./legendary.config');
const locale = process.env.DOC_LANG || 'en';
const langFile = require(`./locales/${locale}.json`);
function generateSwaggerDoc() {
const routesDir = path.join(process.cwd(), 'routes');
const files = fs.readdirSync(routesDir).filter(f => f.startsWith('api.v') && f.endsWith('.json'));
const paths = {};
files.forEach(file => {
const version = file.match(/api\.v(\d+)\.json/)[1];
const routes = require(path.join(routesDir, file)).routes;
routes.forEach(route => {
const fullPath = `/v${version}${route.path}`;
if (!paths[fullPath]) paths[fullPath] = {};
paths[fullPath][route.method.toLowerCase()] = {
tags: [`v${version}`],
summary: langFile[`${route.method} ${route.path}`] || `${route.method} ${route.path}`,
responses: {
200: {
description: 'Successful Response',
content: {
'application/json': {
example: route.response || {}
}
}
}
}
};
});
});
const swaggerDoc = {
openapi: '3.0.0',
info: {
title: 'LegendaryJS API',
version: '1.0.0',
description: 'Automatically generated API documentation'
},
servers: [{ url: 'http://localhost:3000' }],
paths
};
return swaggerDoc;
}
function setupSwagger(app) {
if (!config.docs.swagger) return;
const doc = generateSwaggerDoc();
app.use('/docs', swaggerUi.serve, swaggerUi.setup(doc));
console.log('📘 Swagger docs available at /docs');
}
module.exports = { setupSwagger };