|
1 | 1 | import useSWR from 'swr'; |
| 2 | +import axios from 'axios'; |
| 3 | + |
2 | 4 | const X_API_Key = process.env.NEXT_PUBLIC_X_API_KEY; |
3 | | -const fetcher = async (url : string) => { |
4 | | - const response = await fetch(url, { |
5 | | - headers: { |
6 | | - 'x-api-key': X_API_Key ?? '', |
7 | | - }, |
8 | | - }); |
9 | | - if (!response.ok) { |
10 | | - throw new Error('Failed to fetch token prices'); |
11 | | - } |
12 | | - return response.json(); |
| 5 | + |
| 6 | +const fetcher = async (url: string) => { |
| 7 | + const config = { |
| 8 | + method: 'get', |
| 9 | + url: url, |
| 10 | + headers: { |
| 11 | + 'x-api-key': X_API_Key ?? '', |
| 12 | + }, |
| 13 | + }; |
| 14 | + |
| 15 | + try { |
| 16 | + const response = await axios.request(config); |
| 17 | + return response.data; |
| 18 | + } catch (error) { |
| 19 | + throw new Error('Failed to fetch token prices'); |
| 20 | + } |
13 | 21 | }; |
14 | 22 |
|
15 | 23 | export function useTokenPrices() { |
16 | | - const { data, error } = useSWR('https://api.jiffyscan.xyz/v0/getPrices', fetcher, { |
17 | | - refreshInterval: 300000, // 5 mins |
18 | | - onSuccess: (data) => { |
19 | | - localStorage.setItem('tokenPrices', JSON.stringify(data)); |
20 | | - }, |
21 | | - }); |
| 24 | + const { data, error } = useSWR('https://api.jiffyscan.xyz/v0/getPrices', fetcher, { |
| 25 | + refreshInterval: 300000, // 5 mins |
| 26 | + onSuccess: (data) => { |
| 27 | + localStorage.setItem('tokenPrices', JSON.stringify(data)); |
| 28 | + }, |
| 29 | + }); |
22 | 30 |
|
23 | | - return { |
24 | | - tokenPrices: data, |
25 | | - isLoading: !error && !data, |
26 | | - isError: error, |
27 | | - }; |
| 31 | + return { |
| 32 | + tokenPrices: data, |
| 33 | + isLoading: !error && !data, |
| 34 | + isError: error, |
| 35 | + }; |
28 | 36 | } |
0 commit comments