25 lines
578 B
JavaScript
Executable File
25 lines
578 B
JavaScript
Executable File
/** @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"
|
|
}
|
|
let result
|
|
try {
|
|
result = await fetch(url, options)
|
|
} catch (error) {
|
|
return { ok: false, result: error }
|
|
}
|
|
|
|
// not json?
|
|
if (result.headers.get("content-type").indexOf("application/json") == -1)
|
|
return { ok: false, result: result }
|
|
|
|
return { ok: true, result: await result.json() }
|
|
}
|
|
|
|
module.exports.request = request
|