2024-06-18 01:24:41 +02:00
|
|
|
const swaggerAutogen = require("swagger-autogen")({ openapi: "3.0.0" });
|
|
|
|
const fs = require("fs");
|
|
|
|
const path = require("path");
|
2023-08-24 04:15:07 +02:00
|
|
|
|
|
|
|
const doc = {
|
|
|
|
info: {
|
2024-06-18 01:24:41 +02:00
|
|
|
version: "1.0.0",
|
|
|
|
title: "AnythingLLM Developer API",
|
|
|
|
description:
|
|
|
|
"API endpoints that enable programmatic reading, writing, and updating of your AnythingLLM instance. UI supplied by Swagger.io.",
|
2023-08-24 04:15:07 +02:00
|
|
|
},
|
2024-01-10 04:49:51 +01:00
|
|
|
// Swagger-autogen does not allow us to use relative paths as these will resolve to
|
|
|
|
// http:///api in the openapi.json file, so we need to monkey-patch this post-generation.
|
2024-06-18 01:24:41 +02:00
|
|
|
host: "/api",
|
|
|
|
schemes: ["http"],
|
2023-08-24 04:15:07 +02:00
|
|
|
securityDefinitions: {
|
|
|
|
BearerAuth: {
|
2024-06-18 01:24:41 +02:00
|
|
|
type: "http",
|
|
|
|
scheme: "bearer",
|
|
|
|
bearerFormat: "JWT",
|
|
|
|
},
|
2023-08-24 04:15:07 +02:00
|
|
|
},
|
2024-06-18 01:24:41 +02:00
|
|
|
security: [{ BearerAuth: [] }],
|
2023-08-24 04:15:07 +02:00
|
|
|
definitions: {
|
|
|
|
InvalidAPIKey: {
|
2024-06-18 01:24:41 +02:00
|
|
|
message: "Invalid API Key",
|
2023-08-24 04:15:07 +02:00
|
|
|
},
|
2024-06-18 01:24:41 +02:00
|
|
|
},
|
2023-08-24 04:15:07 +02:00
|
|
|
};
|
|
|
|
|
2024-06-18 01:24:41 +02:00
|
|
|
const outputFile = path.resolve(__dirname, "./openapi.json");
|
2023-08-24 04:15:07 +02:00
|
|
|
const endpointsFiles = [
|
2024-06-18 01:24:41 +02:00
|
|
|
"../endpoints/api/auth/index.js",
|
|
|
|
"../endpoints/api/admin/index.js",
|
|
|
|
"../endpoints/api/document/index.js",
|
|
|
|
"../endpoints/api/workspace/index.js",
|
|
|
|
"../endpoints/api/system/index.js",
|
2024-06-21 01:27:35 +02:00
|
|
|
"../endpoints/api/workspaceThread/index.js",
|
2024-06-18 01:24:41 +02:00
|
|
|
"../endpoints/api/userManagement/index.js",
|
2023-08-24 04:15:07 +02:00
|
|
|
];
|
|
|
|
|
2024-06-18 01:24:41 +02:00
|
|
|
swaggerAutogen(outputFile, endpointsFiles, doc).then(({ data }) => {
|
|
|
|
// Remove Authorization parameters from arguments.
|
|
|
|
for (const path of Object.keys(data.paths)) {
|
|
|
|
if (data.paths[path].hasOwnProperty("get")) {
|
|
|
|
let parameters = data.paths[path].get?.parameters || [];
|
|
|
|
parameters = parameters.filter((arg) => arg.name !== "Authorization");
|
|
|
|
data.paths[path].get.parameters = parameters;
|
|
|
|
}
|
2024-05-22 04:30:37 +02:00
|
|
|
|
2024-06-18 01:24:41 +02:00
|
|
|
if (data.paths[path].hasOwnProperty("post")) {
|
|
|
|
let parameters = data.paths[path].post?.parameters || [];
|
|
|
|
parameters = parameters.filter((arg) => arg.name !== "Authorization");
|
|
|
|
data.paths[path].post.parameters = parameters;
|
2024-05-22 04:30:37 +02:00
|
|
|
}
|
|
|
|
|
2024-06-18 01:24:41 +02:00
|
|
|
if (data.paths[path].hasOwnProperty("delete")) {
|
|
|
|
let parameters = data.paths[path].delete?.parameters || [];
|
|
|
|
parameters = parameters.filter((arg) => arg.name !== "Authorization");
|
|
|
|
data.paths[path].delete.parameters = parameters;
|
2024-01-10 04:49:51 +01:00
|
|
|
}
|
2024-06-18 01:24:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const openApiSpec = {
|
|
|
|
...data,
|
|
|
|
servers: [
|
|
|
|
{
|
|
|
|
url: "/api",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
fs.writeFileSync(outputFile, JSON.stringify(openApiSpec, null, 2), {
|
|
|
|
encoding: "utf-8",
|
|
|
|
flag: "w",
|
|
|
|
});
|
|
|
|
console.log(`Swagger-autogen: \x1b[32mPatched servers.url ✔\x1b[0m`);
|
|
|
|
});
|