@scalar/api-client
Version:
the open source API testing client
87 lines (86 loc) • 2.9 kB
JavaScript
import { buildRequestSecurity as V } from "../../../../libs/send-request/build-request-security.js";
import { replaceTemplateVariables as A } from "../../../../libs/string-template.js";
import { REGEX as s } from "@scalar/oas-utils/helpers";
import { convertToHarRequest as L } from "./convert-to-har-request.js";
const v = "YOUR_SECRET_TOKEN", g = ({
operation: e,
example: u,
server: r,
securitySchemes: i = [],
environment: E
}) => {
const n = E && Array.isArray(E) ? Object.fromEntries(E.map((t) => [t.key, t.value])) : E || {}, T = (() => {
if (r != null && r.url && (s.VARIABLES.test(r.url) || s.PATH.test(r.url))) {
const t = Object.entries((r == null ? void 0 : r.variables) || {}).reduce(
(a, [d, l]) => {
var o, R;
const c = (R = (o = u == null ? void 0 : u.parameters) == null ? void 0 : o.path.find((b) => b.enabled && b.key === d)) == null ? void 0 : R.value;
return c ? a[d] = A(c, n) : l.default && (a[d] = A(l.default, n)), a;
},
{}
);
return A(A(r.url, n), t);
}
return r == null ? void 0 : r.url;
})(), P = (() => {
var a;
const t = (e == null ? void 0 : e.path) ?? "/";
if (t && (s.VARIABLES.test(t) || s.PATH.test(t))) {
const d = (((a = u == null ? void 0 : u.parameters) == null ? void 0 : a.path) ?? []).reduce((l, c) => (c.enabled && (l[c.key] = A(c.value, n)), l), {});
return A(A(t, n), d);
}
return t;
})(), f = V(i, n, v), y = [
...((u == null ? void 0 : u.parameters.headers) ?? []).map((t) => ({
...t,
value: s.VARIABLES.test(t.value) || s.PATH.test(t.value) ? A(t.value, n) : t.value
})) ?? [],
...Object.entries(f.headers).map(([t, a]) => ({
key: t,
value: a,
enabled: !0
}))
], S = [
...((u == null ? void 0 : u.parameters.cookies) ?? []).map((t) => ({
...t,
value: s.VARIABLES.test(t.value) || s.PATH.test(t.value) ? A(t.value, n) : t.value
})) ?? [],
...f.cookies.map((t) => ({
key: t.name,
value: t.value,
enabled: !0
}))
], h = [
...((u == null ? void 0 : u.parameters.query) ?? []).map((t) => ({
...t,
value: s.VARIABLES.test(t.value) || s.PATH.test(t.value) ? A(t.value, n) : t.value
})) ?? [],
...Array.from(f.urlParams.entries()).map(([t, a]) => ({
key: t,
value: a,
enabled: !0
}))
], H = (() => {
var a;
const t = u == null ? void 0 : u.body;
return (a = t == null ? void 0 : t.raw) != null && a.value && (s.VARIABLES.test(t.raw.value) || s.PATH.test(t.raw.value)) ? {
...t,
raw: {
...t.raw,
value: A(t.raw.value, n)
}
} : t;
})();
return L({
baseUrl: T,
method: (e == null ? void 0 : e.method) ?? "get",
path: P,
body: H,
cookies: S,
headers: y,
query: h
});
};
export {
g as getHarRequest
};