UNPKG

@scalar/api-client

Version:

the open source API testing client

67 lines (66 loc) 2.34 kB
import { replaceVariables as U } from "@scalar/helpers/regex/replace-variables"; import { mergeUrls as x } from "@scalar/helpers/url/merge-urls"; import { shouldUseProxy as I, redirectToProxy as T } from "@scalar/oas-utils/helpers"; import { getResolvedRef as V } from "@scalar/workspace-store/helpers/get-resolved-ref"; import { objectEntries as _ } from "@vueuse/core"; import { isElectron as k } from "../../../../libs/electron.js"; import { ERRORS as q, normalizeError as D } from "../../../../libs/errors.js"; import { buildRequestBody as j } from "./build-request-body.js"; import { buildRequestCookieHeader as z } from "./build-request-cookie-header.js"; import { buildRequestParameters as F } from "./build-request-parameters.js"; import { buildRequestSecurity as G } from "./build-request-security.js"; const ee = ({ environment: y, exampleKey: u = "default", globalCookies: h, method: v, operation: m, path: a, proxyUrl: c, server: d, securitySchemes: E, selectedSecurity: C }) => { try { const e = y.variables.reduce( (t, o) => (t[o.name] = typeof o.value == "string" ? o.value : o.value.default, t), {} ), A = _(d?.variables ?? {}).reduce( (t, [o, P]) => (P.default && (t[o] = P.default), t), {} ), i = U(d?.url ?? "", { ...e, ...A }), H = V(m.requestBody); if (!i && !a) throw q.URL_EMPTY; const s = F(m.parameters ?? [], e, u), S = j(H, e, u), n = G(E, C, e), r = { ...s.headers, ...n.headers }, w = new URLSearchParams([...s.urlParams, ...n.urlParams]), f = U(a, { ...e, ...s.pathVariables }), p = x(i, f, w), R = I(c, p), B = T(c, p), b = r["User-Agent"] || r["user-agent"]; k() && b && (r["X-Scalar-User-Agent"] = b); const l = z({ paramCookies: [...s.cookies, ...n.cookies], globalCookies: h, env: e, path: f, originalCookieHeader: r.Cookie || r.cookie, url: i || a, useCustomCookieHeader: k() || R }); l && (r[l.name] = l.value); const g = new AbortController(), L = new Request(B, { method: v, headers: r, signal: g.signal, body: S }); return [ null, { controller: g, isUsingProxy: R, request: L } ]; } catch (e) { return [D(e, q.BUILDING_REQUEST_FAILED), null]; } }; export { ee as buildRequest };