(url, opts = {})
| 138 | } |
| 139 | |
| 140 | async function fetchJSON(url, opts = {}) { |
| 141 | const res = await fetch(url, { headers: { ...authHeaders() }, ...opts }); |
| 142 | if (!res.ok) { |
| 143 | let message = `HTTP ${res.status} for ${url}`; |
| 144 | let bodyText = ''; |
| 145 | try { bodyText = await res.text(); } catch {} |
| 146 | let payload; |
| 147 | try { payload = JSON.parse(bodyText); } catch { payload = null; } |
| 148 | const payloadMsg = payload && payload.message ? payload.message : ''; |
| 149 | if (res.status === 403 && (payloadMsg.toLowerCase().includes('rate limit') || (res.headers.get('x-ratelimit-remaining') === '0'))) { |
| 150 | throw new Error(buildRateLimitMessage(res, payloadMsg)); |
| 151 | } |
| 152 | if (res.status === 401) { |
| 153 | throw new Error('GitHub API authentication failed. Please check your token.'); |
| 154 | } |
| 155 | throw new Error(message + (payloadMsg ? ` — ${payloadMsg}` : '')); |
| 156 | } |
| 157 | return res.json(); |
| 158 | } |
| 159 | |
| 160 | async function fetchText(url, opts = {}) { |
| 161 | const res = await fetch(url, opts); |
no test coverage detected