2022-12-06 22:13:11 +00:00
|
|
|
/// <reference lib="dom" />
|
|
|
|
|
2022-12-07 06:07:42 +00:00
|
|
|
let _undici: any
|
|
|
|
|
2022-12-06 22:13:11 +00:00
|
|
|
// Use `undici` for node.js 16 and 17
|
|
|
|
// Use `fetch` for node.js >= 18
|
2022-12-07 04:07:14 +00:00
|
|
|
// Use `fetch` for all other environments, including browsers
|
|
|
|
// NOTE: The top-level await is removed in a `postbuild` npm script for the
|
|
|
|
// browser build
|
2022-12-06 22:13:11 +00:00
|
|
|
const fetch =
|
|
|
|
globalThis.fetch ??
|
2022-12-07 06:07:42 +00:00
|
|
|
async function undiciFetchWrapper(
|
|
|
|
...args: Parameters<typeof globalThis.fetch>
|
|
|
|
): Promise<Response> {
|
|
|
|
if (!_undici) {
|
|
|
|
_undici = await import('undici')
|
|
|
|
}
|
|
|
|
|
|
|
|
return _undici.fetch(...args)
|
|
|
|
}
|
2022-12-05 23:09:31 +00:00
|
|
|
|
|
|
|
export { fetch }
|