renamed http-client to http and fixed tests

This commit is contained in:
Bojan Kucera 2025-06-07 13:20:58 -04:00
parent e87acb5e18
commit 3d9afd711e
26 changed files with 472 additions and 496 deletions

266
libs/http/src/client.ts Normal file
View file

@ -0,0 +1,266 @@
import type { Logger } from '@stock-bot/logger';
import type {
HttpClientConfig,
RequestConfig,
HttpResponse,
} from './types.js';
import { HttpError } from './types.js';
import { ProxyManager } from './proxy-manager.js';
import got from 'got';
export class HttpClient {
private readonly config: HttpClientConfig;
private readonly logger?: Logger;
constructor(config: HttpClientConfig = {}, logger?: Logger) {
this.config = config;
this.logger = logger;
}
// Convenience methods
async get<T = any>(url: string, config: Omit<RequestConfig, 'method' | 'url'> = {}): Promise<HttpResponse<T>> {
return this.request<T>({ ...config, method: 'GET', url });
}
async post<T = any>(url: string, body?: any, config: Omit<RequestConfig, 'method' | 'url' | 'body'> = {}): Promise<HttpResponse<T>> {
return this.request<T>({ ...config, method: 'POST', url, body });
}
async put<T = any>(url: string, body?: any, config: Omit<RequestConfig, 'method' | 'url' | 'body'> = {}): Promise<HttpResponse<T>> {
return this.request<T>({ ...config, method: 'PUT', url, body });
}
async del<T = any>(url: string, config: Omit<RequestConfig, 'method' | 'url'> = {}): Promise<HttpResponse<T>> {
return this.request<T>({ ...config, method: 'DELETE', url });
}
async patch<T = any>(url: string, body?: any, config: Omit<RequestConfig, 'method' | 'url' | 'body'> = {}): Promise<HttpResponse<T>> {
return this.request<T>({ ...config, method: 'PATCH', url, body });
} /**
* Main request method - unified and simplified
*/
async request<T = any>(config: RequestConfig): Promise<HttpResponse<T>> {
const finalConfig = this.mergeConfig(config);
this.logger?.debug('Making HTTP request', {
method: finalConfig.method,
url: finalConfig.url,
hasProxy: !!finalConfig.proxy
});
try {
// Single decision point for proxy type - only request-level proxy
const proxy = finalConfig.proxy;
const useBunFetch = !proxy || ProxyManager.shouldUseBunFetch(proxy);
const response = await this.makeRequest<T>(finalConfig, useBunFetch);
this.logger?.debug('HTTP request successful', {
method: finalConfig.method,
url: finalConfig.url,
status: response.status,
});
return response;
} catch (error) {
this.logger?.warn('HTTP request failed', {
method: finalConfig.method,
url: finalConfig.url,
error: (error as Error).message,
});
throw error;
}
}
/**
* Unified request method with consolidated timeout handling
*/
private async makeRequest<T>(config: RequestConfig, useBunFetch: boolean): Promise<HttpResponse<T>> {
const timeout = config.timeout ?? this.config.timeout ?? 30000;
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeout);
try {
const response = useBunFetch
? await this.fetchRequest<T>(config, controller.signal)
: await this.gotRequest<T>(config, controller.signal);
clearTimeout(timeoutId);
return response;
} catch (error) {
clearTimeout(timeoutId);
// Unified timeout error handling
if (controller.signal.aborted) {
throw new HttpError(`Request timeout after ${timeout}ms`);
}
if ((error as any).name === 'TimeoutError') {
throw new HttpError(`Request timeout after ${timeout}ms`);
}
throw error; // Re-throw other errors as-is
}
}
/**
* Bun fetch implementation (simplified)
*/
private async fetchRequest<T>(config: RequestConfig, signal: AbortSignal): Promise<HttpResponse<T>> {
try {
const options = this.buildFetchOptions(config, signal);
const response = await fetch(config.url, options);
return this.parseFetchResponse<T>(response);
} catch (error) {
throw signal.aborted
? new HttpError(`Request timeout`)
: new HttpError(`Request failed: ${(error as Error).message}`);
}
} /**
* Got implementation (simplified for SOCKS proxies)
*/
private async gotRequest<T>(config: RequestConfig, signal: AbortSignal): Promise<HttpResponse<T>> {
try {
const options = this.buildGotOptions(config, signal);
const response = await got(config.url, options);
return this.parseGotResponse<T>(response);
} catch (error) {
// Handle both AbortSignal timeout and Got-specific timeout errors
if (signal.aborted) {
throw new HttpError(`Request timeout`);
}
if ((error as any).name === 'TimeoutError') {
throw new HttpError(`Request timeout`);
}
throw new HttpError(`Request failed: ${(error as Error).message}`);
}
}
/**
* Build fetch options (extracted for clarity)
*/
private buildFetchOptions(config: RequestConfig, signal: AbortSignal): RequestInit {
const options: RequestInit = {
method: config.method || 'GET',
headers: config.headers || {},
signal,
};
// Add body
if (config.body && config.method !== 'GET') {
if (typeof config.body === 'object') {
options.body = JSON.stringify(config.body);
options.headers = { 'Content-Type': 'application/json', ...options.headers };
} else {
options.body = config.body;
}
}
// Add proxy (HTTP/HTTPS only) - request level only
if (config.proxy && ProxyManager.shouldUseBunFetch(config.proxy)) {
(options as any).proxy = ProxyManager.createBunProxyUrl(config.proxy);
}
return options;
} /**
* Build Got options (extracted for clarity)
*/
private buildGotOptions(config: RequestConfig, signal: AbortSignal): any {
const options: any = {
method: config.method || 'GET',
headers: config.headers || {},
signal, // Use AbortSignal instead of Got's timeout
retry: {
limit: 3,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH']
},
throwHttpErrors: false,
responseType: 'json'
};
// Add body
if (config.body && config.method !== 'GET') {
if (typeof config.body === 'object') {
options.json = config.body;
} else {
options.body = config.body;
options.headers = { 'Content-Type': 'text/plain', ...options.headers };
}
}
// Add SOCKS proxy via agent - request level only
if (config.proxy && !ProxyManager.shouldUseBunFetch(config.proxy)) {
ProxyManager.validateConfig(config.proxy);
const agent = ProxyManager.createGotAgent(config.proxy);
options.agent = {
http: agent,
https: agent
};
}
return options;
}
/**
* Parse fetch response (simplified)
*/
private async parseFetchResponse<T>(response: Response): Promise<HttpResponse<T>> {
const data = await this.parseResponseBody<T>(response);
const headers = Object.fromEntries(response.headers.entries());
if (!response.ok) {
throw new HttpError(
`Request failed with status ${response.status}`,
response.status,
{ data, status: response.status, headers, ok: response.ok }
);
}
return { data, status: response.status, headers, ok: response.ok };
}
/**
* Parse Got response (simplified)
*/
private parseGotResponse<T>(response: any): HttpResponse<T> {
const headers = response.headers as Record<string, string>;
const status = response.statusCode;
const ok = status >= 200 && status < 300;
const data = response.body as T;
if (!ok) {
throw new HttpError(
`Request failed with status ${status}`,
status,
{ data, status, headers, ok }
);
}
return { data, status, headers, ok };
}
/**
* Unified body parsing (works for fetch response)
*/
private async parseResponseBody<T>(response: Response): Promise<T> {
const contentType = response.headers.get('content-type') || '';
if (contentType.includes('application/json')) {
return response.json();
} else if (contentType.includes('text/')) {
return response.text() as any;
} else {
return response.arrayBuffer() as any;
}
}
/**
* Merge configs - request-level proxy only
*/
private mergeConfig(config: RequestConfig): RequestConfig {
return {
...config,
headers: { ...this.config.headers, ...config.headers },
timeout: config.timeout ?? this.config.timeout,
};
}
}

7
libs/http/src/index.ts Normal file
View file

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

View file

@ -0,0 +1,91 @@
import got from 'got';
import { SocksProxyAgent } from 'socks-proxy-agent';
import { HttpsProxyAgent } from 'https-proxy-agent';
import { HttpProxyAgent } from 'http-proxy-agent';
import type { ProxyConfig } from './types.js';
export class ProxyManager {
/**
* Determine if we should use Bun fetch (HTTP/HTTPS) or Got (SOCKS)
*/
static shouldUseBunFetch(proxy: ProxyConfig): boolean {
return proxy.protocol === 'http' || proxy.protocol === 'https';
}
/**
* Create Bun fetch proxy URL for HTTP/HTTPS proxies
*/
static createBunProxyUrl(proxy: ProxyConfig): 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 Got based on proxy type
*/
static createGotAgent(proxy: ProxyConfig) {
this.validateConfig(proxy);
const proxyUrl = this.buildProxyUrl(proxy);
switch (proxy.protocol) {
case 'socks4':
case 'socks5':
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 Got instance with proxy configuration
*/
static createGotInstance(proxy: ProxyConfig) {
const agent = this.createGotAgent(proxy);
return got.extend({
agent: {
http: agent,
https: agent
},
timeout: {
request: 30000,
connect: 10000
},
retry: {
limit: 3,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH']
},
throwHttpErrors: false // We'll handle errors ourselves
});
}
private static buildProxyUrl(proxy: ProxyConfig): string {
const { protocol, host, port, username, password } = proxy;
if (username && password) {
return `${protocol}://${encodeURIComponent(username)}:${encodeURIComponent(password)}@${host}:${port}`;
}
return `${protocol}://${host}:${port}`;
}
/**
* Simple proxy config validation
*/
static validateConfig(proxy: ProxyConfig): 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}`);
}
}
}

42
libs/http/src/types.ts Normal file
View file

@ -0,0 +1,42 @@
// Minimal types for fast HTTP client
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
export interface ProxyConfig {
protocol: 'http' | 'https' | 'socks4' | 'socks5';
host: string;
port: number;
username?: string;
password?: string;
}
export interface HttpClientConfig {
timeout?: number;
headers?: Record<string, string>;
}
export interface RequestConfig {
method?: HttpMethod;
url: string;
headers?: Record<string, string>;
body?: any;
timeout?: number;
proxy?: ProxyConfig;
}
export interface HttpResponse<T = any> {
data: T;
status: number;
headers: Record<string, string>;
ok: boolean;
}
export class HttpError extends Error {
constructor(
message: string,
public status?: number,
public response?: HttpResponse
) {
super(message);
this.name = 'HttpError';
}
}