generateSwagger.js

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const swagger_autogen_1 = __importDefault(require("swagger-autogen"));
const outputFile = './src/swagger-output.json';
/**
 * @typedef {Object} SwaggerInfo
 * @property {string} title - The title of the API.
 * @property {string} description - The description of the API.
 * @property {string} version - The version of the API.
 */
/**
 * @typedef {Object} SwaggerServer
 * @property {string} url - The server URL.
 */
/**
 * @typedef {Object} SwaggerDoc
 * @property {SwaggerInfo} info - Information about the API.
 * @property {string} host - The host URL for the API.
 * @property {SwaggerServer[]} servers - An array of server objects.
 */
/**
 * List of endpoint files to generate Swagger documentation for.
 *
 * @type {string[]}
 */
const endpointsFiles = [
    './src/routes/admin-router/admin-router.ts',
    './src/routes/county-router/county-router.ts',
    './src/routes/mongodb/mongodb-routes.ts',
];
/**
 * Swagger documentation configuration object.
 *
 * @type {SwaggerDoc}
 */
const doc = {
    info: {
        title: 'Express API with Swagger',
        description: 'API documentation',
        version: '1.0.0',
    },
    host: 'localhost:5000',
    servers: [
        {
            url: 'http://localhost:5000', // Set your server URL here
        },
    ],
};
/**
 * Generates Swagger documentation JSON and README.md in Markdown format.
 */
(0, swagger_autogen_1.default)()(outputFile, endpointsFiles, doc).then(() => {
    console.log('Swagger JSON generated');
    // Read Swagger JSON and convert it to Markdown
    const swaggerData = JSON.parse(fs_1.default.readFileSync(outputFile, 'utf-8'));
    const markdown = generateMarkdown(swaggerData);
    // Write to README.md
    fs_1.default.writeFileSync('./README.md', markdown, 'utf-8');
    console.log('README.md generated');
});
/**
 * Converts Swagger documentation JSON into a Markdown string for use in a README file.
 *
 * @param {Object} swaggerData - The Swagger documentation JSON object.
 * @returns {string} The generated Markdown string.
 * @example
 * const markdown = generateMarkdown(swaggerData);
 * console.log(markdown);
 */
function generateMarkdown(swaggerData) {
    const { info, paths } = swaggerData;
    console.log('Swagger Info:', info); // Debug info
    console.log('Swagger Paths:', paths); // Debug paths
    let markdown = `# ${info.title}\n\n${info.description}\n\n## Endpoints\n`;
    for (const [path, methods] of Object.entries(paths)) {
        markdown += `### ${path}\n`;
        for (const [method, details] of Object.entries(methods)) {
            console.log(`Method Details for ${method.toUpperCase()} ${path}:`, details); // Debug method details
            markdown += `#### ${method.toUpperCase()}\n`;
            const methodDetails = details;
            markdown += `- **Summary**: ${methodDetails.summary || 'N/A'}\n`;
            markdown += `- **Description**: ${methodDetails.description || 'N/A'}\n\n`;
        }
    }
    return markdown;
}