// Created on savesnippets.com ยท https://savesnippets.com/pHnz4Ot5gHN7ss export async function postJson( url: string, body: TReq, opts: Omit = {}, ): Promise { const r = await fetch(url, { method: 'POST', body: JSON.stringify(body), ...opts, headers: { 'Content-Type': 'application/json', 'Accept': 'application/json', ...(opts.headers ?? {}), }, }); if (!r.ok) throw new Error(`HTTP ${r.status}: ${await r.text()}`); return r.json() as Promise; } type CreateUser = { name: string; email: string }; type CreatedUser = { id: number; name: string; email: string; created_at: string }; const u = await postJson('/api/users', { name: 'Alice', email: 'a@x.com' });