"use strict";
(() => {
const container = document.getElementById("docs-container");
if (!container) return;
function fail(error) {
const message = document.createElement("p");
message.lang = "en";
message.setAttribute("role", "alert");
message.textContent = "Unable to load API documentation.";
container.replaceChildren(message);
container.setAttribute("aria-busy", "false");
console.error("Documentation initialization failed", error);
}
async function initialize() {
const response = await fetch(new URL(container.dataset.optionsUrl, document.baseURI), {
credentials: "same-origin",
});
if (!response.ok) throw new Error(`Options request failed: ${response.status}`);
const options = await response.json();
const specUrl = new URL(container.dataset.specUrl, document.baseURI).href;
if (container.dataset.ui === "redoc") {
if (typeof window.Redoc?.init !== "function") throw new Error("ReDoc bundle is unavailable");
window.Redoc.init(specUrl, options, container, (error) => {
if (error) fail(error);
else container.setAttribute("aria-busy", "false");
});
} else if (container.dataset.ui === "rapidoc") {
if (!customElements.get("rapi-doc")) throw new Error("RapiDoc bundle is unavailable");
const viewer = document.createElement("rapi-doc");
for (const [name, value] of Object.entries(options)) {
if (name === "filter-operations") viewer.filterOperations = String(value);
else viewer.setAttribute(name, String(value));
}
container.append(viewer);
await viewer.loadSpec(specUrl);
if (viewer.loadFailed || !viewer.resolvedSpec || viewer.resolvedSpec.specLoadError) {
throw new Error("RapiDoc could not load the specification");
}
while ((await viewer.updateComplete) === false) { }
if (viewer.gotoPath && !window.location.hash) viewer.scrollToPath(viewer.gotoPath);
container.querySelector('[role="status"]')?.remove();
container.setAttribute("aria-busy", "false");
} else {
throw new Error("Unknown documentation renderer");
}
}
initialize().catch(fail);
})();