@@ -63,23 +63,17 @@ export class HermesClient {
63
63
schema : z . ZodSchema < ResponseData > ,
64
64
options ?: RequestInit ,
65
65
retries = this . httpRetries ,
66
- backoff = 100 + Math . floor ( Math . random ( ) * 100 ) , // Adding randomness to the initial backoff to avoid "thundering herd" scenario where a lot of clients that get kicked off all at the same time (say some script or something) and fail to connect all retry at exactly the same time too
67
- externalAbortController ?: AbortController
66
+ backoff = 100 + Math . floor ( Math . random ( ) * 100 ) // Adding randomness to the initial backoff to avoid "thundering herd" scenario where a lot of clients that get kicked off all at the same time (say some script or something) and fail to connect all retry at exactly the same time too
68
67
) : Promise < ResponseData > {
69
- const controller = externalAbortController ?? new AbortController ( ) ;
70
- const { signal } = controller ;
71
- options = {
72
- ...options ,
73
- signal,
74
- headers : { ...this . headers , ...options ?. headers } ,
75
- } ; // Merge any existing options with the signal and headers
76
-
77
- // Set a timeout to abort the request if it takes too long
78
- const timeout = setTimeout ( ( ) => controller . abort ( ) , this . timeout ) ;
79
-
80
68
try {
81
- const response = await fetch ( url , options ) ;
82
- clearTimeout ( timeout ) ; // Clear the timeout if the request completes in time
69
+ const response = await fetch ( url , {
70
+ ...options ,
71
+ signal : AbortSignal . any ( [
72
+ ...( options ?. signal ? [ options . signal ] : [ ] ) ,
73
+ AbortSignal . timeout ( this . timeout ) ,
74
+ ] ) ,
75
+ headers : { ...this . headers , ...options ?. headers } ,
76
+ } ) ;
83
77
if ( ! response . ok ) {
84
78
const errorBody = await response . text ( ) ;
85
79
throw new Error (
@@ -91,7 +85,6 @@ export class HermesClient {
91
85
const data = await response . json ( ) ;
92
86
return schema . parse ( data ) ;
93
87
} catch ( error ) {
94
- clearTimeout ( timeout ) ;
95
88
if (
96
89
retries > 0 &&
97
90
! ( error instanceof Error && error . name === "AbortError" )
@@ -115,17 +108,22 @@ export class HermesClient {
115
108
*
116
109
* @returns Array of PriceFeedMetadata objects.
117
110
*/
118
- async getPriceFeeds ( options ?: {
111
+ async getPriceFeeds ( {
112
+ fetchOptions,
113
+ ...options
114
+ } : {
119
115
query ?: string ;
120
116
filter ?: string ;
121
- } ) : Promise < PriceFeedMetadata [ ] > {
117
+ fetchOptions ?: RequestInit ;
118
+ } = { } ) : Promise < PriceFeedMetadata [ ] > {
122
119
const url = this . buildURL ( "price_feeds" ) ;
123
120
if ( options ) {
124
121
this . appendUrlSearchParams ( url , options ) ;
125
122
}
126
123
return await this . httpRequest (
127
124
url . toString ( ) ,
128
- schemas . PriceFeedMetadata . array ( )
125
+ schemas . PriceFeedMetadata . array ( ) ,
126
+ fetchOptions
129
127
) ;
130
128
}
131
129
@@ -140,10 +138,14 @@ export class HermesClient {
140
138
*
141
139
* @returns PublisherCaps object containing the latest publisher stake caps.
142
140
*/
143
- async getLatestPublisherCaps ( options ?: {
141
+ async getLatestPublisherCaps ( {
142
+ fetchOptions,
143
+ ...options
144
+ } : {
144
145
encoding ?: EncodingType ;
145
146
parsed ?: boolean ;
146
- } ) : Promise < PublisherCaps > {
147
+ fetchOptions ?: RequestInit ;
148
+ } = { } ) : Promise < PublisherCaps > {
147
149
const url = this . buildURL ( "updates/publisher_stake_caps/latest" ) ;
148
150
if ( options ) {
149
151
this . appendUrlSearchParams ( url , options ) ;
@@ -173,7 +175,8 @@ export class HermesClient {
173
175
encoding ?: EncodingType ;
174
176
parsed ?: boolean ;
175
177
ignoreInvalidPriceIds ?: boolean ;
176
- }
178
+ } ,
179
+ fetchOptions ?: RequestInit
177
180
) : Promise < PriceUpdate > {
178
181
const url = this . buildURL ( "updates/price/latest" ) ;
179
182
for ( const id of ids ) {
@@ -185,7 +188,7 @@ export class HermesClient {
185
188
this . appendUrlSearchParams ( url , transformedOptions ) ;
186
189
}
187
190
188
- return this . httpRequest ( url . toString ( ) , schemas . PriceUpdate ) ;
191
+ return this . httpRequest ( url . toString ( ) , schemas . PriceUpdate , fetchOptions ) ;
189
192
}
190
193
191
194
/**
@@ -209,7 +212,8 @@ export class HermesClient {
209
212
encoding ?: EncodingType ;
210
213
parsed ?: boolean ;
211
214
ignoreInvalidPriceIds ?: boolean ;
212
- }
215
+ } ,
216
+ fetchOptions ?: RequestInit
213
217
) : Promise < PriceUpdate > {
214
218
const url = this . buildURL ( `updates/price/${ publishTime } ` ) ;
215
219
for ( const id of ids ) {
@@ -221,7 +225,7 @@ export class HermesClient {
221
225
this . appendUrlSearchParams ( url , transformedOptions ) ;
222
226
}
223
227
224
- return this . httpRequest ( url . toString ( ) , schemas . PriceUpdate ) ;
228
+ return this . httpRequest ( url . toString ( ) , schemas . PriceUpdate , fetchOptions ) ;
225
229
}
226
230
227
231
/**
@@ -286,7 +290,8 @@ export class HermesClient {
286
290
encoding ?: EncodingType ;
287
291
parsed ?: boolean ;
288
292
ignoreInvalidPriceIds ?: boolean ;
289
- }
293
+ } ,
294
+ fetchOptions ?: RequestInit
290
295
) : Promise < TwapsResponse > {
291
296
const url = this . buildURL ( `updates/twap/${ window_seconds } /latest` ) ;
292
297
for ( const id of ids ) {
@@ -298,7 +303,11 @@ export class HermesClient {
298
303
this . appendUrlSearchParams ( url , transformedOptions ) ;
299
304
}
300
305
301
- return this . httpRequest ( url . toString ( ) , schemas . TwapsResponse ) ;
306
+ return this . httpRequest (
307
+ url . toString ( ) ,
308
+ schemas . TwapsResponse ,
309
+ fetchOptions
310
+ ) ;
302
311
}
303
312
304
313
private appendUrlSearchParams (
0 commit comments