UNPKG

@scalar/api-client

Version:

the open source API testing client

59 lines (58 loc) 1.59 kB
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: e, serverUrl: o, proxyUrl: r }) { const n = [], h = $(r, o), i = W( h ? r : o ?? "http://localhost" ); return e.forEach((s) => { const { name: c, value: u, domain: m, ...d } = s; !k(o, m) || !c || n.push( p.parse({ name: c, value: u, domain: m, 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 e = a.startsWith("http") ? a : `http://${a}`, o = new URL(e).hostname, r = !t, n = t === o, h = t.startsWith(".") && t === `.${o}`, i = t.startsWith(".") && (o == null ? void 0 : o.endsWith(t)); return r || n || i || h; } catch { return !1; } }, D = (a, t) => { const e = a.map((o) => `${o.name}=${o.value}`).join("; "); return t ? `${t}; ${e}`.trim() : e.trim(); }; export { D as getCookieHeader, k as matchesDomain, C as setRequestCookies };