File tree Expand file tree Collapse file tree 2 files changed +52
-0
lines changed
Expand file tree Collapse file tree 2 files changed +52
-0
lines changed Original file line number Diff line number Diff line change 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 [
Original file line number Diff line number Diff line change 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+ } ;
You can’t perform that action at this time.
0 commit comments