Skip to content

Commit 6255a32

Browse files
committed
Merge branch 'feat/retry-logic' of github.com:MetaMask/snap-7715-permissions into feat/account-api-v2
2 parents b028ad7 + d7be1fd commit 6255a32

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

packages/gator-permissions-snap/src/clients/accountApiClient.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,10 @@ export class AccountApiClient {
290290
): Promise<TokenBalanceResponse> {
291291
const url = `${this.#accountBaseUrl}/v2/accounts/${account}/balances?networks=${chainId}&filterSupportedTokens=false&includeTokenAddresses=${tokenAddress}&includeStakedAssets=false`;
292292

293-
return (await makeValidatedRequestWithRetry(
293+
return await makeValidatedRequestWithRetry<
294+
TokenBalanceResponse,
295+
typeof TokenBalanceResponseSchema
296+
>(
294297
url,
295298
{
296299
timeoutMs: this.#timeoutMs,
@@ -299,7 +302,7 @@ export class AccountApiClient {
299302
},
300303
TokenBalanceResponseSchema,
301304
retryOptions,
302-
)) as TokenBalanceResponse;
305+
);
303306
}
304307

305308
/**
@@ -316,7 +319,10 @@ export class AccountApiClient {
316319
): Promise<TokenMetadataResponse> {
317320
const url = `${this.#tokensBaseUrl}/token/${chainId}?address=${tokenAddress}&includeEnrichedData=false&includeCoingeckoId=false&includeAggregators=false&includeOccurrences=false&includeIconUrl=true&includeAssetType=false&includeTokenFees=false&includeHoneypotStatus=false&includeContractVerificationStatus=false&includeStorage=false&includeERC20Permit=false&includeDescription=false`;
318321

319-
return (await makeValidatedRequestWithRetry(
322+
return await makeValidatedRequestWithRetry<
323+
TokenMetadataResponse,
324+
typeof TokenMetadataResponseSchema
325+
>(
320326
url,
321327
{
322328
timeoutMs: this.#timeoutMs,
@@ -325,6 +331,6 @@ export class AccountApiClient {
325331
},
326332
TokenMetadataResponseSchema,
327333
retryOptions,
328-
)) as TokenMetadataResponse;
334+
);
329335
}
330336
}

packages/gator-permissions-snap/src/utils/httpClient.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)