Skip to content

Commit

Permalink
feat: add warning for when node polyfill is used in non-node environm…
Browse files Browse the repository at this point in the history
…ents (#90)
  • Loading branch information
pi0 authored Aug 22, 2023
1 parent dc87446 commit a04969a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/_utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function checkNodeEnvironment() {
if (
!globalThis.process?.versions?.node &&
!globalThis.process?.env.DISABLE_NODE_FETCH_NATIVE_WARN
) {
throw new Error(
"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.",
);
}
}
3 changes: 3 additions & 0 deletions src/node.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import _fetch from "node-fetch";
import { checkNodeEnvironment } from "./_utils";

export { Blob, File, FormData, Headers, Request, Response } from "node-fetch";
export { default as AbortController } from "abort-controller";

checkNodeEnvironment();

export const fetch = _fetch;
export default fetch;

Expand Down
4 changes: 4 additions & 0 deletions src/polyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import _fetch, {

import _AbortController from "abort-controller";

import { checkNodeEnvironment } from "./_utils";

checkNodeEnvironment();

function polyfill(name: string, impl: any) {
if (!(name in globalThis)) {
try {
Expand Down

0 comments on commit a04969a

Please sign in to comment.