@scalar/api-client
Version:
the open source API testing client
71 lines (70 loc) • 2.77 kB
JavaScript
import { canMethodHaveBody as S } from "@scalar/helpers/http/can-method-have-body";
import { shouldUseProxy as w, redirectToProxy as A } from "@scalar/helpers/url/redirect-to-proxy";
import { getResolvedRef as B } from "@scalar/workspace-store/helpers/get-resolved-ref";
import { isElectron as U } from "../../../../libs/electron.js";
import { ERRORS as h, normalizeError as D } from "../../../../libs/errors.js";
import { getEnvironmentVariables as L } from "./get-environment-variables.js";
import { getResolvedUrl as T } from "./get-resolved-url.js";
import { getDefaultHeaders as V } from "../../request-block/helpers/get-default-headers.js";
import { buildRequestBody as I } from "./build-request-body.js";
import { buildRequestCookieHeader as _ } from "./build-request-cookie-header.js";
import { buildRequestParameters as x } from "./build-request-parameters.js";
import { buildRequestSecurity as F } from "./build-request-security.js";
const $ = ({
environment: c,
exampleKey: r = "default",
globalCookies: k,
method: n,
operation: o,
path: C,
proxyUrl: f,
server: H,
selectedSecuritySchemes: P
}) => {
try {
const d = B(o.requestBody), t = L(c), s = x(o.parameters ?? [], t, r), u = F(P, t), q = V({ method: n, operation: o, exampleKey: r, hideDisabledHeaders: !0 }).filter((l) => !l.isOverridden).reduce(
(l, g) => (l[g.name] = g.defaultValue, l),
{}
), e = new Headers({ ...q, ...s.headers, ...u.headers }), y = new URLSearchParams([...s.urlParams, ...u.urlParams]), a = S(n) ? I(d, t, r) : null;
a && (a instanceof FormData || a instanceof URLSearchParams) && e.delete("Content-Type");
const i = T({ environment: c, server: H, path: C, pathVariables: s.pathVariables, urlParams: y });
if (!i)
throw h.URL_EMPTY;
const p = w(f, i), E = A(f, i), R = e.get("User-Agent");
U() && R && e.set("X-Scalar-User-Agent", R);
const m = _({
paramCookies: [...s.cookies, ...u.cookies],
globalCookies: k,
env: t,
originalCookieHeader: e.get("Cookie"),
url: i,
useCustomCookieHeader: U() || p,
disabledGlobalCookies: o["x-scalar-disable-parameters"]?.["global-cookies"]?.[r] ?? {}
});
m && e.set(m.name, m.value);
const b = new AbortController(), v = new Request(E, {
/**
* Ensure that all methods are uppercased (though only needed for patch)
*
* @see https://github.com/whatwg/fetch/issues/50
*/
method: n.toUpperCase(),
headers: e,
signal: b.signal,
body: a
});
return [
null,
{
controller: b,
isUsingProxy: p,
request: v
}
];
} catch (d) {
return [D(d, h.BUILDING_REQUEST_FAILED), null];
}
};
export {
$ as buildRequest
};