UNPKG

@scalar/api-client

Version:

the open source API testing client

36 lines (35 loc) 1.24 kB
import { replaceTemplateVariables as n } from "../string-template.js"; import { cookieSchema as l } from "@scalar/oas-utils/entities/cookie"; import { isDefined as d } from "@scalar/oas-utils/helpers"; import { encode as c } from "js-base64"; const y = (p = [], i = {}, s = "") => { const t = {}, e = [], f = new URLSearchParams(); return p.forEach((o) => { if (o.type === "apiKey") { const a = n(o.value, i) || s; o.in === "header" && (t[o.name] = a), o.in === "query" && f.append(o.name, a), o.in === "cookie" && e.push( l.parse({ uid: o.uid, name: o.name, value: a, path: "/" }) ); } if (o.type === "http") if (o.scheme === "basic") { const a = n(o.username, i), u = n(o.password, i), r = `${a}:${u}`; t.Authorization = `Basic ${r === ":" ? "username:password" : c(r)}`; } else { const a = n(o.token, i); t.Authorization = `Bearer ${a || s}`; } if (o.type === "oauth2") { const u = Object.values(o.flows).filter(d).find((r) => r.token)?.token; t.Authorization = `Bearer ${u || s}`; } }), { headers: t, cookies: e, urlParams: f }; }; export { y as buildRequestSecurity };