Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use wrapper to allow patching global fetch #377

Merged
merged 6 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
throw new Error("unable to locate global object");
})();

export const fetch =
_globalThis.fetch ||
(() => Promise.reject(new Error("[ofetch] global.fetch is not supported!")));
// ref: https://github.com/unjs/ofetch/issues/295
export const fetch = _globalThis.fetch
? (...args: Parameters<typeof globalThis.fetch>) => _globalThis.fetch(...args)
: () => Promise.reject(new Error("[ofetch] global.fetch is not supported!"));

Check warning on line 27 in src/index.ts

View check run for this annotation

Codecov / codecov/patch

src/index.ts#L25-L27

Added lines #L25 - L27 were not covered by tests

export const Headers = _globalThis.Headers;
export const AbortController = _globalThis.AbortController;
Expand Down
4 changes: 3 additions & 1 deletion src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
};
}

export const fetch = globalThis.fetch || createNodeFetch();
export const fetch = globalThis.fetch
? (...args: Parameters<typeof globalThis.fetch>) => globalThis.fetch(...args)
: (createNodeFetch() as typeof globalThis.fetch);

Check warning on line 39 in src/node.ts

View check run for this annotation

Codecov / codecov/patch

src/node.ts#L39

Added line #L39 was not covered by tests

export const Headers = globalThis.Headers || _Headers;
export const AbortController = globalThis.AbortController || _AbortController;
Expand Down