All files / src/lib openapi-servers.ts

100% Statements 10/10
100% Branches 2/2
100% Functions 4/4
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38                              4x 4x       3x   15x 12x 3x 12x   3x 3x                  
/**
 * The OpenAPI document with the hub the reader is on as its first server.
 *
 * ui/public/openapi.yaml lists the Azure demo first, and Swagger UI sends
 * "Try it out" to the first server. A reader on a local stack, signed in
 * there, therefore called https://ehds.mabu.red from the Swagger page: another
 * origin, which gets no session cookie and answers 401 or is blocked by the
 * browser (found on 2026-11-11 by trying it). Scalar has `baseServerURL` for
 * this; Swagger UI has no such option, so the page rewrites the `servers`
 * block of the YAML text before Swagger UI reads it.
 *
 * Works on the text, not on a parsed document: the UI has no YAML parser of
 * its own, and the block has one shape, a list of `- url:` entries.
 */
export function specForThisHub(spec: string, origin: string): string {
  const block = /^servers:\n((?:[ \t]+.*\n|\n)*)/m.exec(spec);
  if (!block) return spec;
 
  // Entries start at "  - url:"; comment lines above one belong to the block
  // as a whole and are dropped, since the note they carry is about the order.
  const entries = block[1]
    .split(/^(?=[ \t]*- url:)/m)
    .filter((e) => /^[ \t]*- url:/.test(e))
    .map((e) => e.replace(/\n+$/, "\n"));
  const others = entries.filter(
    (e) => /- url:\s*(\S+)/.exec(e)?.[1].replace(/^["']|["']$/g, "") !== origin,
  );
  const here = `  - url: ${origin}\n    description: This hub, where you are signed in\n`;
  return (
    spec.slice(0, block.index) +
    "servers:\n" +
    here +
    others.join("") +
    "\n" +
    spec.slice(block.index + block[0].length)
  );
}