Skip to content

fetchCurrencies() #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 137 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@
"bugs": {
"url": "https://github.com/JavaScript-Bits/countries-with-cities-select/issues"
},
"homepage": "https://github.com/JavaScript-Bits/countries-with-cities-select#readme"
"homepage": "https://github.com/JavaScript-Bits/countries-with-cities-select#readme",
"dependencies": {
"currency-explorer": "^1.0.1"
}
}
24 changes: 24 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const countriesData = require("./country-cities.json")
const CurrencyExplorer = require('currency-explorer');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this constant should be camel case
also we only need const { currencies } = require('currency-explorer');

also kindly lazy load this inside fetchCurrencies as it's only used there


function getCountries() {
return Object.keys(countriesData)
Expand Down Expand Up @@ -56,8 +57,31 @@ function getCities(filter) {
)
}

async function fetchCurrencies() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the name should be descriptive to what the function does , getCountriesWithCurecy

const currencyExplorer = new CurrencyExplorer();
const countryCurrencies = {};

const countries = getCountries();

// Iterate over each country
for (const country of countries) {
try {
const currencyData = await currencyExplorer.getCurrencyByCountry(
country
);
countryCurrencies[country] = currencyData.currencies;
} catch (error) {
console.error(`Failed to fetch currency for ${country}:`, error);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather suggest we return an empty currency field, rather than throw an error, this is to have consistent currency type|I'll prefer string | null to string | undefined

}
}

// Return the object containing country currencies
return countryCurrencies;
}

module.exports = {
getCountries,
getCountriesWithDetails,
getCities,
fetchCurrencies,
}
82 changes: 82 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,85 @@
# yarn lockfile v1


asynckit@^0.4.0:
version "0.4.0"
resolved "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz"
integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==

axios@^1.4.0:
version "1.4.0"
resolved "https://registry.npmjs.org/axios/-/axios-1.4.0.tgz"
integrity sha512-S4XCWMEmzvo64T9GfvQDOXgYRDJ/wsSZc7Jvdgx5u1sd0JwsuPLqb3SYmusag+edF6ziyMensPVqLTSc1PiSEA==
dependencies:
follow-redirects "^1.15.0"
form-data "^4.0.0"
proxy-from-env "^1.1.0"

combined-stream@^1.0.8:
version "1.0.8"
resolved "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz"
integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==
dependencies:
delayed-stream "~1.0.0"

currency-codes@^2.1.0:
version "2.1.0"
resolved "https://registry.npmjs.org/currency-codes/-/currency-codes-2.1.0.tgz"
integrity sha512-aASwFNP8VjZ0y0PWlSW7c9N/isYTLxK6OCbm7aVuQMk7dWO2zgup9KGiFQgeL9OGL5P/ulvCHcjQizmuEeZXtw==
dependencies:
first-match "~0.0.1"
nub "~0.0.0"

currency-explorer@^1.0.1:
version "1.0.1"
resolved "https://registry.npmjs.org/currency-explorer/-/currency-explorer-1.0.1.tgz"
integrity sha512-+1kmNoY/5g0ByPGfHg/qoQGybl0OUgza0uFscNigi8bJQdysnDxiebz9LAbi1dZNRkfx7qyAIkPoG1NOVhj/oA==
dependencies:
axios "^1.4.0"
currency-codes "^2.1.0"

delayed-stream@~1.0.0:
version "1.0.0"
resolved "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz"
integrity sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==

first-match@~0.0.1:
version "0.0.1"
resolved "https://registry.npmjs.org/first-match/-/first-match-0.0.1.tgz"
integrity sha512-VvKbnaxrC0polTFDC+teKPTdl2mn6B/KUW+WB3C9RzKDeNwbzfLdnUz3FxC+tnjvus6bI0jWrWicQyVIPdS37A==

follow-redirects@^1.15.0:
version "1.15.2"
resolved "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.2.tgz"
integrity sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==

form-data@^4.0.0:
version "4.0.0"
resolved "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz"
integrity sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==
dependencies:
asynckit "^0.4.0"
combined-stream "^1.0.8"
mime-types "^2.1.12"

[email protected]:
version "1.52.0"
resolved "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz"
integrity sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==

mime-types@^2.1.12:
version "2.1.35"
resolved "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz"
integrity sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==
dependencies:
mime-db "1.52.0"

nub@~0.0.0:
version "0.0.0"
resolved "https://registry.npmjs.org/nub/-/nub-0.0.0.tgz"
integrity sha512-dK0Ss9C34R/vV0FfYJXuqDAqHlaW9fvWVufq9MmGF2umCuDbd5GRfRD9fpi/LiM0l4ZXf8IBB+RYmZExqCrf0w==

proxy-from-env@^1.1.0:
version "1.1.0"
resolved "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz"
integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==