@scalar/api-client
Version:
the open source API testing client
60 lines (59 loc) • 1.7 kB
JavaScript
import { getResolvedRef as y } from "@scalar/workspace-store/helpers/get-resolved-ref";
import { processBody as f } from "./process-body.js";
import { processParameters as l } from "./process-parameters.js";
import { processSecuritySchemes as S } from "./process-security-schemes.js";
import { processServerUrl as g } from "./process-server-url.js";
const b = ({
operation: t,
contentType: a,
method: h,
path: i,
server: n,
example: p,
securitySchemes: m
}) => {
const e = {
method: h,
url: i,
headers: [],
queryString: [],
postData: void 0,
httpVersion: "HTTP/1.1",
cookies: [],
headersSize: -1,
bodySize: -1
};
if (n?.url && (e.url = g(n, i)), t.parameters) {
const { url: r, headers: o, queryString: s, cookies: c } = l({
harRequest: e,
parameters: t.parameters,
example: p,
contentType: a
});
e.url = r, e.headers = o, e.queryString = s, e.cookies = c;
}
const u = y(t.requestBody);
if (u?.content) {
const r = f({ requestBody: u, contentType: a, example: p });
if (e.postData = r, e.bodySize = r.text?.length ?? -1, r.mimeType) {
const o = e.headers.find(
(s) => s.name.toLowerCase() === "content-type"
);
o && !o.value ? o.value = r.mimeType : e.headers.push({
name: "Content-Type",
value: r.mimeType
});
}
}
if (m) {
const { headers: r, queryString: o, cookies: s } = S(m);
e.headers.push(...r), e.queryString.push(...o), e.cookies.push(...s);
}
let d = 0;
for (const r of e.headers)
d += (r.name?.length ?? 0) + 2 + (r.value?.length ?? 0) + 2;
return e.headersSize = d, e;
};
export {
b as operationToHar
};