Skip to content

Commit 2c0d5ec

Browse files
committed
chore: Added support for exchangerate api
1 parent 827ce15 commit 2c0d5ec

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

config.example.hjson

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
duckduckgo: {
3535
"base_url": https://duckduckgo.com/js/spice/currency/1/
3636
}
37+
exchangeratapi: {
38+
api_key: TOKEN_EXCHANGERATE
39+
base_url: https://v6.exchangerate-api.com/v6/
40+
}
3741
}
3842
fiat:
3943
[
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const axios = require('axios');
2+
const config = require('../../shared/config/src/main.js')();
3+
4+
module.exports = {
5+
info: {
6+
name: 'ExchangeRateAPI',
7+
type: 'fiat',
8+
},
9+
parseCurrencies: async () => {
10+
const promises = config['currency']['fiat'].map((fromCurrency) =>
11+
config['currency']['fiat'].map((convCurrency) => {
12+
if (fromCurrency === convCurrency) return Promise.resolve(null);
13+
14+
const exchangeratapi =
15+
config['currency']['services']['exchangeratapi'];
16+
const serviceName = module.exports.info.name;
17+
18+
return axios
19+
.get(
20+
`${exchangeratapi['base_url']}/${exchangeratapi['api_key']}/pair/${fromCurrency}/${convCurrency}`,
21+
)
22+
.then((res) => {
23+
const data = res.data;
24+
25+
console.log(
26+
`Data fetched from ${serviceName}: ${fromCurrency} -> ${convCurrency}, Rate: ${data.conversion_rate}`,
27+
);
28+
29+
return {
30+
from_currency: fromCurrency,
31+
conv_currency: convCurrency,
32+
rate: data.conversion_rate,
33+
date: new Date(),
34+
};
35+
})
36+
.catch((err) => {
37+
console.error(err);
38+
return null;
39+
});
40+
}),
41+
);
42+
43+
const flattenedPromises = promises.flat();
44+
const results = await Promise.all(flattenedPromises);
45+
46+
return results.filter((result) => result !== null);
47+
},
48+
};

0 commit comments

Comments
 (0)