export abstract class BaseHttpClient { protected baseUrl: string; constructor() { this.baseUrl = process.env.NEXT_PUBLIC_API_URL || "/api/"; } protected async request( endpoint: string, options: RequestInit = {} ): Promise { const url = `${this.baseUrl}${endpoint}`; const defaultOptions: RequestInit = { credentials: "same-origin", headers: { "Content-Type": "application/json", ...options.headers, }, ...options, }; try { const response = await fetch(url, defaultOptions); if (!response.ok) { throw new Error(`HTTP ${response.status}: ${response.statusText}`); } // Handle empty responses if ( response.status === 204 || response.headers.get("content-length") === "0" ) { return {} as T; } return await response.json(); } catch (error) { console.error(`Request failed for ${endpoint}:`, error); throw error; } } protected async get(endpoint: string): Promise { return this.request(endpoint); } protected async post(endpoint: string, data?: any): Promise { return this.request(endpoint, { method: "POST", body: data ? JSON.stringify(data) : undefined, }); } protected async put(endpoint: string, data?: any): Promise { return this.request(endpoint, { method: "PUT", body: data ? JSON.stringify(data) : undefined, }); } protected async delete(endpoint: string, data?: any): Promise { return this.request(endpoint, { method: "DELETE", body: data ? JSON.stringify(data) : undefined, }); } }