mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-30 18:05:55 +01:00 
			
		
		
		
	Compare commits
	
		
			2 Commits
		
	
	
		
			feat/websi
			...
			fix/try-to
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|  | 055556891d | ||
|  | a58cfbec05 | 
| @@ -3,31 +3,70 @@ 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 fs from "fs"; | ||||||
| import { RESOURCE_DIR } from "../services/resource_dir"; | import { RESOURCE_DIR } from "../services/resource_dir"; | ||||||
|  | import log from "../services/log"; | ||||||
|  |  | ||||||
|  | // 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 { | ||||||
|  |         const etapiPath = join(RESOURCE_DIR, "etapi.openapi.yaml"); | ||||||
|  |         const apiPath = join(RESOURCE_DIR, "api-openapi.yaml"); | ||||||
|  |          | ||||||
|  |         // Load and cache the documents | ||||||
|  |         const etapiYaml = fs.readFileSync(etapiPath, "utf8"); | ||||||
|  |         etapiDocument = yaml.load(etapiYaml) as JsonObject; | ||||||
|  |          | ||||||
|  |         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 }; | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
| 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 docs = loadDocuments(); | ||||||
|          |          | ||||||
|     // Load the comprehensive API documentation from YAML |         if (!docs.etapi || !docs.api) { | ||||||
|     const apiDocument = yaml.load(readFileSync(join(RESOURCE_DIR, "api-openapi.yaml"), "utf8")) as JsonObject; |             log.error("OpenAPI documents could not be loaded, skipping API documentation setup"); | ||||||
|  |             return; | ||||||
|  |         } | ||||||
|  |  | ||||||
|     app.use( |         // Use serveFiles for multiple Swagger instances | ||||||
|         "/etapi/docs/", |         // Note: serveFiles returns an array of middleware, so we need to spread it | ||||||
|         swaggerUi.serveFiles(etapiDocument), |         app.use( | ||||||
|         swaggerUi.setup(etapiDocument, { |             "/etapi/docs",  | ||||||
|             explorer: true, |             ...swaggerUi.serveFiles(docs.etapi),  | ||||||
|             customSiteTitle: "TriliumNext ETAPI Documentation" |             swaggerUi.setup(docs.etapi, { | ||||||
|         }) |                 explorer: true, | ||||||
|     ); |                 customSiteTitle: "TriliumNext ETAPI Documentation" | ||||||
|  |             }) | ||||||
|  |         ); | ||||||
|  |  | ||||||
|     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 }' | ||||||
|         }) |             }) | ||||||
|     ); |         ); | ||||||
|  |          | ||||||
|  |         log.info("Swagger UI endpoints registered at /etapi/docs and /api/docs"); | ||||||
|  |     } catch (error) { | ||||||
|  |         log.error(`Failed to setup API documentation: ${error}`); | ||||||
|  |     } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user