You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.5 KiB

11 months ago
  1. const swaggerAutogen = require("swagger-autogen")({ openapi: "3.0.0" });
  2. const fs = require("fs");
  3. const path = require("path");
  4. const doc = {
  5. info: {
  6. version: "1.0.0",
  7. title: "AnythingLLM Developer API",
  8. description:
  9. "API endpoints that enable programmatic reading, writing, and updating of your AnythingLLM instance. UI supplied by Swagger.io.",
  10. },
  11. // Swagger-autogen does not allow us to use relative paths as these will resolve to
  12. // http:///api in the openapi.json file, so we need to monkey-patch this post-generation.
  13. host: "/api",
  14. schemes: ["http"],
  15. securityDefinitions: {
  16. BearerAuth: {
  17. type: "http",
  18. scheme: "bearer",
  19. bearerFormat: "JWT",
  20. },
  21. },
  22. security: [{ BearerAuth: [] }],
  23. definitions: {
  24. InvalidAPIKey: {
  25. message: "Invalid API Key",
  26. },
  27. },
  28. };
  29. const outputFile = path.resolve(__dirname, "./openapi.json");
  30. const endpointsFiles = [
  31. "../endpoints/api/auth/index.js",
  32. "../endpoints/api/admin/index.js",
  33. "../endpoints/api/document/index.js",
  34. "../endpoints/api/workspace/index.js",
  35. "../endpoints/api/system/index.js",
  36. "../endpoints/api/workspaceThread/index.js",
  37. "../endpoints/api/userManagement/index.js",
  38. "../endpoints/api/openai/index.js",
  39. "../endpoints/api/embed/index.js",
  40. ];
  41. swaggerAutogen(outputFile, endpointsFiles, doc).then(({ data }) => {
  42. // Remove Authorization parameters from arguments.
  43. for (const path of Object.keys(data.paths)) {
  44. if (data.paths[path].hasOwnProperty("get")) {
  45. let parameters = data.paths[path].get?.parameters || [];
  46. parameters = parameters.filter((arg) => arg.name !== "Authorization");
  47. data.paths[path].get.parameters = parameters;
  48. }
  49. if (data.paths[path].hasOwnProperty("post")) {
  50. let parameters = data.paths[path].post?.parameters || [];
  51. parameters = parameters.filter((arg) => arg.name !== "Authorization");
  52. data.paths[path].post.parameters = parameters;
  53. }
  54. if (data.paths[path].hasOwnProperty("delete")) {
  55. let parameters = data.paths[path].delete?.parameters || [];
  56. parameters = parameters.filter((arg) => arg.name !== "Authorization");
  57. data.paths[path].delete.parameters = parameters;
  58. }
  59. }
  60. const openApiSpec = {
  61. ...data,
  62. servers: [
  63. {
  64. url: "/api",
  65. },
  66. ],
  67. };
  68. fs.writeFileSync(outputFile, JSON.stringify(openApiSpec, null, 2), {
  69. encoding: "utf-8",
  70. flag: "w",
  71. });
  72. console.log(`Swagger-autogen: \x1b[32mPatched servers.url ✔\x1b[0m`);
  73. });