Skip to content

Build your personal knowledge base with Trilium Notes

add Swagger UI endpoint and add to router

perf3ct18f5f1b759a8
diff --git a/src/routes/api_docs.ts b/src/routes/api_docs.ts
new file mode 100644
index 0000000000..7546c50d17
--- /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 1de332be0e..05c7612f2d 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);
}