/** * When running in Node.js, this function configures the global fetch * implementation to use a proxy if one is set in the environment. * * It must be called before any SDK methods are called. * * This is useful when running a tool like mitmproxy to inspect HTTP traffic * during development. */ export async function configureProxy() { // Bun's fetch client automatically recognises environment variables for // proxy configuration. if ("Bun" in globalThis) { return; } const proxyKey = [ "HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy", ].find((k) => process.env[k]); if (!proxyKey) { return; } const proxy = process.env[proxyKey]; if (!proxy) { return; } console.log("Using proxy:", proxy); const undici = await import("undici"); const proxyAgent = new undici.ProxyAgent(proxy); undici.setGlobalDispatcher(proxyAgent); }