Spaces:
Running
Running
Download explorer.js from GEOgod/goglobal-api-explorer: direct link, hf CLI and curl.
- Browser
- Download file 5.29 kB
-
https://huggingface.co/spaces/GEOgod/goglobal-api-explorer/resolve/main/explorer.js
- Command line
-
hf download hf://spaces/GEOgod/goglobal-api-explorer/explorer.js
-
curl -L -o explorer.js https://huggingface.co/spaces/GEOgod/goglobal-api-explorer/resolve/main/explorer.js
5.29 kB
| 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); | |