diff --git a/packages/fetch/src/fetch.ts b/packages/fetch/src/fetch.ts index b5b461647..fbf51f108 100644 --- a/packages/fetch/src/fetch.ts +++ b/packages/fetch/src/fetch.ts @@ -5,7 +5,7 @@ import { HttpsProxyAgent } from "https-proxy-agent"; import { BodyInit, RequestInit, Response } from "node-fetch"; import { getAgentOptions } from "./getAgentOptions.js"; import fetch from "./node-fetch-patch.js"; -import { getProxyFromEnv, shouldBypassProxy } from "./util.js"; +import { getProxy, shouldBypassProxy } from "./util.js"; const { http, https } = (followRedirects as any).default; @@ -92,10 +92,7 @@ export async function fetchwithRequestOptions( const agentOptions = getAgentOptions(requestOptions); // Get proxy from options or environment variables - let proxy = requestOptions?.proxy; - if (!proxy) { - proxy = getProxyFromEnv(url.protocol); - } + const proxy = getProxy(url.protocol, requestOptions); // Check if should bypass proxy based on requestOptions or NO_PROXY env var const shouldBypass = shouldBypassProxy(url.hostname, requestOptions); diff --git a/packages/fetch/src/util.ts b/packages/fetch/src/util.ts index 19fba2ba3..1a18d5b1e 100644 --- a/packages/fetch/src/util.ts +++ b/packages/fetch/src/util.ts @@ -18,6 +18,17 @@ export function getProxyFromEnv(protocol: string): string | undefined { } } +// Note that request options proxy (per model) takes precedence over environment variables +export function getProxy( + protocol: string, + requestOptions?: RequestOptions, +): string | undefined { + if (requestOptions?.proxy) { + return requestOptions.proxy; + } + return getProxyFromEnv(protocol); +} + export function getEnvNoProxyPatterns(): string[] { const envValue = process.env.NO_PROXY || process.env.no_proxy; if (envValue) {