cleaned up random .js imports

This commit is contained in:
Bojan Kucera 2025-06-09 20:04:42 -04:00
parent 47109baff7
commit c172ecc6d3
19 changed files with 107 additions and 69 deletions

View file

@ -1,8 +1,8 @@
import axios, { type AxiosRequestConfig, type AxiosResponse } from 'axios';
import type { RequestConfig, HttpResponse } from '../types.js';
import type { RequestAdapter } from './types.js';
import { ProxyManager } from '../proxy-manager.js';
import { HttpError } from '../types.js';
import type { RequestConfig, HttpResponse } from '../types';
import type { RequestAdapter } from './types';
import { ProxyManager } from '../proxy-manager';
import { HttpError } from '../types';
/**
* Axios adapter for SOCKS proxies

View file

@ -1,7 +1,7 @@
import type { RequestConfig } from '../types.js';
import type { RequestAdapter } from './types.js';
import { FetchAdapter } from './fetch-adapter.js';
import { AxiosAdapter } from './axios-adapter.js';
import type { RequestConfig } from '../types';
import type { RequestAdapter } from './types';
import { FetchAdapter } from './fetch-adapter';
import { AxiosAdapter } from './axios-adapter';
/**
* Factory for creating the appropriate request adapter

View file

@ -1,7 +1,7 @@
import type { RequestConfig, HttpResponse } from '../types.js';
import type { RequestAdapter } from './types.js';
import { ProxyManager } from '../proxy-manager.js';
import { HttpError } from '../types.js';
import type { RequestConfig, HttpResponse } from '../types';
import type { RequestAdapter } from './types';
import { ProxyManager } from '../proxy-manager';
import { HttpError } from '../types';
/**
* Fetch adapter for HTTP/HTTPS proxies and non-proxy requests

View file

@ -1,4 +1,4 @@
export * from './types.js';
export * from './fetch-adapter.js';
export * from './axios-adapter.js';
export * from './factory.js';
export * from './types';
export * from './fetch-adapter';
export * from './axios-adapter';
export * from './factory';

View file

@ -1,4 +1,4 @@
import type { RequestConfig, HttpResponse } from '../types.js';
import type { RequestConfig, HttpResponse } from '../types';
/**
* Request adapter interface for different HTTP implementations

View file

@ -3,10 +3,10 @@ import type {
HttpClientConfig,
RequestConfig,
HttpResponse,
} from './types.js';
import { HttpError } from './types.js';
import { ProxyManager } from './proxy-manager.js';
import { AdapterFactory } from './adapters/index.js';
} from './types';
import { HttpError } from './types';
import { ProxyManager } from './proxy-manager';
import { AdapterFactory } from './adapters/index';
export class HttpClient {
private readonly config: HttpClientConfig;

View file

@ -1,8 +1,8 @@
// Re-export all types and classes
export * from './types.js';
export * from './client.js';
export * from './proxy-manager.js';
export * from './adapters/index.js';
export * from './types';
export * from './client';
export * from './proxy-manager';
export * from './adapters/index';
// Default export
export { HttpClient as default } from './client.js';
export { HttpClient as default } from './client';

View file

@ -0,0 +1,66 @@
import axios, { AxiosRequestConfig, type AxiosInstance } from 'axios';
import { SocksProxyAgent } from 'socks-proxy-agent';
import { HttpsProxyAgent } from 'https-proxy-agent';
import { HttpProxyAgent } from 'http-proxy-agent';
import type { ProxyInfo } from './types';
export class ProxyManager {
/**
* Determine if we should use Bun fetch (HTTP/HTTPS) or Axios (SOCKS)
*/
static shouldUseBunFetch(proxy: ProxyInfo): boolean {
return proxy.protocol === 'http' || proxy.protocol === 'https';
}
/**
* Create proxy URL for both Bun fetch and Axios proxy agents
*/
static createProxyUrl(proxy: ProxyInfo): string {
const { protocol, host, port, username, password } = proxy;
if (username && password) {
return `${protocol}://${encodeURIComponent(username)}:${encodeURIComponent(password)}@${host}:${port}`;
}
return `${protocol}://${host}:${port}`;
}
/**
* Create appropriate agent for Axios based on proxy type
*/
static createProxyAgent(proxy: ProxyInfo) {
this.validateConfig(proxy);
const proxyUrl = this.createProxyUrl(proxy);
switch (proxy.protocol) {
case 'socks4':
case 'socks5':
// console.log(`Using SOCKS proxy: ${proxyUrl}`);
return new SocksProxyAgent(proxyUrl);
case 'http':
return new HttpProxyAgent(proxyUrl);
case 'https':
return new HttpsProxyAgent(proxyUrl);
default:
throw new Error(`Unsupported proxy protocol: ${proxy.protocol}`);
}
}
/**
* Create Axios instance with proxy configuration
*/
static createAxiosConfig(proxy: ProxyInfo): AxiosRequestConfig {
const agent = this.createProxyAgent(proxy);
return {
httpAgent: agent,
httpsAgent: agent,
};
}
/**
* Simple proxy config validation
*/
static validateConfig(proxy: ProxyInfo): void {
if (!proxy.host || !proxy.port) {
throw new Error('Proxy host and port are required');
}
if (!['http', 'https', 'socks4', 'socks5'].includes(proxy.protocol)) {
throw new Error(`Unsupported proxy protocol: ${proxy.protocol}`);
}
}
}