export class HttpClient { private TIMEOUT = 10000; constructor(private baseURL: string) {} get(path: string, params?: Record): Promise { const query = params && new URLSearchParams(params); path = query ? `${path}?${query.toString()}` : path; const url = `${this.baseURL}/${path}`; const options = { method: 'GET', headers: { 'Content-Type': 'application/json' }, }; return this.handleResponse(this.fetchWithTimeout(url, options)); } post(path: string, data: Record): Promise { const url = `${this.baseURL}/${path}`; const options = { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data), }; return this.handleResponse(this.fetchWithTimeout(url, options)); } private async handleResponse(responsePromise: Promise): Promise { const response = await responsePromise; if (response.ok) { return await response.json(); } else { const errorMessage = await response.text(); return Promise.reject(new Error(errorMessage)); } } private async fetchWithTimeout(input: RequestInfo, options: RequestInit & { timeout?: number } = {}) { const { timeout = this.TIMEOUT } = options; const controller = new AbortController(); const id = setTimeout(() => controller.abort(), timeout); const response = await fetch(input, { ...options, signal: controller.signal, }); clearTimeout(id); return response; } }