All files / src/api/package docs.ts

0% Statements 0/24
0% Branches 0/14
0% Functions 0/3
0% Lines 0/24

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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84                                                                                                                                                                       
import type { Schema } from "jsii-docgen";
import { getAssetsPath } from "./util";
 
const getDocsSuffix = (
  language: string,
  extension: string,
  submodule?: string
): string => {
  return `/docs-${submodule ? `${submodule}-` : ""}${language}.${extension}`;
};
 
/**
 * Fetch markdown docs of a specific package from the backend.
 */
export const fetchMarkdownDocs = async (
  name: string,
  version: string,
  language: string,
  scope?: string,
  submodule?: string
): Promise<string> => {
  const docsSuffix = getDocsSuffix(language, "md", submodule);
 
  const markdownPath = `${getAssetsPath(name, version, scope)}${docsSuffix}`;
  const response = await fetch(markdownPath);
  if (!response.ok) {
    throw new Error(
      `Failed fetching documentation for ${markdownPath}: ${response.statusText}`
    );
  }
 
  // since CloudFront returns a 200 /index.html for missing documents,
  // we assert the expected docs content type to detect these errors.
  // TODO: switch to proper 404 responses in this case (requires backend changes)
  const expectedContentType = "text/markdown";
  const contentType = response.headers.get("Content-Type");
 
  // we check 'includes' and not 'equal' because the content type contains
  // charset as well (e.g text/markdown; charset=UTF-8)
  if (!contentType || !contentType.includes(expectedContentType)) {
    throw new Error(
      `Invalid content type: ${contentType}. Expected ${expectedContentType}"`
    );
  }
  return response.text();
};
 
/**
 * Fetch JSON docs of a specific package from the backend.
 */
export const fetchJsonDocs = async (
  name: string,
  version: string,
  language: string,
  scope?: string,
  submodule?: string
): Promise<Schema> => {
  const docsSuffix = getDocsSuffix(language, "json", submodule);
 
  const jsonPath = `${getAssetsPath(name, version, scope)}${docsSuffix}`;
  const response = await fetch(jsonPath);
  if (!response.ok) {
    throw new Error(
      `Failed fetching documentation for ${jsonPath}: ${response.statusText}`
    );
  }
 
  // since CloudFront returns a 200 /index.html for missing documents,
  // we assert the expected docs content type to detect these errors.
  // TODO: switch to proper 404 responses in this case (requires backend changes)
  const expectedContentType = "application/json";
  const contentType = response.headers.get("Content-Type");
 
  // we check 'includes' and not 'equal' because the content type contains
  // charset as well (e.g text/markdown; charset=UTF-8)
  if (!contentType || !contentType.includes(expectedContentType)) {
    throw new Error(
      `Invalid content type: ${contentType}. Expected ${expectedContentType}"`
    );
  }
 
  return response.json();
};