ofetch
ofetch copied to clipboard
Agent did not support on node latest LTS (v18.14.2)
Update
it seems like a bug of node
Environment
ofetch: v1.0.1 node:v18.14.2
Reproduction
here is a minimum recurrence demo
import { getProxySettings } from "get-proxy-settings";
import { ofetch } from "ofetch/node";
import httpAgent from 'http-proxy-agent'
import httpsAgent from 'https-proxy-agent'
let agent
await (async function setSystemProxy() {
const { http, https } = (await getProxySettings()) || {}
function agentGenerator(ProxySetting) {
const { protocol, host, port } = ProxySetting
const agentUrl = `${protocol}://${host}:${port}`
if (protocol === 'http') {
return new httpAgent(agentUrl)
}
if (protocol === 'https') {
return new httpsAgent(agentUrl)
}
return false
}
if (http) {
agent = agentGenerator(http)
}
if (https) {
agent = agentGenerator(https)
}
})().then(async () => {
const response = await ofetch.raw('https://google.com', { agent })
})
Describe the bug
Nodejs has supported fetch api since v17.5.0
This caused current issue:
export const fetch = globalThis.fetch || createNodeFetch();
I'm going to submit a PR to fix this bug!
Additional context
No response
Logs
No response
it seems like a bug of node
I bumped into this problem today when using Node 16 the "agent" option works as expected but when using Node 18 the "agent" option does not seem to work. Is this a known issue with ofetch or am I missconfiguring something? :)
Node.js 18 uses undici for fetch. It seems they use dispatchers (like this). PR for docs is welcome if anyone can investigate further.
Hey @everchanger @pidanmeng, this isn't 100% your situation(s) but here's a stripped-down example of how I solved a localhost self-signed certificate issue in development (source):
import { Agent } from 'undici'
try {
const response = await $fetch('/my/api/call', {
...(process.env.NODE_ENV === 'development' && {
dispatcher: new Agent({
connect: {
rejectUnauthorized: false
}
})
})
})
console.log(response)
} catch (e) {
console.log(e)
}
I hope this helps set you on the right path!
Agent did not support on node v21.4.0
You should use dispatcher
in modern node (https://github.com/unjs/ofetch#%EF%B8%8F-adding-https-agent)