(url, opts = {})
| 36 | |
| 37 | /** Fetch JSON with a short timeout; returns null on any failure. */ |
| 38 | export async function fetchJSON(url, opts = {}) { |
| 39 | const ctrl = new AbortController(); |
| 40 | const t = setTimeout(() => ctrl.abort(), opts.timeout ?? 6000); |
| 41 | try { |
| 42 | const res = await fetch(url, { signal: ctrl.signal, ...opts }); |
| 43 | if (!res.ok) return null; |
| 44 | return await res.json(); |
| 45 | } catch { |
| 46 | return null; |
| 47 | } finally { |
| 48 | clearTimeout(t); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | export const fmt = { |
| 53 | pct: (v, d = 0) => (v == null || isNaN(v) ? '—' : `${(v * (v <= 1 ? 100 : 1)).toFixed(d)}%`), |
no outgoing calls
no test coverage detected