Skip to content

Commit 852b5f9

Browse files
authored
Update useTokenPrices.tsx
fixes
1 parent bb9ce83 commit 852b5f9

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

src/hooks/useTokenPrices.tsx

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
11
import useSWR from 'swr';
2-
import axios from 'axios';
32

43
const X_API_Key = process.env.NEXT_PUBLIC_X_API_KEY;
54

65
const fetcher = async (url: string) => {
7-
const config = {
8-
method: 'get',
9-
url: url,
6+
// Ensure the API key is not undefined
7+
if (!X_API_Key) {
8+
throw new Error('API key is missing');
9+
}
10+
11+
12+
const response = await fetch(url, {
1013
headers: {
11-
'x-api-key': X_API_Key ?? '',
14+
'x-api-key': X_API_Key,
1215
},
13-
};
16+
});
1417

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');
18+
19+
if (!response.ok) {
20+
throw new Error(`Failed to fetch data: ${response.statusText}`);
2021
}
22+
23+
24+
return response.json();
2125
};
2226

2327
export function useTokenPrices() {
2428
const { data, error } = useSWR('https://api.jiffyscan.xyz/v0/getPrices', fetcher, {
2529
refreshInterval: 300000, // 5 mins
2630
onSuccess: (data) => {
31+
// Cache the data in localStorage
2732
localStorage.setItem('tokenPrices', JSON.stringify(data));
2833
},
2934
});

0 commit comments

Comments
 (0)