Skip to content

Commit

Permalink
refactor: reuse exports from src/node.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Aug 22, 2023
1 parent 2e8d2aa commit a6e602d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 39 deletions.
10 changes: 0 additions & 10 deletions src/_utils.ts

This file was deleted.

31 changes: 14 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import _fetch, {
import {
fetch as _fetch,
AbortController as _AbortController,
Blob as _Blob,
File as _File,
FormData as _FormData,
Headers as _Headers,
Request as _Request,
Response as _Response,
} from "node-fetch";
} from "./node";

import _AbortController from "abort-controller";
export {
AbortError,
FetchError,
blobFrom,
blobFromSync,
fileFrom,
fileFromSync,
isRedirect,
} from "./node";

const _forceNodeFetch =
typeof process !== undefined &&
typeof process.env !== undefined &&
process.env.FORCE_NODE_FETCH;
const _forceNodeFetch = !!globalThis.process?.env?.FORCE_NODE_FETCH;

function _getFetch() {
if (!_forceNodeFetch && globalThis.fetch) {
Expand All @@ -32,13 +39,3 @@ export const Request = (!_forceNodeFetch && globalThis.Request) || _Request;
export const Response = (!_forceNodeFetch && globalThis.Response) || _Response;
export const AbortController =
(!_forceNodeFetch && globalThis.AbortController) || _AbortController;

export {
AbortError,
FetchError,
blobFrom,
blobFromSync,
fileFrom,
fileFromSync,
isRedirect,
} from "node-fetch";
4 changes: 3 additions & 1 deletion src/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export const AbortController = globalThis.AbortController;
export const fetch =
globalThis.fetch ||
(() => {
throw new Error("global fetch is not available!");
throw new Error(
"[node-fetch-native] Failed to fetch: `globalThis.fetch` is not available!",
);
});
export default fetch;
16 changes: 13 additions & 3 deletions src/node.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
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 All @@ -18,3 +15,16 @@ export {
fileFromSync,
isRedirect,
} from "node-fetch";

checkNodeEnvironment();

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.",
);
}
}
12 changes: 4 additions & 8 deletions src/polyfill.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import _fetch, {
import {
fetch as _fetch,
Blob as _Blob,
File as _File,
FormData as _FormData,
Headers as _Headers,
Request as _Request,
Response as _Response,
} from "node-fetch";

import _AbortController from "abort-controller";

import { checkNodeEnvironment } from "./_utils";

checkNodeEnvironment();
AbortController as _AbortController,
} from "./node";

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

0 comments on commit a6e602d

Please sign in to comment.