@@ -22,6 +22,7 @@ export default class HttpClient {
22
22
#abortController;
23
23
#agent;
24
24
#breaker;
25
+ #followRedirects;
25
26
#hasFallback = false ;
26
27
#logger;
27
28
#throwOn400;
@@ -51,6 +52,7 @@ export default class HttpClient {
51
52
this . #throwOn500 = throwOn500 ;
52
53
53
54
this . #abortController = abortController ;
55
+ this . #followRedirects = followRedirects ;
54
56
55
57
// TODO; Can we avoid bind here in a nice way?????
56
58
this . #breaker = new Opossum ( this . #request. bind ( this ) , {
@@ -63,23 +65,17 @@ export default class HttpClient {
63
65
timeout,
64
66
} ) ;
65
67
66
- const { redirect } = interceptors ;
67
- let agent = new Agent ( {
68
+ this . #agent = new Agent ( {
68
69
keepAliveMaxTimeout,
69
70
keepAliveTimeout,
70
71
connections,
71
72
pipelining,
72
73
} ) ;
73
- if ( followRedirects ) {
74
- agent = agent . compose (
75
- redirect ( { maxRedirections : 1 , throwOnMaxRedirects : true } ) ,
76
- ) ;
77
- }
78
- this . #agent = agent ;
79
74
80
75
if ( fallback ) {
81
76
this . #hasFallback = true ;
82
77
if ( typeof fallback === 'string' ) {
78
+ //@ts -ignore
83
79
this . #fallback( ( ) => fallback ) ;
84
80
} else {
85
81
this . #fallback( fallback ) ;
@@ -88,8 +84,16 @@ export default class HttpClient {
88
84
}
89
85
90
86
async #request( options = { } ) {
87
+ if ( this . #followRedirects) {
88
+ const { redirect } = interceptors ;
89
+ options . dispatcher = this . #agent. compose (
90
+ redirect ( { maxRedirections : 1 } ) ,
91
+ ) ;
92
+ } else {
93
+ options . dispatcher = this . #agent;
94
+ }
95
+
91
96
const { statusCode, headers, trailers, body } = await request ( {
92
- dispatcher : this . #agent,
93
97
...options ,
94
98
} ) ;
95
99
0 commit comments