@scalar/api-client
Version:
the open source API testing client
46 lines (45 loc) • 1.38 kB
JavaScript
import { snippetz as a } from "@scalar/snippetz";
import { getHarRequest as d } from "./get-har-request.js";
const s = "ws://replace.me", f = (i, t, {
operation: l,
example: u,
server: c,
securitySchemes: g = [],
environment: E
}) => {
var n;
try {
const r = d({
operation: l,
example: u,
server: c,
securitySchemes: g,
environment: E
});
if (!r.url)
return [new Error("Please enter a URL to see a code snippet"), null];
const o = r.url.startsWith("/") ? "" : "/";
try {
new URL(r.url);
} catch (e) {
console.error("[getSnippet] Invalid URL", e), r.url = `${s}${o}${r.url}`;
}
if (((n = r.postData) == null ? void 0 : n.mimeType) === "application/json")
try {
JSON.parse(r.postData.text || "{}");
} catch (e) {
return console.error("[getSnippet] Invalid JSON body", e), [new Error("Invalid JSON body"), null];
}
const p = i.replace("javascript", "js");
if (a().hasPlugin(p, t)) {
const e = a().print(p, t, r);
return e ? [null, e.replace(`${s}${o}`, "")] : [new Error("Error generating snippet"), null];
}
} catch (r) {
return console.error("[getSnippet] Error generating snippet", r), [new Error("Error generating snippet"), null];
}
return [new Error("No snippet found"), null];
};
export {
f as getSnippet
};