footing 0.4.1

Feature-gated foundations for Rust backend services
Documentation
"use strict";

(() => {
  const container = document.getElementById("docs-container");
  if (!container) return;

  function fail(error) {
    const message = document.createElement("p");
    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");
      // These native attribute callbacks schedule loadSpec independently. Set
      // their public properties so initial configuration has one observed load.
      const filterProperties = new Map([
        ["match-paths", "matchPaths"],
        ["match-type", "matchType"],
        ["remove-endpoints-with-badge-label-as", "removeEndpointsWithBadgeLabelAs"],
      ]);
      // Set options before connecting: connectedCallback may load CDN fonts.
      for (const [name, value] of Object.entries(options)) {
        const property = filterProperties.get(name);
        if (property) viewer[property] = String(value);
        else viewer.setAttribute(name, String(value));
      }
      // Keep the URL for downloads without starting a second, unobserved load
      // through the spec-url attribute callback.
      viewer.specUrl = specUrl;
      container.append(viewer);
      await viewer.loadSpec(specUrl);
      // RapiDoc 9.x catches resolver errors instead of rejecting loadSpec().
      if (viewer.loadFailed || !viewer.resolvedSpec || viewer.resolvedSpec.specLoadError) {
        throw new Error("RapiDoc could not load the specification");
      }
      while ((await viewer.updateComplete) === false) { /* Wait for pending renders. */ }
      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);
})();