feat: add ANTHROPIC_BASE_URL proxy support with optional TLS skip - #20
Conversation
Add proxy module (src/proxy.ts) with three utilities: - resolveBaseUrl(): parses ANTHROPIC_BASE_URL, rejects non-HTTP protocols and URLs with embedded credentials - isInsecure(): reads ANTHROPIC_INSECURE, only active when BASE_URL is set - rewriteOrigin(): rewrites request origin while preserving path and query Integration into fetch.ts is minimal: - rewriteOrigin() wraps addBetaParam() to rewrite the request origin - isInsecure() conditionally adds Bun's tls option to skip TLS verification Both features are opt-in via environment variables and have zero impact when unset. Useful for users routing through LiteLLM, API gateways, monitoring proxies, or corporate environments with custom endpoints. Includes 24 tests covering URL parsing, credential rejection, TLS gating, origin rewriting for string/URL/Request inputs, and no-op behavior.
There was a problem hiding this comment.
A few things to fix before merging:
1. Retry path drops the TLS skip
Initial fetch gets tls: { rejectUnauthorized: false } but handleRetryableError doesn't carry it through. First request works against a self-signed proxy, retry breaks.
2. Path in base URL gets silently ignored
ANTHROPIC_BASE_URL=https://proxy.company.com/anthropic the /anthropic part disappears. Requests go to https://proxy.company.com/v1/messages with no warning. Either join the path or reject URLs with a path and log why.
3. Silent catch in rewriteOrigin
Catch block returns original input without logging. If rewriting fails, requests silently go to api.anthropic.com. Add a log.warn.
4. No startup log when proxy is active
Only debug-level per-request log. Should have an info/warn at startup when proxy is configured.
5. Type cast
as RequestInit hides the Bun-specific tls property. Use BunFetchRequestInit.
- Carry TLS skip option through retry path in handleRetryableError - Reject ANTHROPIC_BASE_URL with path component (would be silently dropped) - Log warning when rewriteOrigin fails instead of silent fallthrough - Log proxy config at startup (info level) when ANTHROPIC_BASE_URL is set - Replace 'as RequestInit' with explicit BunFetchRequestInit type - Cache resolveBaseUrl result for process lifetime (avoid repeated parsing) - Add resetProxyCache() for test isolation
|
All five points addressed:
Also cached 116 tests pass, lint clean. |
|
Looks good |
|
🎉 This PR is included in version 1.5.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Summary
ANTHROPIC_BASE_URLsupport to route API requests through a custom proxy/gatewayANTHROPIC_INSECUREto optionally skip TLS verification (double-gated: only active whenANTHROPIC_BASE_URLis also set)src/proxy.tsmodule with 24 testsProblem
Users behind API gateways, corporate proxies, or monitoring layers (e.g. LiteLLM, custom logging proxies) cannot use the plugin because all requests go directly to
api.anthropic.com. There's no way to reroute traffic through an intermediary.Solution
Two opt-in environment variables:
ANTHROPIC_BASE_URLhttps://proxy.internal:8443ANTHROPIC_INSECURE1ortrueSafety design
ANTHROPIC_INSECUREis double-gated — it has no effect unlessANTHROPIC_BASE_URLis also set. This prevents accidentally disabling TLS against the production Anthropic API.user:pass@host) are rejected.Integration
The change to
fetch.tsis minimal (4 lines):New module:
src/proxy.tsresolveBaseUrl()ANTHROPIC_BASE_URLisInsecure()rewriteOrigin()Tests
24 new tests covering:
string,URL, andRequestinputsAll 115 tests pass (91 existing + 24 new):