UNPKG

@adonis-agora/authkit-react

Version:

Frontend ergonomics over AuthKit for AdonisJS + Inertia + React apps: a typed useAuth() hook, role-gating hooks and gating components.

27 lines (26 loc) • 819 B
export async function jsonRequest(url, init = {}) { const { csrfToken, headers, ...rest } = init; const res = await fetch(url, { credentials: 'same-origin', headers: { Accept: 'application/json', ...(rest.body ? { 'Content-Type': 'application/json' } : {}), ...(csrfToken ? { 'X-CSRF-TOKEN': csrfToken } : {}), ...headers, }, ...rest, }); if (!res.ok) { let message = `Request failed (${res.status})`; try { const body = await res.json(); if (body && typeof body.message === 'string') message = body.message; } catch { } throw new Error(message); } const text = await res.text(); return (text ? JSON.parse(text) : null); }