UNPKG

@scalar/api-client

Version:

the open source API testing client

57 lines (56 loc) 1.97 kB
import { replaceEnvVariables as m } from "@scalar/helpers/regex/replace-variables"; import { getResolvedRef as u } from "@scalar/workspace-store/helpers/get-resolved-ref"; import { xScalarCookieSchema as P } from "@scalar/workspace-store/schemas/extensions/general/x-scalar-cookies"; import { coerceValue as y } from "@scalar/workspace-store/schemas/typebox-coerce"; import { getDelimiter as c } from "./get-delimiter.js"; import { getExample as v } from "./get-example.js"; const $ = (f = [], n = {}, l = "default") => { const a = []; let i = "application/json"; for (const t of f) { const e = u(t); a.push(e), e.in === "header" && e.name.toLowerCase() === "content-type" && "examples" in e && (i = u(e?.examples?.[l])?.value ?? i); } return a.reduce( (t, e) => { const s = v(e, l, i); if (!s || s["x-disabled"]) return t; const r = typeof s.value == "string" ? m(s.value, n) : s.value, o = m(e.name, n); if (e.in === "header") { if (o.toLowerCase() === "content-type" && r === "multipart/form-data") return t; t.headers[o] ? t.headers[o] += `,${r}` : t.headers[o] = r; } if (e.in === "path" && (t.pathVariables[o] = encodeURIComponent(r)), e.in === "query") { const p = "explode" in e && e.explode !== void 0 ? e.explode : !0, h = "style" in e && e.style ? e.style : "form"; if (p) t.urlParams.append(o, r); else { const d = t.urlParams.get(o); if (d) { const x = c(h); t.urlParams.set(o, `${d}${x}${r}`); } else t.urlParams.set(o, r); } } return e.in === "cookie" && t.cookies.push( y(P, { name: o, value: r, path: "/" }) ), t; }, { cookies: [], headers: {}, pathVariables: {}, urlParams: new URLSearchParams() } ); }; export { $ as buildRequestParameters };