From 18f5f1b759a81f936c00cea7eb0ec56287240392 Mon Sep 17 00:00:00 2001 From: perf3ct Date: Sun, 9 Feb 2025 21:15:12 +0000 Subject: [PATCH] add Swagger UI endpoint and add to router --- src/routes/api_docs.ts | 26 ++++++++++++++++++++++++++ src/routes/routes.ts | 5 ++++- 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 src/routes/api_docs.ts diff --git a/src/routes/api_docs.ts b/src/routes/api_docs.ts new file mode 100644 index 000000000..7546c50d1 --- /dev/null +++ b/src/routes/api_docs.ts @@ -0,0 +1,26 @@ +import type { Router } from "express"; +import swaggerUi from "swagger-ui-express"; +import { readFileSync } from "fs"; +import { fileURLToPath } from "url"; +import { dirname, join } from "path"; +import yaml from "js-yaml"; + +const __dirname = dirname(fileURLToPath(import.meta.url)); +const swaggerDocument = yaml.load( + readFileSync(join(__dirname, "../etapi/etapi.openapi.yaml"), "utf8") +) as object; + +function register(router: Router) { + router.use( + "/api-docs", + swaggerUi.serve, + swaggerUi.setup(swaggerDocument, { + explorer: true, + customSiteTitle: "Trilium ETAPI Documentation" + }) + ); +} + +export default { + register +}; diff --git a/src/routes/routes.ts b/src/routes/routes.ts index 1de332be0..05c7612f2 100644 --- a/src/routes/routes.ts +++ b/src/routes/routes.ts @@ -71,7 +71,7 @@ import etapiSpecialNoteRoutes from "../etapi/special_notes.js"; import etapiSpecRoute from "../etapi/spec.js"; import etapiBackupRoute from "../etapi/backup.js"; - +import apiDocsRoute from "./api_docs.js"; const MAX_ALLOWED_FILE_SIZE_MB = 250; const GET = "get", @@ -369,6 +369,9 @@ function register(app: express.Application) { etapiSpecRoute.register(router); etapiBackupRoute.register(router); + // API Documentation + apiDocsRoute.register(app); + app.use("", router); }