Skip to content

Instantly share code, notes, and snippets.

@kaiobrito
Last active November 12, 2021 21:27
Show Gist options
  • Save kaiobrito/1204d39c966c9ea44d7829372141d13a to your computer and use it in GitHub Desktop.
Save kaiobrito/1204d39c966c9ea44d7829372141d13a to your computer and use it in GitHub Desktop.
import merge from "lodash.merge";
import fetch from "node-fetch";
const fetchSchemas = (urls) => {
const requests = urls.map((url) =>
fetch(url)
.then((res) => res.json())
.catch(() => ({}))
);
return Promise.all(requests);
};
export const mergeSchemas = (urls) =>
fetchSchemas(urls).then((schemas) => merge({}, ...schemas));
import express from "express";
import { createProxyMiddleware } from "http-proxy-middleware";
const app = express();
const FOO_API_HOST = "http://localhost:8001";
const BAR_API_HOST = "http://localhost:8002";
const fooProxy = createProxyMiddleware("/api", {
target: FOO_API_HOST,
});
const barProxy = createProxyMiddleware("/api", {
target: BAR_API_HOST,
});
app.use("/api/foo", fooProxy);
app.use("/api/bar", barProxy);
app.get("/api/schema", async (req, res) => {
res.json(
await mergeSchemas([
`${FOO_API_HOST}/api/schema?format=openapi-json`,
`${BAR_API_HOST}/api/schema?format=openapi-json`,
])
);
});
<!-- HTML for static distribution bundle build -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Swagger UI</title>
<link
rel="stylesheet"
type="text/css"
href="https://unpkg.com/swagger-ui-dist@3.52.5/swagger-ui.css"
/>
<style>
html {
box-sizing: border-box;
overflow: -moz-scrollbars-vertical;
overflow-y: scroll;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
body {
margin: 0;
background: #fafafa;
}
</style>
</head>
<body>
<div id="swagger-ui"></div>
<script
src="https://unpkg.com/swagger-ui-dist@3/swagger-ui-bundle.js"
charset="UTF-8"
></script>
<script
src="https://unpkg.com/swagger-ui-dist@3.52.5/swagger-ui-standalone-preset.js"
charset="UTF-8"
></script>
<script>
window.onload = function () {
// Begin Swagger UI call region
const ui = SwaggerUIBundle({
url: "/api/schema/",
dom_id: "#swagger-ui",
deepLinking: true,
presets: [SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset],
plugins: [SwaggerUIBundle.plugins.DownloadUrl],
layout: "StandaloneLayout",
});
// End Swagger UI call region
window.ui = ui;
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment