Last active
November 12, 2021 21:27
-
-
Save kaiobrito/1204d39c966c9ea44d7829372141d13a to your computer and use it in GitHub Desktop.
Code copied from https://github.com/kaiobrito/multi-open-api-docs-sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`, | |
]) | |
); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 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/[email protected]/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/[email protected]/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