Skip to content

Commit bb26df9

Browse files
authored
Update useTokenPrices.tsx
axios fix
1 parent 3a21acf commit bb26df9

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

src/hooks/useTokenPrices.tsx

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
11
import useSWR from 'swr';
2+
import axios from 'axios';
3+
24
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+
}
1321
};
1422

1523
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+
});
2230

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+
};
2836
}

0 commit comments

Comments
 (0)