javascript snippet
Fetch JSON helper
Tiny wrapper for JSON requests with error handling.
export async function requestJSON(url, options = {}) {const response = await fetch(url, {headers: { "Content-Type": "application/json", ...(options.headers || {}) },...options,});const payload = await response.json().catch(() => ({}));if (!response.ok) {const message = payload?.message || response.statusText;throw new Error(message);}return payload;}// Usageconst data = await requestJSON("/api/data", { method: "POST", body: JSON.stringify({ hello: "world" }) });