mirror of
https://github.com/zadam/trilium.git
synced 2025-12-05 06:54:23 +01:00
fix(docs): try to fix swagger ui api pages, take 1
This commit is contained in:
parent
a58cfbec05
commit
055556891d
@ -3,34 +3,53 @@ 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, existsSync } from "fs";
|
import fs from "fs";
|
||||||
import { RESOURCE_DIR } from "../services/resource_dir";
|
import { RESOURCE_DIR } from "../services/resource_dir";
|
||||||
import log from "../services/log";
|
import log from "../services/log";
|
||||||
|
|
||||||
export default function register(app: Application) {
|
// Cache the documents to avoid repeated file reads, especially important for ASAR archives
|
||||||
|
let etapiDocument: JsonObject | null = null;
|
||||||
|
let apiDocument: JsonObject | null = null;
|
||||||
|
|
||||||
|
function loadDocuments(): { etapi: JsonObject | null; api: JsonObject | null } {
|
||||||
|
if (etapiDocument && apiDocument) {
|
||||||
|
return { etapi: etapiDocument, api: apiDocument };
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const etapiPath = join(RESOURCE_DIR, "etapi.openapi.yaml");
|
const etapiPath = join(RESOURCE_DIR, "etapi.openapi.yaml");
|
||||||
const apiPath = join(RESOURCE_DIR, "api-openapi.yaml");
|
const apiPath = join(RESOURCE_DIR, "api-openapi.yaml");
|
||||||
|
|
||||||
// Check if files exist
|
// Load and cache the documents
|
||||||
if (!existsSync(etapiPath)) {
|
const etapiYaml = fs.readFileSync(etapiPath, "utf8");
|
||||||
log.error(`ETAPI OpenAPI spec not found at: ${etapiPath}`);
|
etapiDocument = yaml.load(etapiYaml) as JsonObject;
|
||||||
return;
|
|
||||||
|
const apiYaml = fs.readFileSync(apiPath, "utf8");
|
||||||
|
apiDocument = yaml.load(apiYaml) as JsonObject;
|
||||||
|
|
||||||
|
log.info("OpenAPI documents loaded successfully");
|
||||||
|
return { etapi: etapiDocument, api: apiDocument };
|
||||||
|
} catch (error) {
|
||||||
|
log.error(`Failed to load OpenAPI documents from ${RESOURCE_DIR}: ${error}`);
|
||||||
|
return { etapi: null, api: null };
|
||||||
}
|
}
|
||||||
if (!existsSync(apiPath)) {
|
|
||||||
log.error(`API OpenAPI spec not found at: ${apiPath}`);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const etapiDocument = yaml.load(readFileSync(etapiPath, "utf8")) as JsonObject;
|
export default function register(app: Application) {
|
||||||
const apiDocument = yaml.load(readFileSync(apiPath, "utf8")) as JsonObject;
|
try {
|
||||||
|
const docs = loadDocuments();
|
||||||
|
|
||||||
|
if (!docs.etapi || !docs.api) {
|
||||||
|
log.error("OpenAPI documents could not be loaded, skipping API documentation setup");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Use serveFiles for multiple Swagger instances
|
// Use serveFiles for multiple Swagger instances
|
||||||
// Note: serveFiles returns an array of middleware, so we need to spread it
|
// Note: serveFiles returns an array of middleware, so we need to spread it
|
||||||
app.use(
|
app.use(
|
||||||
"/etapi/docs",
|
"/etapi/docs",
|
||||||
...swaggerUi.serveFiles(etapiDocument),
|
...swaggerUi.serveFiles(docs.etapi),
|
||||||
swaggerUi.setup(etapiDocument, {
|
swaggerUi.setup(docs.etapi, {
|
||||||
explorer: true,
|
explorer: true,
|
||||||
customSiteTitle: "TriliumNext ETAPI Documentation"
|
customSiteTitle: "TriliumNext ETAPI Documentation"
|
||||||
})
|
})
|
||||||
@ -38,8 +57,8 @@ export default function register(app: Application) {
|
|||||||
|
|
||||||
app.use(
|
app.use(
|
||||||
"/api/docs",
|
"/api/docs",
|
||||||
...swaggerUi.serveFiles(apiDocument),
|
...swaggerUi.serveFiles(docs.api),
|
||||||
swaggerUi.setup(apiDocument, {
|
swaggerUi.setup(docs.api, {
|
||||||
explorer: true,
|
explorer: true,
|
||||||
customSiteTitle: "TriliumNext Internal API Documentation",
|
customSiteTitle: "TriliumNext Internal API Documentation",
|
||||||
customCss: '.swagger-ui .topbar { display: none }'
|
customCss: '.swagger-ui .topbar { display: none }'
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user