98 lines
2.9 KiB
TypeScript
98 lines
2.9 KiB
TypeScript
import { RequestOptions } from "@continuedev/config-types";
|
|
import * as followRedirects from "follow-redirects";
|
|
import { HttpProxyAgent } from "http-proxy-agent";
|
|
import { HttpsProxyAgent } from "https-proxy-agent";
|
|
import fetch, { RequestInit, Response } from "node-fetch";
|
|
import { getAgentOptions } from "./getAgentOptions.js";
|
|
import { getProxyFromEnv, shouldBypassProxy } from "./util.js";
|
|
|
|
const { http, https } = (followRedirects as any).default;
|
|
|
|
export async function fetchwithRequestOptions(
|
|
url_: URL | string,
|
|
init?: RequestInit,
|
|
requestOptions?: RequestOptions,
|
|
): Promise<Response> {
|
|
const url = typeof url_ === "string" ? new URL(url_) : url_;
|
|
if (url.host === "localhost") {
|
|
url.host = "127.0.0.1";
|
|
}
|
|
|
|
const agentOptions = getAgentOptions(requestOptions);
|
|
|
|
// Get proxy from options or environment variables
|
|
let proxy = requestOptions?.proxy;
|
|
if (!proxy) {
|
|
proxy = getProxyFromEnv(url.protocol);
|
|
}
|
|
|
|
// Check if should bypass proxy based on requestOptions or NO_PROXY env var
|
|
const shouldBypass =
|
|
requestOptions?.noProxy?.includes(url.hostname) ||
|
|
shouldBypassProxy(url.hostname);
|
|
|
|
// Create agent
|
|
const protocol = url.protocol === "https:" ? https : http;
|
|
const agent =
|
|
proxy && !shouldBypass
|
|
? protocol === https
|
|
? new HttpsProxyAgent(proxy, agentOptions)
|
|
: new HttpProxyAgent(proxy, agentOptions)
|
|
: new protocol.Agent(agentOptions);
|
|
|
|
let headers: { [key: string]: string } = {};
|
|
for (const [key, value] of Object.entries(init?.headers || {})) {
|
|
headers[key] = value as string;
|
|
}
|
|
headers = {
|
|
...headers,
|
|
...requestOptions?.headers,
|
|
};
|
|
|
|
// Replace localhost with 127.0.0.1
|
|
if (url.hostname === "localhost") {
|
|
url.hostname = "127.0.0.1";
|
|
}
|
|
|
|
// add extra body properties if provided
|
|
let updatedBody: string | undefined = undefined;
|
|
try {
|
|
if (requestOptions?.extraBodyProperties && typeof init?.body === "string") {
|
|
const parsedBody = JSON.parse(init.body);
|
|
updatedBody = JSON.stringify({
|
|
...parsedBody,
|
|
...requestOptions.extraBodyProperties,
|
|
});
|
|
}
|
|
} catch (e) {
|
|
console.log("Unable to parse HTTP request body: ", e);
|
|
}
|
|
|
|
// fetch the request with the provided options
|
|
try {
|
|
const resp = await fetch(url, {
|
|
...init,
|
|
body: updatedBody ?? init?.body,
|
|
headers: headers,
|
|
agent: agent,
|
|
});
|
|
|
|
if (!resp.ok) {
|
|
const requestId = resp.headers.get("x-request-id");
|
|
if (requestId) {
|
|
console.log(`Request ID: ${requestId}, Status: ${resp.status}`);
|
|
}
|
|
}
|
|
|
|
return resp;
|
|
} catch (error) {
|
|
if (error instanceof Error && error.name === "AbortError") {
|
|
// Return a Response object that streamResponse etc can handle
|
|
return new Response(null, {
|
|
status: 499, // Client Closed Request
|
|
statusText: "Client Closed Request",
|
|
});
|
|
}
|
|
throw error;
|
|
}
|
|
}
|