UNPKG

@fdm-monster/server

Version:

FDM Monster is a bulk OctoPrint, Klipper, PrusaLink and BambuLab manager to set up, configure and monitor 3D printers. Our aim is to provide neat overview over your farm.

82 lines (81 loc) 2.74 kB
import { AppConstants } from "../../server.constants.js"; import { ensureDirExists, getMediaPath } from "../fs.utils.js"; import { SwaggerGenerator } from "./generator.js"; import { dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; import { static as static$1 } from "express"; import { writeFile } from "node:fs/promises"; //#region src/utils/swagger/swagger.ts function getSwaggerUiDistPath() { try { return dirname(fileURLToPath(import.meta.resolve("swagger-ui-dist/package.json"))); } catch { return join(process.cwd(), "node_modules", "swagger-ui-dist"); } } function generateSwaggerHTML() { return ` <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>FDM Monster API Documentation</title> <link rel="stylesheet" href="/api-docs/static/swagger-ui.css" /> <style> html { box-sizing: border-box; overflow: -moz-scrollbars-vertical; overflow-y: scroll; } *, *:before, *:after { box-sizing: inherit; } body { margin: 0; padding: 0; } </style> </head> <body> <div id="swagger-ui"></div> <script src="/api-docs/static/swagger-ui-bundle.js"><\/script> <script src="/api-docs/static/swagger-ui-standalone-preset.js"><\/script> <script> window.onload = function() { window.ui = SwaggerUIBundle({ url: '/api-docs/swagger.json', dom_id: '#swagger-ui', deepLinking: true, presets: [ SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset ], plugins: [ SwaggerUIBundle.plugins.DownloadUrl ], layout: "StandaloneLayout" }); }; <\/script> </body> </html> `.trim(); } async function setupSwagger(app, logger) { const specification = await new SwaggerGenerator(logger).generate(); if (process.env[AppConstants.GENERATE_SWAGGER_JSON] === "true") try { const mediaPath = getMediaPath(); ensureDirExists(mediaPath); const swaggerJsonPath = join(mediaPath, "swagger.json"); await writeFile(swaggerJsonPath, JSON.stringify(specification, null, 2), "utf-8"); logger.log(`Swagger JSON file generated at: ${swaggerJsonPath}`); } catch (error) { logger.error("Failed to generate swagger.json file", error); } const swaggerUiPath = getSwaggerUiDistPath(); app.use("/api-docs/static", static$1(swaggerUiPath)); app.get("/api-docs/swagger.json", (_req, res) => { res.setHeader("Content-Type", "application/json"); res.send(specification); }); app.get("/api-docs", (_req, res) => { res.setHeader("Content-Type", "text/html"); res.send(generateSwaggerHTML()); }); return specification; } //#endregion export { setupSwagger }; //# sourceMappingURL=swagger.js.map