| 31 | |
| 32 | // Auto-parse JSON when the response advertises it, fall back to raw text |
| 33 | const parseBody = async (response) => { |
| 34 | const ct = (response.headers.get('content-type') || '').toLowerCase(); |
| 35 | const text = await response.text(); |
| 36 | if (!text) return ct.includes('json') ? null : ''; |
| 37 | if (ct.includes('json')) { |
| 38 | try { |
| 39 | return JSON.parse(text); |
| 40 | } catch { |
| 41 | return text; |
| 42 | } |
| 43 | } |
| 44 | return text; |
| 45 | }; |
| 46 | |
| 47 | const isOk = (status, validate) => (validate ? validate(status) : status >= 200 && status < 300); |
| 48 | |