2020-11-06 03:33:14 +01:00
|
|
|
import fetch, { Headers, Request, Response } from 'node-fetch'
|
2020-05-12 21:58:21 +02:00
|
|
|
|
|
|
|
// Polyfill fetch() in the Node.js environment
|
|
|
|
if (!global.fetch) {
|
2021-07-22 16:34:33 +02:00
|
|
|
const agent = ({ protocol }) =>
|
2021-08-03 02:38:42 +02:00
|
|
|
protocol === 'http:' ? global.__NEXT_HTTP_AGENT : global.__NEXT_HTTPS_AGENT
|
2021-07-22 16:34:33 +02:00
|
|
|
const fetchWithAgent = (url, opts, ...rest) => {
|
|
|
|
if (!opts) {
|
|
|
|
opts = { agent }
|
|
|
|
} else if (!opts.agent) {
|
|
|
|
opts.agent = agent
|
|
|
|
}
|
|
|
|
return fetch(url, opts, ...rest)
|
|
|
|
}
|
|
|
|
global.fetch = fetchWithAgent
|
2020-05-12 21:58:21 +02:00
|
|
|
global.Headers = Headers
|
|
|
|
global.Request = Request
|
|
|
|
global.Response = Response
|
|
|
|
}
|