File size: 5,293 Bytes
f6a4ae7
 
 
 
7dc43d2
f6a4ae7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7dc43d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const query = document.querySelector("#query");
const category = document.querySelector("#category");
const results = document.querySelector("#results");
const status = document.querySelector("#status");
const recipeList = document.querySelector("#recipes");
let operations = [];

function render() {
  const terms = query.value.trim().toLowerCase().split(/\s+/).filter(Boolean);
  const visible = operations.filter((operation) => {
    const text = [operation.operation_id, operation.method, operation.path, operation.summary, operation.category]
      .join(" ").toLowerCase();
    return (!category.value || operation.category === category.value)
      && terms.every((term) => text.includes(term));
  });
  const list = document.createDocumentFragment();
  for (const operation of visible) {
    const item = document.createElement("li");
    const link = document.createElement("a");
    const route = document.createElement("code");
    route.textContent = operation.method + " " + operation.path;
    const documentation = new URL(operation.source);
    if (documentation.origin !== "https://www.goglobal.to" || documentation.pathname !== "/docs/api") continue;
    link.href = documentation.href;
    link.target = "_blank";
    link.rel = "noopener noreferrer";
    link.append(route);
    const summary = document.createElement("p");
    summary.textContent = operation.summary;
    item.append(link, summary);
    list.append(item);
  }
  results.replaceChildren(list);
  status.textContent = visible.length ? `${visible.length} of ${operations.length} operations` : "No matching operations.";
}

async function load() {
  try {
    const response = await fetch("./endpoints.jsonl");
    if (!response.ok) throw new Error("Could not load the data file");
    operations = (await response.text()).split("\n").filter(Boolean).map((line) => JSON.parse(line));
    for (const name of [...new Set(operations.map((operation) => operation.category))].sort()) {
      category.add(new Option(name, name));
    }
    query.addEventListener("input", render);
    category.addEventListener("change", render);
    render();
  } catch {
    status.textContent = "The endpoint catalog could not be loaded. Open the official API docs above.";
  }
}

function linkTo(label, rawUrl, allowedOrigin) {
  const url = new URL(rawUrl);
  if (url.origin !== allowedOrigin) throw new Error("Unexpected workflow link");
  const link = document.createElement("a");
  link.textContent = label;
  link.href = url.href;
  link.target = "_blank";
  link.rel = "noopener noreferrer";
  return link;
}

function recipeCard(recipe, operationsById) {
  const card = document.createElement("article");
  const title = document.createElement("h3");
  title.textContent = recipe.title;
  const description = document.createElement("p");
  description.textContent = recipe.description;
  const links = document.createElement("div");
  links.className = "recipe-links";
  links.append(
    linkTo("View sample output", recipe.example, "https://github.com"),
    linkTo("GitHub repository", recipe.repository, "https://github.com"),
    linkTo(recipe.product_label, recipe.product, "https://www.goglobal.to")
  );
  const endpoints = document.createElement("div");
  endpoints.className = "recipe-endpoints";
  endpoints.setAttribute("aria-label", "Operations used");
  for (const id of recipe.operations) {
    const operation = operationsById.get(id);
    if (!operation) throw new Error("Unknown workflow operation");
    endpoints.append(linkTo(operation.operation_id, operation.source, "https://www.goglobal.to"));
  }
  const pre = document.createElement("pre");
  const code = document.createElement("code");
  code.textContent = recipe.commands.join("\n");
  pre.append(code);
  const copy = document.createElement("button");
  copy.type = "button";
  copy.textContent = "Copy commands";
  copy.setAttribute("aria-label", "Copy commands for " + recipe.title);
  const feedback = document.createElement("span");
  feedback.className = "copy-feedback";
  feedback.setAttribute("role", "status");
  const controls = document.createElement("div");
  controls.className = "recipe-controls";
  controls.append(copy, feedback);
  copy.addEventListener("click", async () => {
    try {
      await navigator.clipboard.writeText(code.textContent);
      feedback.textContent = "Copied.";
    } catch {
      const selection = window.getSelection();
      const range = document.createRange();
      range.selectNodeContents(code);
      selection.removeAllRanges();
      selection.addRange(range);
      feedback.textContent = "Commands selected. Use your copy shortcut.";
    }
  });
  card.append(title, description, links, endpoints, pre, controls);
  return card;
}

async function loadRecipes() {
  try {
    const response = await fetch("./recipes.json");
    if (!response.ok) throw new Error("Could not load workflow file");
    const recipes = await response.json();
    const operationsById = new Map(operations.map((operation) => [operation.operation_id, operation]));
    recipeList.replaceChildren(...recipes.map((recipe) => recipeCard(recipe, operationsById)));
  } catch {
    recipeList.textContent = "Workflows could not be loaded. The workflow JSON is available below.";
  }
}

load().then(loadRecipes);