fix(docs): try to fix swagger ui api pages, take 1

This commit is contained in:
perf3ct 2025-08-27 19:41:51 +00:00
parent 3795be4750
commit a58cfbec05
No known key found for this signature in database
GPG Key ID: 569C4EEC436F5232

View File

@ -3,18 +3,33 @@ 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;
try {
const etapiPath = join(RESOURCE_DIR, "etapi.openapi.yaml");
const apiPath = join(RESOURCE_DIR, "api-openapi.yaml");
// Load the comprehensive API documentation from YAML
const apiDocument = yaml.load(readFileSync(join(RESOURCE_DIR, "api-openapi.yaml"), "utf8")) as JsonObject;
// 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;
// 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),
"/etapi/docs",
...swaggerUi.serveFiles(etapiDocument),
swaggerUi.setup(etapiDocument, {
explorer: true,
customSiteTitle: "TriliumNext ETAPI Documentation"
@ -22,12 +37,17 @@ export default function register(app: Application) {
);
app.use(
"/api/docs/",
swaggerUi.serveFiles(apiDocument),
"/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}`);
}
}