(ui, path, options = {})
| 10 | * @throws {Error} If the response is not OK. |
| 11 | */ |
| 12 | export async function ghFetch(ui, path, options = {}) { |
| 13 | const url = path.startsWith('https://') |
| 14 | ? path |
| 15 | : `https://api.github.com/repos/${ui.ghOwnerInput.value}/${ui.ghRepoInput.value}${path}`; |
| 16 | const res = await fetch(url, { |
| 17 | ...options, |
| 18 | headers: { |
| 19 | Authorization: `token ${ui.ghTokenInput.value}`, |
| 20 | Accept: 'application/vnd.github.v3+json', |
| 21 | ...options.headers, |
| 22 | }, |
| 23 | }); |
| 24 | if (!res.ok) { |
| 25 | const txt = await res.text(); |
| 26 | let err; |
| 27 | try { |
| 28 | err = JSON.parse(txt); |
| 29 | } catch (e) { |
| 30 | err = { message: txt }; |
| 31 | } |
| 32 | throw new Error(err.message || res.statusText); |
| 33 | } |
| 34 | return res.json(); |
| 35 | } |
no test coverage detected