@scalar/api-client
Version:
the open source API testing client
47 lines (46 loc) • 1.83 kB
JavaScript
import { isDefined as p } from "@scalar/helpers/array/is-defined";
import { objectKeys as m } from "@scalar/helpers/object/object-keys";
import { replaceEnvVariables as s } from "@scalar/helpers/regex/replace-variables";
import { getResolvedRef as d } from "@scalar/workspace-store/helpers/get-resolved-ref";
import { xScalarCookieSchema as S } from "@scalar/workspace-store/schemas/extensions/general/x-scalar-cookies";
import { coerceValue as x } from "@scalar/workspace-store/schemas/typebox-coerce";
import { encode as y } from "js-base64";
const k = (n, f) => f.flatMap(
(r) => m(r).flatMap((i) => {
const o = d(n?.[i]);
return o || [];
})
) ?? [], g = (n, f, r = {}, i = "") => {
const o = {}, u = [], l = new URLSearchParams();
return k(n, f).forEach((t) => {
if (t.type === "apiKey") {
const e = s(t.name, r), a = s(t["x-scalar-secret-token"], r) || i;
t.in === "header" && (o[e] = a), t.in === "query" && l.append(e, a), t.in === "cookie" && u.push(
x(S, {
name: e,
value: a,
path: "/"
})
);
}
if (t.type === "http")
if (t.scheme === "basic") {
const e = s(t["x-scalar-secret-username"], r), a = s(t["x-scalar-secret-password"], r), c = `${e}:${a}`;
o.Authorization = `Basic ${c === ":" ? "username:password" : y(c)}`;
} else {
const e = s(t["x-scalar-secret-token"], r);
o.Authorization = `Bearer ${e || i}`;
}
if (t.type === "oauth2") {
const e = Object.values(t.flows), a = s(
e.filter(p).find((c) => c["x-scalar-secret-token"])?.["x-scalar-secret-token"] ?? "",
r
);
o.Authorization = `Bearer ${a || i}`;
}
}), { headers: o, cookies: u, urlParams: l };
};
export {
g as buildRequestSecurity,
k as getSelectedSecuritySchemes
};