Skip to content

Commit c6b02eb

Browse files
authored
fix: respect http_proxy env vars in CLI API client (#695)
1 parent 26e593b commit c6b02eb

1 file changed

Lines changed: 38 additions & 1 deletion

File tree

cli/src/utils/codebuff-api.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ export interface CodebuffApiClientConfig {
103103
defaultTimeoutMs?: number
104104
/** Default retry configuration */
105105
retry?: RetryConfig
106+
/**
107+
* Proxy URL to use for all requests.
108+
* If not set, falls back to HTTPS_PROXY / https_proxy / HTTP_PROXY / http_proxy
109+
* environment variables. Set to null to explicitly disable proxy even if env
110+
* vars are present.
111+
*/
112+
proxy?: string | null
106113
}
107114

108115
/**
@@ -195,6 +202,23 @@ export interface CodebuffApiClient {
195202
feedback(req: FeedbackRequest): Promise<ApiResponse<FeedbackResponse>>
196203
}
197204

205+
/**
206+
* Resolve the proxy URL from standard environment variables.
207+
* Priority: HTTPS_PROXY > https_proxy > HTTP_PROXY > http_proxy
208+
* Returns undefined when no proxy is configured.
209+
*/
210+
export function resolveProxyUrl(
211+
env: Record<string, string | undefined> = process.env,
212+
): string | undefined {
213+
return (
214+
env['HTTPS_PROXY'] ||
215+
env['https_proxy'] ||
216+
env['HTTP_PROXY'] ||
217+
env['http_proxy'] ||
218+
undefined
219+
)
220+
}
221+
198222
/**
199223
* Sleep for a given duration
200224
*/
@@ -253,8 +277,16 @@ export function createCodebuffApiClient(
253277
fetch: fetchFn = fetch,
254278
defaultTimeoutMs = 30000,
255279
retry: defaultRetryConfig = {},
280+
proxy: proxyConfig,
256281
} = config
257282

283+
// Resolve proxy: explicit config wins, then env vars, then no proxy.
284+
// Pass proxy: null to explicitly disable even when env vars are set.
285+
const proxyUrl: string | undefined =
286+
proxyConfig === null
287+
? undefined
288+
: (proxyConfig ?? resolveProxyUrl())
289+
258290
const mergedDefaultRetry: Required<RetryConfig> = {
259291
...DEFAULT_RETRY_CONFIG,
260292
...defaultRetryConfig,
@@ -321,7 +353,12 @@ export function createCodebuffApiClient(
321353
const response = await fetchFn(url, {
322354
...fetchOptions,
323355
signal: controller.signal,
324-
})
356+
// Bun supports a `proxy` option on fetch. When a proxy URL is
357+
// resolved (from config or env vars) we pass it here so that all
358+
// API calls are tunnelled through the proxy. The cast is required
359+
// because the WhatWG RequestInit type does not include `proxy`.
360+
...(proxyUrl ? { proxy: proxyUrl } : {}),
361+
} as RequestInit)
325362

326363
clearTimeout(timeoutId)
327364

0 commit comments

Comments
 (0)