Skip to content

Commit a488d25

Browse files
authored
Merge pull request #1839 from codders/feat/add-proxy-for-client-agent
feat(client): add proxy object for `request.agent` for compatibility
2 parents cec2606 + 44e68b1 commit a488d25

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/client.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,14 @@ Request.prototype._end = function () {
872872
xhr.send(typeof data === 'undefined' ? null : data);
873873
};
874874

875-
request.agent = () => new Agent();
875+
// create a Proxy that can instantiate a new Agent without using `new` keyword
876+
// (for backward compatibility and chaining)
877+
const proxyAgent = new Proxy(Agent, {
878+
apply(target, thisArg, argumentsList) {
879+
return new target(...argumentsList);
880+
}
881+
});
882+
request.agent = proxyAgent;
876883

877884
for (const method of ['GET', 'POST', 'OPTIONS', 'PATCH', 'PUT', 'DELETE']) {
878885
Agent.prototype[method.toLowerCase()] = function (url, fn) {

0 commit comments

Comments
 (0)