/** @type {import("node-fetch").default} */ // @ts-ignore const fetch = require("node-fetch") async function request(url, options = {}) { if (!options.headers) options.headers = {} options.headers = { "user-agent": "CloudTubeBackend/1.0" } const res = await fetch(url, options) if (res.status >= 400) { if (res.status == 500) { let body = await res.text(); const traceback_start = '
'; const traceback_end = ''; const si = body.indexOf(traceback_start) if (si >= 0) body = body.slice(si + traceback_start.length); const ei = body.indexOf(traceback_end) if (ei >= 0) body = body.slice(0, ei); throw new Error(`Unexpected error in backend:\n\n${body}`); } else { throw new Error(`Unexpected response from backend: ${res.status} ${res.statusText}`); } } return res } module.exports.request = request