Skip to content

Commit

Permalink
fix: improve DISABLE_NODE_FETCH_NATIVE_WARN behavior (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub authored Jan 20, 2025
1 parent 2b5e3d4 commit c4cac60
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 9 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ You have two ways to do this:
- Set the `FORCE_NODE_FETCH` environment variable before starting the application.
- Import from `node-fetch-native/node`

## Disable runtime check

Once the `node-fetch-native/node` module is loaded, it pushes a log warning if the current runtime differs from the Node.js. Set the `DISABLE_NODE_FETCH_NATIVE_WARN` environment variable to turn this check off.

## Polyfill support

Using the polyfill method, we can ensure global fetch is available in the environment and all files. Natives are always preferred.
Expand Down
9 changes: 1 addition & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,7 @@ export {

const _forceNodeFetch = !!globalThis.process?.env?.FORCE_NODE_FETCH;

function _getFetch() {
if (!_forceNodeFetch && globalThis.fetch) {
return globalThis.fetch;
}
return _fetch;
}

export const fetch = _getFetch();
export const fetch = (!_forceNodeFetch && globalThis.fetch) || _fetch;
export default fetch;

export const Blob = (!_forceNodeFetch && globalThis.Blob) || _Blob;
Expand Down
2 changes: 1 addition & 1 deletion src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ checkNodeEnvironment();
function checkNodeEnvironment() {
if (
!globalThis.process?.versions?.node &&
!globalThis.process?.env.DISABLE_NODE_FETCH_NATIVE_WARN
!globalThis.process?.env?.DISABLE_NODE_FETCH_NATIVE_WARN
) {
console.warn(
"[node-fetch-native] Node.js compatible build of `node-fetch-native` is being used in a non-Node.js environment. Please make sure you are using proper export conditions or report this issue to https://github.com/unjs/node-fetch-native. You can set `process.env.DISABLE_NODE_FETCH_NATIVE_WARN` to disable this warning.",
Expand Down

0 comments on commit c4cac60

Please sign in to comment.