// Centralized API client for cloud mode. // Adds the Authorization: Bearer header from the stored session token, and turns // a 402 (quota exceeded) into a typed QuotaError the UI can catch to prompt a top-up. import { getApiUrl } from '../config'; export const AUTH_TOKEN_KEY = 'openshorts_auth'; export const getToken = () => localStorage.getItem(AUTH_TOKEN_KEY) || ''; export const setToken = (t) => localStorage.setItem(AUTH_TOKEN_KEY, t); export const clearToken = () => localStorage.removeItem(AUTH_TOKEN_KEY); export class QuotaError extends Error { constructor(detail) { super('quota_exceeded'); this.name = 'QuotaError'; this.minutesRequired = detail?.minutes_required; this.minutesRemaining = detail?.minutes_remaining; } } // Drop-in fetch wrapper. Always attaches the bearer token when present. export async function apiFetch(path, options = {}) { const headers = new Headers(options.headers || {}); const token = getToken(); if (token) headers.set('Authorization', `Bearer ${token}`); const res = await fetch(getApiUrl(path), { ...options, headers }); if (res.status === 402) { let detail = {}; try { const body = await res.clone().json(); detail = body.detail || body; } catch (_) { /* ignore */ } throw new QuotaError(detail); } return res; } // Convenience JSON helper. export async function apiJson(path, options = {}) { const res = await apiFetch(path, options); if (!res.ok) { const text = await res.text().catch(() => ''); throw new Error(`${res.status}: ${text}`); } return res.json(); }