-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurrency-exchange-rate.js
62 lines (56 loc) · 2.91 KB
/
currency-exchange-rate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const axios = require('axios');
var getCurrencyExchangeRatePromiseCallback = function (config, callbackFunction, resolve, reject) {
try {
if (config && typeof (config.fromCurrency) == 'string' && typeof (config.toCurrency) == 'string') {
var yahooFinanceUrl = "https://query1.finance.yahoo.com/v8/finance/chart/" + config.fromCurrency.trim().toUpperCase() + config.toCurrency.trim().toUpperCase() + "=X?region=US&lang=en-US&includePrePost=false&interval=2m&useYfid=true&range=1d&corsDomain=finance.yahoo.com&.tsrc=finance";
var axiosConfig = config;
var defaultHeaders = {
"accept": "*/*",
"accept-encoding": "gzip, deflate, br",
"user-agent": "Axios",
"keep-alive": "true",
"cache-control": "no-cache"
}
axiosConfig.headers = ((typeof (axiosConfig.headers) == 'object') ? axiosConfig.headers : {});
axiosConfig.headers = Object.assign(axiosConfig.headers, defaultHeaders);
axiosConfig.url = yahooFinanceUrl;
axiosConfig.method = "get";
axios.get(yahooFinanceUrl, axiosConfig).then(function (response) {
if (response && response.status === 200 && response.data && response.data.chart && response.data.chart.result
&& Array.isArray(response.data.chart.result) && response.data.chart.result.length > 0
&& response.data.chart.result[0].meta && typeof (response.data.chart.result[0].meta.regularMarketPrice) == 'number') {
var conversionRate = response.data.chart.result[0].meta.regularMarketPrice;
if (typeof (callbackFunction) == "function") {
callbackFunction(conversionRate, undefined);
}
return resolve(conversionRate);
}
else {
if (typeof (callbackFunction) == "function") {
callbackFunction(undefined, "Backend service didn't return a HTTP 200 response");
}
return reject("Backend service didn't return a HTTP 200 response");
}
}).catch(function (err) {
if (typeof (callbackFunction) == "function") {
callbackFunction(undefined, err);
}
});
}
else {
throw "fromCurrency and toCurrency configs are required";
}
}
catch (e) {
if (typeof (callbackFunction) == "function") {
callbackFunction(undefined, e);
}
return reject(e);
}
};
var getCurrencyExchangeRate = function (config, callbackFunction) {
return new Promise(function (resolve, reject) {
return getCurrencyExchangeRatePromiseCallback(config, callbackFunction, resolve, reject);
});
};
module.exports.getCurrencyExchangeRate = getCurrencyExchangeRate;