UNPKG

@scalar/api-client

Version:

the open source API testing client

173 lines (172 loc) 5.96 kB
import { getResolvedRef as m } from "@scalar/workspace-store/helpers/get-resolved-ref"; import { getExampleFromSchema as S } from "../get-example-from-schema.js"; import { deSerializeParameter as k } from "../../../operation-block/helpers/de-serialize-parameter.js"; import { getExample as b } from "../../../operation-block/helpers/get-example.js"; import { serializeFormStyleForCookies as h, serializeSimpleStyle as v, serializeContentValue as p, serializeDeepObjectStyle as x, serializePipeDelimitedStyle as j, serializeSpaceDelimitedStyle as g, serializeFormStyle as z } from "../../../operation-block/helpers/serialize-parameter.js"; import { isParamDisabled as A } from "../../../request-block/helpers/is-param-disabled.js"; const P = (e) => (e ?? []).map((t) => m(t)), O = (e) => { if (e.in === "header") return { style: "simple", explode: "explode" in e && e.explode !== void 0 ? e.explode : !1 }; if (e.in === "cookie") return { style: "form", explode: "explode" in e && e.explode !== void 0 ? e.explode : !0 }; const t = { path: "simple", query: "form", header: "simple", cookie: "form" }[e.in], n = "style" in e && e.style ? e.style : t, d = "explode" in e && e.explode !== void 0 ? e.explode : n === "form"; return { style: n, explode: d }; }, w = (e, t, n) => { const d = b(e, t, n); if (A(e, d)) return; if (d?.value !== void 0) return k(d.value, e); if (!("schema" in e) || !e.schema) return; const s = e.in === "path" ? { emptyString: `{${e.name}}` } : {}; return S(m(e.schema), s); }, U = ({ harRequest: e, parameters: t, example: n }) => { const d = [...e.headers], s = [...e.queryString]; let r = e.url; const l = P(t); for (const i of l) { if (!i.in || !i.name) continue; const f = w(i, n, void 0); if (f === void 0) continue; const { style: u, explode: y } = O(i); switch (i.in) { case "path": { r = D(r, i, f, u, y); break; } case "query": { if ("content" in i && i.content) { const o = Object.keys(i.content)[0] ?? "application/json", c = p(f, o); s.push({ name: i.name, value: c }); break; } switch (u) { case "form": { const o = z(f, y); if (Array.isArray(o)) for (const c of o) { const $ = c.key || i.name; s.push({ name: $, value: String(c.value) }); } else s.push({ name: i.name, value: String(o) }); break; } case "spaceDelimited": { const o = g(f); s.push({ name: i.name, value: o }); break; } case "pipeDelimited": { const o = j(f); s.push({ name: i.name, value: o }); break; } case "deepObject": { if (y) { const o = x(i.name, f); for (const c of o) s.push({ name: c.key, value: c.value }); } break; } // Default to form style default: s.push({ name: i.name, value: String(f) }); } break; } // Headers only support 'simple' style according to OpenAPI 3.1.1 // For arrays, simple style always produces comma-separated values regardless of explode // The explode parameter only affects object serialization case "header": { const o = v(f, y); d.push({ name: i.name, value: String(o) }); break; } // Cookies only support 'form' style according to OpenAPI 3.1.1 case "cookie": { const o = h(f, y); if (Array.isArray(o)) for (const c of o) { const $ = c.key || i.name, a = c.value === null ? "null" : String(c.value); e.cookies.push({ name: $, value: a }); } else { const c = o === null ? "null" : String(o); e.cookies.push({ name: i.name, value: c }); } break; } } } return { url: r, headers: d, queryString: s, cookies: e.cookies }; }, D = (e, t, n, d, s) => { switch (d) { case "matrix": { if (s) { if (Array.isArray(n)) { const r = n.map((l) => `${t.name}=${l}`).join(";"); return e.replace(`{;${t.name}}`, `;${r}`); } if (typeof n == "object" && n !== null) { const r = Object.entries(n).map(([l, i]) => `${l}=${i}`).join(";"); return e.replace(`{;${t.name}}`, `;${r}`); } return e.replace(`{;${t.name}}`, `;${t.name}=${n}`); } if (Array.isArray(n)) return e.replace(`{;${t.name}}`, `;${t.name}=${n.join(",")}`); if (typeof n == "object" && n !== null) { const r = Object.entries(n).map(([l, i]) => `${l},${i}`).join(","); return e.replace(`{;${t.name}}`, `;${t.name}=${r}`); } return e.replace(`{;${t.name}}`, `;${t.name}=${n}`); } case "label": { if (s) { if (Array.isArray(n)) return e.replace(`{.${t.name}}`, `.${n.join(".")}`); if (typeof n == "object" && n !== null) { const r = Object.entries(n).map(([l, i]) => `${l}=${i}`).join("."); return e.replace(`{.${t.name}}`, `.${r}`); } return e.replace(`{.${t.name}}`, `.${n}`); } if (Array.isArray(n)) return e.replace(`{.${t.name}}`, `.${n.join(",")}`); if (typeof n == "object" && n !== null) { const r = Object.entries(n).map(([l, i]) => `${l},${i}`).join(","); return e.replace(`{.${t.name}}`, `.${r}`); } return e.replace(`{.${t.name}}`, `.${n}`); } case "simple": { const r = v(n, s); return e.replace(`{${t.name}}`, String(r)); } // Default to simple style default: return e.replace(`{${t.name}}`, String(n)); } }; export { U as processParameters };