Skip to content

Commit

Permalink
fix: use wrapper to allow patching global fetch (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
dwightjack committed Aug 27, 2024
1 parent b5fe505 commit f7707b1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
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 @@ const _globalThis = (function () {
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!"));

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 function createNodeFetch() {
};
}

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

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

0 comments on commit f7707b1

Please sign in to comment.