From f02f4f70d24daf38c120ff47c77ef4ed2066d08e Mon Sep 17 00:00:00 2001 From: Nicolas Froidure Date: Sat, 28 May 2022 21:34:36 +0200 Subject: [PATCH] fix(swagger): fix swagger ui URL setup --- .../src/__snapshots__/index.test.ts.snap | 2 +- .../src/__snapshots__/index.test.ts.snap | 10 +-- packages/whook-swagger-ui/src/index.ts | 65 ++++++++++++++----- 3 files changed, 50 insertions(+), 27 deletions(-) diff --git a/packages/whook-example/src/__snapshots__/index.test.ts.snap b/packages/whook-example/src/__snapshots__/index.test.ts.snap index ce16bf44..cafee4cc 100644 --- a/packages/whook-example/src/__snapshots__/index.test.ts.snap +++ b/packages/whook-example/src/__snapshots__/index.test.ts.snap @@ -859,7 +859,7 @@ Object { "🎙️ - HTTP Server listening at \\"http://localhost:9999\\".", ], Array [ - "💁 - Serving the public API docs: http://localhost:9999/docs?url=http%3A%2F%2Flocalhost%3A9999%2Fv4%2FopenAPI", + "💁 - Serving the API docs: http://localhost:9999/docs", ], Array [ "🔂 - Running in \\"test\\" environment.", diff --git a/packages/whook-swagger-ui/src/__snapshots__/index.test.ts.snap b/packages/whook-swagger-ui/src/__snapshots__/index.test.ts.snap index 7cccf8e7..de17dd74 100644 --- a/packages/whook-swagger-ui/src/__snapshots__/index.test.ts.snap +++ b/packages/whook-swagger-ui/src/__snapshots__/index.test.ts.snap @@ -268,10 +268,7 @@ Object { "🔂 - Running in \\"test\\" environment.", ], Array [ - "💁 - Serving the public API docs: http://localhost:22224/docs?url=http%3A%2F%2Flocalhost%3A22224%2Fv1%2FopenAPI", - ], - Array [ - "💁 - Serving the private API docs: http://localhost:22224/docs?url=http%3A%2F%2Flocalhost%3A22224%2Fv1%2FopenAPI%3Faccess_token%3Doudelali", + "💁 - Serving the API docs: http://localhost:22224/docs", ], Array [ "🎙️ - HTTP Server listening at \\"http://localhost:22224\\".", @@ -422,10 +419,7 @@ Object { "🔂 - Running in \\"test\\" environment.", ], Array [ - "💁 - Serving the public API docs: http://localhost:22222/docs?url=http%3A%2F%2Flocalhost%3A22222%2Fv1%2FopenAPI", - ], - Array [ - "💁 - Serving the private API docs: http://localhost:22222/docs?url=http%3A%2F%2Flocalhost%3A22222%2Fv1%2FopenAPI%3Faccess_token%3Doudelali", + "💁 - Serving the API docs: http://localhost:22222/docs", ], Array [ "🎙️ - HTTP Server listening at \\"http://localhost:22222\\".", diff --git a/packages/whook-swagger-ui/src/index.ts b/packages/whook-swagger-ui/src/index.ts index 40d280db..884efcd3 100644 --- a/packages/whook-swagger-ui/src/index.ts +++ b/packages/whook-swagger-ui/src/index.ts @@ -10,6 +10,7 @@ import type { } from '@whook/whook'; import type { LogService } from 'common-services'; import type ECStatic from 'ecstatic'; +import type { IncomingMessage, ServerResponse } from 'http'; export { initGetOpenAPI, getOpenAPIDefinition }; @@ -79,40 +80,68 @@ export default function wrapHTTPRouterWithSwaggerUI( const localURL = `http://${HOST}:${PORT}`; const swaggerUIURL = `${localURL}/docs`; + const absolutePath = (await importer('swagger-ui-dist')).absolutePath(); const publicSwaggerURL = `${localURL}${BASE_PATH || ''}${ getOpenAPIDefinition.path }`; const staticRouter = (await importer('ecstatic')).default({ - root: (await importer('swagger-ui-dist')).absolutePath(), + root: absolutePath, showdir: false, baseDir: './docs', }); - log( - 'warning', - `💁 - Serving the public API docs: ${swaggerUIURL}?url=${encodeURIComponent( - publicSwaggerURL, - )}`, - ); + log('warning', `💁 - Serving the API docs: ${swaggerUIURL}`); - if (DEV_ACCESS_TOKEN) { - log( - 'warning', - `💁 - Serving the private API docs: ${swaggerUIURL}?url=${encodeURIComponent( - publicSwaggerURL + + const initializerContent = ` +window.onload = function() { + // + + // the following lines will be replaced by docker/configurator, when it runs in a docker-container + window.ui = SwaggerUIBundle({ + urls: [{"name":"Public API","url":"${publicSwaggerURL}"}${ + DEV_ACCESS_TOKEN + ? `, {"name":"Private API","url":"${ + publicSwaggerURL + '?access_token=' + - encodeURIComponent(DEV_ACCESS_TOKEN), - )}`, - ); - } + encodeURIComponent(DEV_ACCESS_TOKEN) + }"}` + : '' + }], + dom_id: '#swagger-ui', + deepLinking: true, + presets: [ + SwaggerUIBundle.presets.apis, + SwaggerUIStandalonePreset + ], + plugins: [ + SwaggerUIBundle.plugins.DownloadUrl, + SwaggerUIBundle.plugins.Topbar + ], + layout: "StandaloneLayout" + }); + + // +}; +`; return { ...httpRouter, service: customHTTPRouter, }; - async function customHTTPRouter(req, res) { - if (req.url.startsWith('/docs')) { + async function customHTTPRouter( + req: IncomingMessage, + res: ServerResponse, + ) { + if (req.url && req.url.startsWith('/docs/swagger-initializer.js')) { + res + .writeHead(200, { + 'Content-Type': 'text/javascript', + }) + .end(initializerContent); + return; + } + if (req.url && req.url.startsWith('/docs')) { return staticRouter(req, res); } return httpRouter.service(req, res);