@@ -31,10 +31,13 @@ export type HttpClientConfig = {
3131 * @throws {ParseError } If the response cannot be parsed as JSON.
3232 * @throws {ResourceUnavailableError } If the response structure is invalid according to the schema.
3333 */
34- export async function makeValidatedRequestWithRetry < TResponse > (
34+ export async function makeValidatedRequestWithRetry <
35+ TResponse ,
36+ TSchema extends z . ZodType < TResponse , any , any > ,
37+ > (
3538 url : string ,
3639 config : HttpClientConfig ,
37- responseSchema : z . ZodSchema < TResponse > ,
40+ responseSchema : TSchema ,
3841 retryOptions ?: RetryOptions ,
3942) : Promise < TResponse > {
4043 const { retries = 1 , delayMs = 1000 } = retryOptions ?? { } ;
@@ -68,10 +71,13 @@ export async function makeValidatedRequestWithRetry<TResponse>(
6871 * @throws {ParseError } If the response cannot be parsed as JSON.
6972 * @throws {ResourceUnavailableError } If the response structure is invalid according to the schema.
7073 */
71- export async function makeValidatedRequest < TResponse > (
74+ async function makeValidatedRequest <
75+ TResponse ,
76+ TSchema extends z . ZodType < TResponse , any , any > ,
77+ > (
7278 url : string ,
7379 config : HttpClientConfig ,
74- responseSchema : z . ZodSchema < TResponse > ,
80+ responseSchema : TSchema ,
7581) : Promise < TResponse > {
7682 const { timeoutMs, maxResponseSizeBytes, fetch = globalThis . fetch } = config ;
7783
@@ -89,8 +95,6 @@ export async function makeValidatedRequest<TResponse>(
8995 } ,
9096 } ) ;
9197 } catch ( error ) {
92- clearTimeout ( timeoutId ) ;
93-
9498 if ( error instanceof Error && error . name === 'AbortError' ) {
9599 throw new ResourceUnavailableError (
96100 `Request timed out after ${ timeoutMs } ms` ,
@@ -100,10 +104,10 @@ export async function makeValidatedRequest<TResponse>(
100104 throw new InternalError (
101105 `Failed to fetch resource: ${ error instanceof Error ? error . message : 'Unknown error' } ` ,
102106 ) ;
107+ } finally {
108+ clearTimeout ( timeoutId ) ;
103109 }
104110
105- clearTimeout ( timeoutId ) ;
106-
107111 // Check HTTP status code
108112 if ( ! response . ok ) {
109113 if ( response . status === 404 ) {
0 commit comments