This commit is contained in:
Bojan Kucera 2025-06-06 22:20:35 -04:00
parent 0937651f67
commit 117ff56d1e
5 changed files with 11 additions and 14 deletions

View file

@ -3,8 +3,6 @@ import type {
HttpClientConfig,
RequestConfig,
HttpResponse,
HttpMethod,
ProxyConfig,
} from './types.js';
import {
HttpError,

View file

@ -8,17 +8,17 @@ export class ProxyManager {
* Create appropriate proxy agent based on configuration
*/
static createAgent(proxy: ProxyConfig): HttpsProxyAgent<string> | SocksProxyAgent {
const { type, host, port, username, password } = proxy;
const { protocol, host, port, username, password } = proxy;
let proxyUrl: string;
if (username && password) {
proxyUrl = `${type}://${encodeURIComponent(username)}:${encodeURIComponent(password)}@${host}:${port}`;
proxyUrl = `${protocol}://${encodeURIComponent(username)}:${encodeURIComponent(password)}@${host}:${port}`;
} else {
proxyUrl = `${type}://${host}:${port}`;
proxyUrl = `${protocol}://${host}:${port}`;
}
switch (type) {
switch (protocol) {
case 'http':
case 'https':
return new HttpsProxyAgent(proxyUrl);
@ -26,7 +26,7 @@ export class ProxyManager {
case 'socks5':
return new SocksProxyAgent(proxyUrl);
default:
throw new Error(`Unsupported proxy type: ${type}`);
throw new Error(`Unsupported proxy protocol: ${protocol}`);
}
}
/**

View file

@ -3,7 +3,7 @@ export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' |
// Proxy configuration
export interface ProxyConfig {
type: 'http' | 'https' | 'socks4' | 'socks5';
protocol: 'http' | 'https' | 'socks4' | 'socks5';
host: string;
port: number;
username?: string;
@ -120,7 +120,7 @@ export function validateProxyConfig(config: unknown): ProxyConfig {
}
return {
type: proxy.type as ProxyConfig['type'],
protocol: proxy.protocol as ProxyConfig['protocol'],
host: proxy.host.trim(),
port: proxy.port,
username: proxy.username as string | undefined,