Skip to content

Commit

Permalink
fix(swagger): fix swagger ui URL setup
Browse files Browse the repository at this point in the history
  • Loading branch information
nfroidure committed May 28, 2022
1 parent d31b01c commit f02f4f7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
10 changes: 2 additions & 8 deletions packages/whook-swagger-ui/src/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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\\".",
Expand Down Expand Up @@ -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\\".",
Expand Down
65 changes: 47 additions & 18 deletions packages/whook-swagger-ui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Expand Down Expand Up @@ -79,40 +80,68 @@ export default function wrapHTTPRouterWithSwaggerUI<D>(

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() {
//<editor-fold desc="Changeable Configuration Block">
// 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"
});
//</editor-fold>
};
`;

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);
Expand Down

0 comments on commit f02f4f7

Please sign in to comment.