@scalar/api-client
Version:
the open source API testing client
59 lines (58 loc) • 1.57 kB
JavaScript
import { replaceTemplateVariables as f } from "../string-template.js";
import { cookieSchema as p } from "@scalar/oas-utils/entities/cookie";
import { shouldUseProxy as $ } from "@scalar/oas-utils/helpers";
const l = "/";
function C({
example: a,
env: t,
globalCookies: o,
serverUrl: e,
proxyUrl: r
}) {
const n = [], h = $(r, e), i = W(
h ? r : e ?? "http://localhost"
);
return o.forEach((s) => {
const { name: m, value: u, domain: c, ...d } = s;
!k(e, c) || !m || n.push(
p.parse({
name: m,
value: u,
domain: c,
path: d.path
})
);
}), a.parameters.cookies.forEach((s) => {
!s.enabled || !s.key || n.push(
p.parse({
name: s.key,
value: f(s.value, t),
domain: i,
path: l
})
);
}), {
cookieParams: n
};
}
const W = (a) => {
const t = new URL(a.startsWith("http") ? a : `http://${a}`).hostname;
return t.match(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) || t.match(/^[a-fA-F0-9:]+$/) || t.startsWith(".") ? t : `.${t}`;
}, k = (a, t) => {
if (!a || !t)
return !0;
try {
const o = a.startsWith("http") ? a : `http://${a}`, e = new URL(o).hostname, r = !t, n = t === e, h = t.startsWith(".") && t === `.${e}`, i = t.startsWith(".") && e?.endsWith(t);
return r || n || i || h;
} catch {
return !1;
}
}, D = (a, t) => {
const o = a.map((e) => `${e.name}=${e.value}`).join("; ");
return t ? `${t}; ${o}`.trim() : o.trim();
};
export {
D as getCookieHeader,
k as matchesDomain,
C as setRequestCookies
};