UNPKG

@scalar/api-client

Version:

the open source API testing client

32 lines (31 loc) 1.1 kB
import { snippetz as l } from "@scalar/snippetz"; const i = "ws://replace.me", c = (a, t, r) => { var o; try { if (!r.url) return [new Error("Please enter a URL to see a code snippet"), null]; const e = r.url.startsWith("/") ? "" : "/"; try { new URL(r.url); } catch (n) { console.error("[getSnippet] Invalid URL", n), r.url = `${i}${e}${r.url}`; } if (((o = r.postData) == null ? void 0 : o.mimeType) === "application/json") try { JSON.parse(r.postData.text || "{}"); } catch (n) { return console.error("[getSnippet] Invalid JSON body", n), [new Error("Invalid JSON body"), null]; } const p = a.replace("javascript", "js"); if (l().hasPlugin(p, t)) { const n = l().print(p, t, r); return n ? [null, n.replace(`${i}${e}`, "")] : [new Error("Error generating snippet"), null]; } } catch (e) { return console.error("[getSnippet] Error generating snippet", e), [new Error("Error generating snippet"), null]; } return [new Error("No snippet found"), null]; }; export { c as getSnippet };