| 1 | import nodeFetch from "node-fetch"; |
| 2 | |
| 3 | export const fetch: typeof nodeFetch = async (...args) => { |
| 4 | const response = await nodeFetch(...args); |
| 5 | const { status, statusText, json } = response; |
| 6 | if (status >= 400 && status < 600) { |
| 7 | const baseError = `${status} ${statusText}`; |
| 8 | try { |
| 9 | const responseJSON = (await json()) as { error: string }; |
| 10 | throw new Error(`${baseError}: ${responseJSON.error}`); |
| 11 | } catch { |
| 12 | throw new Error(baseError); |
| 13 | } |
| 14 | } |
| 15 | return response; |
| 16 | }; |