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,31 +3,51 @@ import swaggerUi from "swagger-ui-express";
import { join } from "path"; import { join } from "path";
import yaml from "js-yaml"; import yaml from "js-yaml";
import type { JsonObject } from "swagger-ui-express"; import type { JsonObject } from "swagger-ui-express";
import { readFileSync } from "fs"; import { readFileSync, existsSync } from "fs";
import { RESOURCE_DIR } from "../services/resource_dir"; import { RESOURCE_DIR } from "../services/resource_dir";
import log from "../services/log";
export default function register(app: Application) { 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 // Check if files exist
const apiDocument = yaml.load(readFileSync(join(RESOURCE_DIR, "api-openapi.yaml"), "utf8")) as JsonObject; 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;
}
app.use( const etapiDocument = yaml.load(readFileSync(etapiPath, "utf8")) as JsonObject;
"/etapi/docs/", const apiDocument = yaml.load(readFileSync(apiPath, "utf8")) as JsonObject;
swaggerUi.serveFiles(etapiDocument),
swaggerUi.setup(etapiDocument, {
explorer: true,
customSiteTitle: "TriliumNext ETAPI Documentation"
})
);
app.use( // Use serveFiles for multiple Swagger instances
"/api/docs/", // Note: serveFiles returns an array of middleware, so we need to spread it
swaggerUi.serveFiles(apiDocument), app.use(
swaggerUi.setup(apiDocument, { "/etapi/docs",
explorer: true, ...swaggerUi.serveFiles(etapiDocument),
customSiteTitle: "TriliumNext Internal API Documentation", swaggerUi.setup(etapiDocument, {
customCss: '.swagger-ui .topbar { display: none }' 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 }'
})
);
log.info("Swagger UI endpoints registered at /etapi/docs and /api/docs");
} catch (error) {
log.error(`Failed to setup API documentation: ${error}`);
}
} }