bug: onReq
oldrich-svec opened this issue · 2 comments
oldrich-svec commented
[async] onReq(req, options[, callback]): Called before proxy request. If returning a truthy value it will be used as the request.
If I do something like:
onReq: (req, ureq) => {
ureq.headers['x-user-id'] = 'TEST'
return Http.request(ureq)
}
it works. If I do:
onReq: (req, ureq) => {
ureq.headers['x-user-id'] = 'TEST'
}
It does not work ;).
EDIT:
I guess it should be changed to something like:
const callOnReq = async () => {
if (!onReq) return
if (onReq.length <= 2) {
return onReq(req, ureq)
} else {
// Legacy compat...
return new Promise((resolve, reject) => {
const promiseOrReq = onReq(req, ureq, (err, val) =>
err ? reject(err) : resolve(val)
)
if (promiseOrReq) {
if (promiseOrReq.then) {
promiseOrReq.then(resolve).catch(reject)
} else if (promiseOrReq.abort) {
resolve(promiseOrReq)
} else {
throw new Error(
'onReq must return a promise or a request object'
)
}
}
})
}
}
const request = await callOnReq()
if (request) {
return request
} else {
let agent
if (protocol == null || /^(http|ws):?$/.test(protocol)) {
agent = http
} else if (/^(http|ws)s:?$/.test(protocol)) {
agent = https
} else {
throw new Error('invalid protocol')
}
return agent.request(ureq)
}
ronag commented
Thank you for reporting this. Please try 5.0.39.
oldrich-s commented
Perfect, thanks a lot! Tested and seems to work just fine 👍