From 755c10b8ca1a5687902a6597a2dea352620ae88a Mon Sep 17 00:00:00 2001 From: Timothy Carambat <rambat1010@gmail.com> Date: Tue, 9 Jan 2024 19:49:51 -0800 Subject: [PATCH] [API] patch swagger host to be relative (#563) patch swagger host to be relative --- server/swagger/init.js | 18 ++++++++++++++++-- server/swagger/openapi.json | 2 +- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/server/swagger/init.js b/server/swagger/init.js index c84daf323..b68e3249c 100644 --- a/server/swagger/init.js +++ b/server/swagger/init.js @@ -1,4 +1,6 @@ const swaggerAutogen = require('swagger-autogen')({ openapi: '3.0.0' }); +const fs = require('fs') +const path = require('path') const doc = { info: { @@ -6,6 +8,8 @@ const doc = { title: 'AnythingLLM Developer API', description: 'API endpoints that enable programmatic reading, writing, and updating of your AnythingLLM instance. UI supplied by Swagger.io.', }, + // 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. host: '/api', schemes: ['http'], securityDefinitions: { @@ -25,7 +29,7 @@ const doc = { } }; -const outputFile = './openapi.json'; +const outputFile = path.resolve(__dirname, './openapi.json'); const endpointsFiles = [ '../endpoints/api/auth/index.js', '../endpoints/api/admin/index.js', @@ -34,4 +38,14 @@ const endpointsFiles = [ '../endpoints/api/system/index.js', ]; -swaggerAutogen(outputFile, endpointsFiles, doc) \ No newline at end of file +swaggerAutogen(outputFile, endpointsFiles, doc) + .then(({ data }) => { + 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`) + }) \ No newline at end of file diff --git a/server/swagger/openapi.json b/server/swagger/openapi.json index cb065522e..184723ed7 100644 --- a/server/swagger/openapi.json +++ b/server/swagger/openapi.json @@ -7,7 +7,7 @@ }, "servers": [ { - "url": "http:///api/" + "url": "/api" } ], "paths": { -- GitLab