/ test-api / src / utils / apiUtils.js
apiUtils.js
 1  // Common API utilities and configurations
 2  
 3  /**
 4   * Get the configured proxy URL from environment or default
 5   * @returns {string} Proxy URL
 6   */
 7  export const getProxyUrl = () => {
 8    return process.env.REACT_APP_RPC_PROXY_URL || 'http://localhost:8080';
 9  };
10  
11  /**
12   * Common axios configuration for API requests
13   * @param {string} token - Optional JWT token
14   * @param {Object} basicAuth - Optional basic auth {username, password}
15   * @returns {Object} Axios config object
16   */
17  export const getApiConfig = (token = null, basicAuth = null) => {
18    const config = {
19      headers: {
20        'Content-Type': 'application/json'
21      },
22      timeout: 10000
23    };
24  
25    if (token) {
26      config.headers.Authorization = `Bearer ${token}`;
27    }
28  
29    if (basicAuth && basicAuth.username && basicAuth.password) {
30      config.auth = {
31        username: basicAuth.username,
32        password: basicAuth.password
33      };
34    }
35  
36    return config;
37  };
38  
39  /**
40   * Standard error handler for API responses
41   * @param {Error} error - Axios error object
42   * @returns {Object} Formatted error object
43   */
44  export const handleApiError = (error) => {
45    return {
46      status: error.response?.status || 'Network Error',
47      message: error.response?.data?.error?.message || error.response?.data || error.message || 'Unknown error',
48      data: error.response?.data
49    };
50  };
51  
52  /**
53   * Available networks configuration
54   */
55  export const AVAILABLE_NETWORKS = [
56    { value: 'ethereum/mainnet', label: 'Ethereum Mainnet', chainId: 1 },
57    { value: 'ethereum/sepolia', label: 'Ethereum Sepolia', chainId: 11155111 },
58    { value: 'optimism/mainnet', label: 'Optimism Mainnet', chainId: 10 },
59    { value: 'optimism/sepolia', label: 'Optimism Sepolia', chainId: 11155420 },
60    { value: 'arbitrum/mainnet', label: 'Arbitrum One', chainId: 42161 },
61    { value: 'arbitrum/sepolia', label: 'Arbitrum Sepolia', chainId: 421614 },
62    { value: 'base/mainnet', label: 'Base Mainnet', chainId: 8453 },
63    { value: 'base/sepolia', label: 'Base Sepolia', chainId: 84532 },
64    { value: 'linea/mainnet', label: 'Linea Mainnet', chainId: 59144 },
65    { value: 'linea/sepolia', label: 'Linea Sepolia', chainId: 59141 },
66    { value: 'blast/mainnet', label: 'Blast Mainnet', chainId: 81457 },
67    { value: 'blast/sepolia', label: 'Blast Sepolia', chainId: 168587773 },
68    { value: 'zksync/mainnet', label: 'zkSync Era', chainId: 324 },
69    { value: 'zksync/sepolia', label: 'zkSync Sepolia', chainId: 300 },
70    { value: 'mantle/mainnet', label: 'Mantle Mainnet', chainId: 5000 },
71    { value: 'mantle/sepolia', label: 'Mantle Sepolia', chainId: 5003 },
72    { value: 'abstract/mainnet', label: 'Abstract Mainnet', chainId: 2741 },
73    { value: 'abstract/testnet', label: 'Abstract Testnet', chainId: 11124 },
74    { value: 'unichain/mainnet', label: 'Unichain Mainnet', chainId: 130 },
75    { value: 'unichain/sepolia', label: 'Unichain Sepolia', chainId: 1301 },
76    { value: 'status/sepolia', label: 'Status Sepolia', chainId: 1660990954 },
77    { value: 'bsc/mainnet', label: 'BSC Mainnet', chainId: 56 },
78    { value: 'bsc/testnet', label: 'BSC Testnet', chainId: 97 },
79    { value: 'polygon/mainnet', label: 'Polygon Mainnet', chainId: 137 },
80    { value: 'polygon/amoy', label: 'Polygon Amoy', chainId: 80002 }
81  ];
82  
83  /**
84   * Predefined RPC methods for testing
85   */
86  export const PREDEFINED_RPC_METHODS = [
87    { name: 'Get Block Number', method: 'eth_blockNumber', params: [], cacheType: 'short' },
88    { name: 'Get Gas Price', method: 'eth_gasPrice', params: [], cacheType: 'minimal' },
89    { name: 'Get Chain ID', method: 'eth_chainId', params: [], cacheType: 'permanent' },
90    { name: 'Get Latest Block', method: 'eth_getBlockByNumber', params: ['latest', false], cacheType: 'short' },
91    { name: 'Get Balance (Vitalik)', method: 'eth_getBalance', params: ['0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045', 'latest'], cacheType: 'short' }
92  ];