From a58cfbec05bca52971efa9d60b893b99657eeaed Mon Sep 17 00:00:00 2001 From: perf3ct Date: Wed, 27 Aug 2025 19:41:51 +0000 Subject: [PATCH] fix(docs): try to fix swagger ui api pages, take 1 --- apps/server/src/routes/api_docs.ts | 64 ++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 22 deletions(-) diff --git a/apps/server/src/routes/api_docs.ts b/apps/server/src/routes/api_docs.ts index df39f3d61..d2fc1b04a 100644 --- a/apps/server/src/routes/api_docs.ts +++ b/apps/server/src/routes/api_docs.ts @@ -3,31 +3,51 @@ import swaggerUi from "swagger-ui-express"; import { join } from "path"; import yaml from "js-yaml"; import type { JsonObject } from "swagger-ui-express"; -import { readFileSync } from "fs"; +import { readFileSync, existsSync } from "fs"; import { RESOURCE_DIR } from "../services/resource_dir"; +import log from "../services/log"; export default function register(app: Application) { - const etapiDocument = yaml.load(readFileSync(join(RESOURCE_DIR, "etapi.openapi.yaml"), "utf8")) as JsonObject; - - // Load the comprehensive API documentation from YAML - const apiDocument = yaml.load(readFileSync(join(RESOURCE_DIR, "api-openapi.yaml"), "utf8")) as JsonObject; + try { + const etapiPath = join(RESOURCE_DIR, "etapi.openapi.yaml"); + const apiPath = join(RESOURCE_DIR, "api-openapi.yaml"); + + // Check if files exist + if (!existsSync(etapiPath)) { + log.error(`ETAPI OpenAPI spec not found at: ${etapiPath}`); + return; + } + if (!existsSync(apiPath)) { + log.error(`API OpenAPI spec not found at: ${apiPath}`); + return; + } + + const etapiDocument = yaml.load(readFileSync(etapiPath, "utf8")) as JsonObject; + const apiDocument = yaml.load(readFileSync(apiPath, "utf8")) as JsonObject; - app.use( - "/etapi/docs/", - swaggerUi.serveFiles(etapiDocument), - swaggerUi.setup(etapiDocument, { - explorer: true, - customSiteTitle: "TriliumNext ETAPI Documentation" - }) - ); + // Use serveFiles for multiple Swagger instances + // Note: serveFiles returns an array of middleware, so we need to spread it + app.use( + "/etapi/docs", + ...swaggerUi.serveFiles(etapiDocument), + swaggerUi.setup(etapiDocument, { + explorer: true, + customSiteTitle: "TriliumNext ETAPI Documentation" + }) + ); - app.use( - "/api/docs/", - swaggerUi.serveFiles(apiDocument), - swaggerUi.setup(apiDocument, { - explorer: true, - customSiteTitle: "TriliumNext Internal API Documentation", - customCss: '.swagger-ui .topbar { display: none }' - }) - ); + app.use( + "/api/docs", + ...swaggerUi.serveFiles(apiDocument), + swaggerUi.setup(apiDocument, { + explorer: true, + customSiteTitle: "TriliumNext Internal API Documentation", + customCss: '.swagger-ui .topbar { display: none }' + }) + ); + + log.info("Swagger UI endpoints registered at /etapi/docs and /api/docs"); + } catch (error) { + log.error(`Failed to setup API documentation: ${error}`); + } }