Created
July 29, 2022 10:09
-
-
Save adrianhajdin/8f9f36dc3b789b2899bbcf80493b2a1e to your computer and use it in GitHub Desktop.
I Abandoned Postman for This NEW VS Code Extension | Build, Test & Sell APIs 🤑
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
import axios from 'axios'; | |
const getCountries = async (currencyCode) => { | |
try { | |
const response = await axios.get(`https://restcountries.com/v3.1/currency/${currencyCode}`); | |
return response.data.map(country => country.name.common); | |
} catch (error) { | |
throw new Error(`Unable to get countries that use ${currencyCode}`); | |
} | |
}; | |
const convertCurrency = async (fromCurrency, toCurrency, amount) => { | |
const countries = await getCountries(toCurrency); | |
const convertedAmount = (amount * Math.random() * 11).toFixed(2); | |
return `${amount} ${fromCurrency} is worth ${convertedAmount} ${toCurrency}. You can spend these in the following countries: ${countries}`; | |
}; | |
convertCurrency('USD', 'CHF', 20) | |
.then((data) => console.log(data)) | |
.catch((error) => console.log(error)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment