mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-03 21:19:01 +01:00 
			
		
		
		
	feat(docs): internal API docs
This commit is contained in:
		
							parent
							
								
									81d5d16e1a
								
							
						
					
					
						commit
						9299f90b85
					
				@ -29,7 +29,12 @@ const copy = async () => {
 | 
				
			|||||||
        fs.copySync(path.join("build", srcFile), destFile, { recursive: true });
 | 
					        fs.copySync(path.join("build", srcFile), destFile, { recursive: true });
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const filesToCopy = ["config-sample.ini", "tsconfig.webpack.json", "./src/etapi/etapi.openapi.yaml"];
 | 
					    const filesToCopy = [
 | 
				
			||||||
 | 
					        "config-sample.ini",
 | 
				
			||||||
 | 
					        "tsconfig.webpack.json",
 | 
				
			||||||
 | 
					        "./src/etapi/etapi.openapi.yaml",
 | 
				
			||||||
 | 
					        "./src/routes/api/openapi.json"
 | 
				
			||||||
 | 
					    ];
 | 
				
			||||||
    for (const file of filesToCopy) {
 | 
					    for (const file of filesToCopy) {
 | 
				
			||||||
        log(`Copying ${file}`);
 | 
					        log(`Copying ${file}`);
 | 
				
			||||||
        await fs.copy(file, path.join(DEST_DIR, file));
 | 
					        await fs.copy(file, path.join(DEST_DIR, file));
 | 
				
			||||||
 | 
				
			|||||||
@ -1,4 +1,7 @@
 | 
				
			|||||||
 | 
					import { fileURLToPath } from "url";
 | 
				
			||||||
 | 
					import { dirname, join } from "path";
 | 
				
			||||||
import swaggerJsdoc from 'swagger-jsdoc';
 | 
					import swaggerJsdoc from 'swagger-jsdoc';
 | 
				
			||||||
 | 
					import fs from "fs";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
 * Usage: npm run generate-openapi | tail -n1 > x.json
 | 
					 * Usage: npm run generate-openapi | tail -n1 > x.json
 | 
				
			||||||
@ -33,8 +36,10 @@ const options = {
 | 
				
			|||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const openapiSpecification = swaggerJsdoc(options);
 | 
					const openapiSpecification = swaggerJsdoc(options);
 | 
				
			||||||
 | 
					const scriptDir = dirname(fileURLToPath(import.meta.url));
 | 
				
			||||||
console.log(JSON.stringify(openapiSpecification));
 | 
					const outputPath = join(scriptDir, "..", "src", "routes", "api", "openapi.json");
 | 
				
			||||||
 | 
					fs.writeFileSync(outputPath, JSON.stringify(openapiSpecification));
 | 
				
			||||||
 | 
					console.log("Saved to ", outputPath);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * @swagger
 | 
					 * @swagger
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										1
									
								
								src/routes/api/openapi.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								src/routes/api/openapi.json
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							@ -1,4 +1,4 @@
 | 
				
			|||||||
import type { Router } from "express";
 | 
					import type { Application, Router } from "express";
 | 
				
			||||||
import swaggerUi from "swagger-ui-express";
 | 
					import swaggerUi from "swagger-ui-express";
 | 
				
			||||||
import { readFile } from "fs/promises";
 | 
					import { readFile } from "fs/promises";
 | 
				
			||||||
import { fileURLToPath } from "url";
 | 
					import { fileURLToPath } from "url";
 | 
				
			||||||
@ -7,19 +7,29 @@ import yaml from "js-yaml";
 | 
				
			|||||||
import type { JsonObject } from "swagger-ui-express";
 | 
					import type { JsonObject } from "swagger-ui-express";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const __dirname = dirname(fileURLToPath(import.meta.url));
 | 
					const __dirname = dirname(fileURLToPath(import.meta.url));
 | 
				
			||||||
const swaggerDocument = yaml.load(
 | 
					const etapiDocument = yaml.load(
 | 
				
			||||||
    await readFile(join(__dirname, "../etapi/etapi.openapi.yaml"), "utf8")
 | 
					    await readFile(join(__dirname, "../etapi/etapi.openapi.yaml"), "utf8")
 | 
				
			||||||
) as JsonObject;
 | 
					) as JsonObject;
 | 
				
			||||||
 | 
					const apiDocument = JSON.parse(await readFile(join(__dirname, "api", "openapi.json"), "utf-8"));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function register(router: Router) {
 | 
					function register(app: Application) {
 | 
				
			||||||
    router.use(
 | 
					    app.use(
 | 
				
			||||||
        "/etapi",
 | 
					        "/etapi/docs/",
 | 
				
			||||||
        swaggerUi.serve,
 | 
					        swaggerUi.serveFiles(etapiDocument),
 | 
				
			||||||
        swaggerUi.setup(swaggerDocument, {
 | 
					        swaggerUi.setup(etapiDocument, {
 | 
				
			||||||
            explorer: true,
 | 
					            explorer: true,
 | 
				
			||||||
            customSiteTitle: "TriliumNext ETAPI Documentation"
 | 
					            customSiteTitle: "TriliumNext ETAPI Documentation"
 | 
				
			||||||
        })
 | 
					        })
 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    app.use(
 | 
				
			||||||
 | 
					        "/api/docs/",
 | 
				
			||||||
 | 
					        swaggerUi.serveFiles(apiDocument),
 | 
				
			||||||
 | 
					        swaggerUi.setup(apiDocument, {
 | 
				
			||||||
 | 
					            explorer: true,
 | 
				
			||||||
 | 
					            customSiteTitle: "TriliumNext Internal API Documentation"
 | 
				
			||||||
 | 
					        })
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default {
 | 
					export default {
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user