Files
discord-clone/node_modules/unenv/runtime/fetch/index.mjs
2023-01-03 09:29:04 -06:00

27 lines
791 B
JavaScript

export * from "./call.mjs";
export function createFetch(call, _fetch = global.fetch) {
return async function ufetch(input, init) {
const url = input.toString();
if (!url.startsWith("/")) {
return _fetch(url, init);
}
try {
const r = await call({ url, ...init });
return new Response(r.body, {
status: r.status,
statusText: r.statusText,
headers: Object.fromEntries(
Object.entries(r.headers).map(
([name, value]) => [name, Array.isArray(value) ? value.join(",") : value || ""]
)
)
});
} catch (error) {
return new Response(error.toString(), {
status: Number.parseInt(error.statusCode || error.code) || 500,
statusText: error.statusText
});
}
};
}