-
Notifications
You must be signed in to change notification settings - Fork 4.6k
fix: use the native fetch
API in browsers and a patched node-fetch
otherwise
#25576
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const Headers = globalThis.Headers; | ||
export const Request = globalThis.Request; | ||
export const Response = globalThis.Response; | ||
export default globalThis.fetch; | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import * as nodeFetch from 'node-fetch'; | ||
|
||
export * from 'node-fetch'; | ||
export default async function ( | ||
input: nodeFetch.RequestInfo, | ||
init?: nodeFetch.RequestInit, | ||
): Promise<nodeFetch.Response> { | ||
const processedInput = | ||
typeof input === 'string' && input.slice(0, 2) === '//' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. when would the input be missing a protocol and just start with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a standard patch for the broken implementation of Folks can and will write apps that make use of protocol-relative URLs, and we don't want them to break in Node when they do. This replicates what |
||
? 'https:' + input | ||
: input; | ||
return await nodeFetch.default(processedInput, init); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
globalThis
will resolve toself
in workers, andwindow
in browsers. Both havefetch
available on them.