-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: replace bitcoin usd price fetch api url (#244)
- Loading branch information
1 parent
b5978ce
commit adfc6f9
Showing
2 changed files
with
8 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,22 @@ | ||
import { BitcoinError } from '@models/error-types'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { UseQueryResult, useQuery } from '@tanstack/react-query'; | ||
|
||
interface UseBitcoinPriceReturnType { | ||
bitcoinPrice: number | undefined; | ||
} | ||
|
||
export function useBitcoinPrice(): UseBitcoinPriceReturnType { | ||
export function useBitcoinPrice(): UseQueryResult<number, BitcoinError> { | ||
const fetchBitcoinPrice = async (): Promise<number> => { | ||
try { | ||
const response = await fetch('https://api.coindesk.com/v1/bpi/currentprice.json'); | ||
const response = await fetch( | ||
'https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd' | ||
); | ||
const data = await response.json(); | ||
return data.bpi.USD.rate_float; | ||
return data.bitcoin.usd; | ||
} catch (error) { | ||
throw new BitcoinError(`Error fetching Bitcoin price: ${error}`); | ||
} | ||
}; | ||
|
||
const { data: bitcoinPrice } = useQuery<number, BitcoinError>({ | ||
return useQuery<number, BitcoinError>({ | ||
queryKey: ['bitcoinPrice'], | ||
queryFn: fetchBitcoinPrice, | ||
refetchInterval: 60000, | ||
}); | ||
|
||
return { bitcoinPrice }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters