function getHeaders(): Record { return { 'Content-Type': 'application/json', }; } async function get(uri: string, baseUrl: string = ''): Promise { const headers = getHeaders(); const theUrl = baseUrl ? `${baseUrl}/${uri.replace(/^\//, '')}` : uri; const res = await fetch(theUrl, { headers }); return res.json() as Promise; } async function post(uri: string, payload?: unknown, baseUrl: string = ''): Promise { const headers = getHeaders(); const theUrl = baseUrl ? `${baseUrl}/${uri.replace(/^\//, '')}` : uri; const res = await fetch(theUrl, { method: 'POST', body: JSON.stringify(payload), headers, }); return res.json() as Promise; } interface HttpClient { baseUrl: string; get: (url: string) => Promise; post: (url: string, payload?: unknown) => Promise; } export function createClient(baseUrl?: string): HttpClient { const finalBaseUrl = baseUrl || ''; return { baseUrl: finalBaseUrl, get: (url: string) => get(url, finalBaseUrl), post: (url: string, payload?: unknown) => post(url, payload, finalBaseUrl), }; }