utils.ts


Overview

The `utils.ts` file provides a set of utility functions and configurations primarily focused on error handling, HTTP request retry logic, pagination validation, and JSON-RPC request ID generation. These utilities support robust API interaction by standardizing error transformation, enforcing safe page size limits, and enabling resilient HTTP client behavior with configurable retry strategies. This file is intended to be a foundational helper module used across various parts of the application that require HTTP communication and error management.


Detailed Descriptions

Constants

MAX_PAGE_SIZE: number


Functions

validatePageSize(pageSize: number): void


handleError(err: unknown): ApiError


createAxiosRetry(config: RetryConfig, axiosParams?: CreateAxiosDefaults): axios.AxiosInstance


exponentialDelay(retryCount: number): Promise<void>


rpcId(): number


Important Implementation Details and Algorithms


Interactions with Other Parts of the System

Overall, `utils.ts` acts as a foundational utility module supporting network communication, error handling, and API request validation across the application.


Mermaid Flowchart Diagram

flowchart TD
    A[validatePageSize(pageSize)] -->|throws ApiError if invalid| B[ApiError]
    C[handleError(err)] --> D{Error Type?}
    D -->|ApiError| E[Return err]
    D -->|AxiosError| F[Create ApiError from Axios response]
    D -->|BlockbookApiError| G[Create ApiError from Blockbook response]
    D -->|Generic Error| H[Create ApiError with 500 status]
    D -->|Unknown| H
    I[createAxiosRetry(config, axiosParams)] --> J[Create axios instance]
    J --> K[Apply axiosRetry with config]
    K -->|RetryCondition| L[Network errors, 405-599 status, timeout]
    K -->|RetryDelay| M[Exponential backoff delay]
    N[exponentialDelay(retryCount)] --> M
    O[rpcId()] --> P[Increment and return unique RPC ID]

Summary

The `utils.ts` file provides robust and reusable utilities essential for:

It interacts closely with the error handling architecture (`ApiError`), network request layers, and external blockchain APIs, making it a critical support module in the application’s infrastructure.