Adjust for available ESM-only dependencies (#1581)

* Adjust for available ESM-only dependencies

* Flags for jest (sadly, still required mid-2022)
This commit is contained in:
Jaco
2022-07-06 12:50:36 +03:00
committed by GitHub
parent 1d71f67e2a
commit 4367bb0f68
9 changed files with 182 additions and 104 deletions
+14 -2
View File
@@ -1,10 +1,22 @@
// Copyright 2017-2022 @polkadot/x-fetch authors & contributors
// SPDX-License-Identifier: Apache-2.0
import nodeFetch from 'node-fetch';
import { extractGlobal } from '@polkadot/x-global';
export { packageInfo } from './packageInfo';
let resolvedFetch: null | typeof fetch = null;
async function nodeFetch (...args: Parameters<typeof fetch>): Promise<Response> {
if (!resolvedFetch) {
// we use the async import here to resolve on-demand
// (this allows us to use the latest ESM version of the package)
const mod = await import('node-fetch');
resolvedFetch = mod.default as unknown as typeof fetch;
}
return resolvedFetch(...args);
}
export const fetch = extractGlobal('fetch', nodeFetch);