Skip to content

Commit a6e602d

Browse files
committed
refactor: reuse exports from src/node.ts
1 parent 2e8d2aa commit a6e602d

File tree

5 files changed

+34
-39
lines changed

5 files changed

+34
-39
lines changed

src/_utils.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/index.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
1-
import _fetch, {
1+
import {
2+
fetch as _fetch,
3+
AbortController as _AbortController,
24
Blob as _Blob,
35
File as _File,
46
FormData as _FormData,
57
Headers as _Headers,
68
Request as _Request,
79
Response as _Response,
8-
} from "node-fetch";
10+
} from "./node";
911

10-
import _AbortController from "abort-controller";
12+
export {
13+
AbortError,
14+
FetchError,
15+
blobFrom,
16+
blobFromSync,
17+
fileFrom,
18+
fileFromSync,
19+
isRedirect,
20+
} from "./node";
1121

12-
const _forceNodeFetch =
13-
typeof process !== undefined &&
14-
typeof process.env !== undefined &&
15-
process.env.FORCE_NODE_FETCH;
22+
const _forceNodeFetch = !!globalThis.process?.env?.FORCE_NODE_FETCH;
1623

1724
function _getFetch() {
1825
if (!_forceNodeFetch && globalThis.fetch) {
@@ -32,13 +39,3 @@ export const Request = (!_forceNodeFetch && globalThis.Request) || _Request;
3239
export const Response = (!_forceNodeFetch && globalThis.Response) || _Response;
3340
export const AbortController =
3441
(!_forceNodeFetch && globalThis.AbortController) || _AbortController;
35-
36-
export {
37-
AbortError,
38-
FetchError,
39-
blobFrom,
40-
blobFromSync,
41-
fileFrom,
42-
fileFromSync,
43-
isRedirect,
44-
} from "node-fetch";

src/native.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export const AbortController = globalThis.AbortController;
99
export const fetch =
1010
globalThis.fetch ||
1111
(() => {
12-
throw new Error("global fetch is not available!");
12+
throw new Error(
13+
"[node-fetch-native] Failed to fetch: `globalThis.fetch` is not available!",
14+
);
1315
});
1416
export default fetch;

src/node.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import _fetch from "node-fetch";
2-
import { checkNodeEnvironment } from "./_utils";
32

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

7-
checkNodeEnvironment();
8-
96
export const fetch = _fetch;
107
export default fetch;
118

@@ -18,3 +15,16 @@ export {
1815
fileFromSync,
1916
isRedirect,
2017
} from "node-fetch";
18+
19+
checkNodeEnvironment();
20+
21+
function checkNodeEnvironment() {
22+
if (
23+
!globalThis.process?.versions?.node &&
24+
!globalThis.process?.env.DISABLE_NODE_FETCH_NATIVE_WARN
25+
) {
26+
throw new Error(
27+
"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.",
28+
);
29+
}
30+
}

src/polyfill.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
import _fetch, {
1+
import {
2+
fetch as _fetch,
23
Blob as _Blob,
34
File as _File,
45
FormData as _FormData,
56
Headers as _Headers,
67
Request as _Request,
78
Response as _Response,
8-
} from "node-fetch";
9-
10-
import _AbortController from "abort-controller";
11-
12-
import { checkNodeEnvironment } from "./_utils";
13-
14-
checkNodeEnvironment();
9+
AbortController as _AbortController,
10+
} from "./node";
1511

1612
function polyfill(name: string, impl: any) {
1713
if (!(name in globalThis)) {

0 commit comments

Comments
 (0)