@scalar/api-client
Version:
the open source API testing client
64 lines (63 loc) • 1.95 kB
JavaScript
import { mergeUrls as w } from "@scalar/oas-utils/helpers";
const D = ({
baseUrl: f = "",
method: u,
body: a,
path: d,
cookies: p,
headers: n,
query: m
}) => {
var l, s, c;
const g = w(f, d, void 0, !0), r = {
method: u.toUpperCase(),
url: g.toString(),
httpVersion: "HTTP/1.1",
headers: [],
queryString: [],
cookies: [],
headersSize: -1,
bodySize: -1
};
if (p.length && (r.cookies = p.filter((e) => e.enabled).map(({ key: e, value: t }) => ({
name: e,
value: t
}))), n.length && (r.headers = n.filter((e) => e.enabled && !(e.key.toLowerCase() === "accept" && e.value === "*/*")).map(({ key: e, value: t }) => ({
name: e.replace(/\b\w/g, (o) => o.toUpperCase()),
value: t
}))), m.length && (r.queryString = m.filter((e) => e.enabled).map(({ key: e, value: t }) => ({
name: e,
value: t
}))), a)
try {
const e = (l = n.find((t) => t.key.toLowerCase() === "content-type")) == null ? void 0 : l.value;
if (a.activeBody === "formData" && a.formData) {
const t = [];
a.formData.value.forEach(({ key: o, value: h, file: i, enabled: v }) => {
v && (i ? t.push({
name: o || "blob",
value: "BINARY",
fileName: i.name,
contentType: i.type || "application/octet-stream"
}) : t.push({
name: o,
value: h
}));
}), ((s = a.formData) == null ? void 0 : s.encoding) === "urlencoded" ? r.postData = {
mimeType: e || "application/x-www-form-urlencoded",
params: t
} : r.postData = {
mimeType: e || "multipart/form-data",
params: t
};
} else a.activeBody === "raw" && a.raw && (r.postData = {
mimeType: e || "application/json",
text: ((c = a.raw) == null ? void 0 : c.value) ?? ""
});
} catch {
}
return r;
};
export {
D as convertToHarRequest
};