google-finance-quote 3.0.0 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -3
- package/dist/cjs/index.js +75 -0
- package/dist/cjs/lib/config.js +4 -0
- package/dist/cjs/lib/cryptoCurrencyCodesSymbols.js +88 -0
- package/dist/cjs/lib/currencyCodesSymbols.js +125 -0
- package/dist/cjs/lib/symbols.js +210 -0
- package/dist/esm/index.mjs +66 -0
- package/dist/esm/lib/config.js +1 -0
- package/dist/esm/lib/cryptoCurrencyCodesSymbols.js +85 -0
- package/dist/esm/lib/currencyCodesSymbols.js +122 -0
- package/dist/esm/lib/symbols.js +207 -0
- package/package.json +22 -6
- package/index.d.ts +0 -57
- package/index.js +0 -108
- package/lib/config.js +0 -3
- package/lib/cryptoCurrencyCodesSymbols.js +0 -93
- package/lib/currencyCodesSymbols.js +0 -130
- package/lib/symbols.js +0 -214
package/README.md
CHANGED
|
@@ -2,14 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
Node Google Finance API wrapper for free.
|
|
4
4
|
No API key is required!
|
|
5
|
-
|
|
6
|
-
> Note
|
|
5
|
+
|
|
6
|
+
> ![Note]
|
|
7
|
+
> This results may vary by up to 20 minutes.
|
|
8
|
+
|
|
9
|
+
> ![Note]
|
|
10
|
+
> `3.0.0 <= x` doesn't support proxies.
|
|
7
11
|
|
|
8
12
|
## Usage
|
|
9
13
|
|
|
10
14
|
### Get Started
|
|
11
15
|
|
|
12
16
|
```js
|
|
17
|
+
// ESM
|
|
18
|
+
import { Finance, symbols, currencyCodesSymbols, cryptoCurrencyCodesSymbols } from "google-finance-quote";
|
|
19
|
+
// CJS
|
|
13
20
|
const { Finance, symbols, currencyCodesSymbols, cryptoCurrencyCodesSymbols } = require("google-finance-quote");
|
|
14
21
|
|
|
15
22
|
console.log(symbols); // Returns available symbols.
|
|
@@ -19,7 +26,7 @@ console.log(cryptoCurrencyCodesSymbols); // Returns available crypto currency co
|
|
|
19
26
|
const finance = new Finance(); // You can use this: new Finance({ from 'USD', to: 'JPY' });
|
|
20
27
|
|
|
21
28
|
finance
|
|
22
|
-
.setFrom('USD')
|
|
29
|
+
.setFrom('USD')
|
|
23
30
|
.setTo('JPY');
|
|
24
31
|
|
|
25
32
|
(async () => {
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.cryptocurrencyCodesSymbols = exports.currencyCodesSymbols = exports.symbols = exports.Finance = void 0;
|
|
7
|
+
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
const config_1 = require("./lib/config");
|
|
9
|
+
const symbols_1 = require("./lib/symbols");
|
|
10
|
+
Object.defineProperty(exports, "symbols", { enumerable: true, get: function () { return symbols_1.symbols; } });
|
|
11
|
+
const currencyCodesSymbols_1 = require("./lib/currencyCodesSymbols");
|
|
12
|
+
Object.defineProperty(exports, "currencyCodesSymbols", { enumerable: true, get: function () { return currencyCodesSymbols_1.currencyCodesSymbols; } });
|
|
13
|
+
const cryptoCurrencyCodesSymbols_1 = require("./lib/cryptoCurrencyCodesSymbols");
|
|
14
|
+
Object.defineProperty(exports, "cryptocurrencyCodesSymbols", { enumerable: true, get: function () { return cryptoCurrencyCodesSymbols_1.cryptocurrencyCodesSymbols; } });
|
|
15
|
+
class Finance {
|
|
16
|
+
constructor(p) {
|
|
17
|
+
var _a, _b, _c, _d;
|
|
18
|
+
if (typeof p === "object" &&
|
|
19
|
+
(symbols_1.symbols.includes(((_a = p === null || p === void 0 ? void 0 : p.from) === null || _a === void 0 ? void 0 : _a.toUpperCase()) || "") ||
|
|
20
|
+
symbols_1.symbols.includes(((_b = p === null || p === void 0 ? void 0 : p.to) === null || _b === void 0 ? void 0 : _b.toUpperCase()) || ""))) {
|
|
21
|
+
this.param = {
|
|
22
|
+
from: ((_c = p.from) === null || _c === void 0 ? void 0 : _c.toUpperCase()) || null,
|
|
23
|
+
to: ((_d = p.to) === null || _d === void 0 ? void 0 : _d.toUpperCase()) || null,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
else if (typeof p === "undefined") {
|
|
27
|
+
this.param = { from: null, to: null };
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
throw new Error("Invalid parameters.");
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
setFrom(from) {
|
|
34
|
+
if (!symbols_1.symbols.includes(from === null || from === void 0 ? void 0 : from.toUpperCase()))
|
|
35
|
+
throw new Error("invalid from.");
|
|
36
|
+
this.param.from = from.toUpperCase();
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
setTo(to) {
|
|
40
|
+
if (!symbols_1.symbols.includes(to === null || to === void 0 ? void 0 : to.toUpperCase()))
|
|
41
|
+
throw new Error("invalid to.");
|
|
42
|
+
this.param.to = to.toUpperCase();
|
|
43
|
+
return this;
|
|
44
|
+
}
|
|
45
|
+
getParam() {
|
|
46
|
+
return this.param;
|
|
47
|
+
}
|
|
48
|
+
async quote(amount = 1) {
|
|
49
|
+
const result = { success: false, rate: 0 };
|
|
50
|
+
try {
|
|
51
|
+
if (typeof amount !== "number")
|
|
52
|
+
throw new Error("amount must be number.");
|
|
53
|
+
const from = this.param.from;
|
|
54
|
+
const to = this.param.to;
|
|
55
|
+
if (!(symbols_1.symbols === null || symbols_1.symbols === void 0 ? void 0 : symbols_1.symbols.includes(from || "")) || !(symbols_1.symbols === null || symbols_1.symbols === void 0 ? void 0 : symbols_1.symbols.includes(to || "")))
|
|
56
|
+
throw new Error("from and/or to are invalid.");
|
|
57
|
+
const url = `${config_1.API_URL}${from}-${to}`;
|
|
58
|
+
const response = await axios_1.default.get(url);
|
|
59
|
+
const html = response.data;
|
|
60
|
+
const startIndex = html.indexOf(`data-source="${from}" data-target="${to}" data-last-price="`) +
|
|
61
|
+
`data-source="${from}" data-target="${to}" data-last-price="`.length;
|
|
62
|
+
const endIndex = html.indexOf('"', startIndex);
|
|
63
|
+
const rate = Number(html.substring(startIndex, endIndex));
|
|
64
|
+
if (isNaN(rate))
|
|
65
|
+
throw new Error("Failed to get the current finance.");
|
|
66
|
+
result.success = true;
|
|
67
|
+
result.rate = rate * amount;
|
|
68
|
+
return result;
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
return result;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
exports.Finance = Finance;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.cryptocurrencyCodesSymbols = void 0;
|
|
4
|
+
exports.cryptocurrencyCodesSymbols = [
|
|
5
|
+
"BTC",
|
|
6
|
+
"ETH",
|
|
7
|
+
"ADA",
|
|
8
|
+
"BNB",
|
|
9
|
+
"USDT",
|
|
10
|
+
"XRP",
|
|
11
|
+
"DOGE",
|
|
12
|
+
"LINK",
|
|
13
|
+
"LTC",
|
|
14
|
+
"BCH",
|
|
15
|
+
"XLM",
|
|
16
|
+
"TRX",
|
|
17
|
+
"ETC",
|
|
18
|
+
"XMR",
|
|
19
|
+
"EOS",
|
|
20
|
+
"NEO",
|
|
21
|
+
"WAVES",
|
|
22
|
+
"DASH",
|
|
23
|
+
"OMG",
|
|
24
|
+
"DCR",
|
|
25
|
+
"XEM",
|
|
26
|
+
"MANA",
|
|
27
|
+
"ICX",
|
|
28
|
+
"QTUM",
|
|
29
|
+
"ZIL",
|
|
30
|
+
"BAT",
|
|
31
|
+
"BTG",
|
|
32
|
+
"BNT",
|
|
33
|
+
"ZRX",
|
|
34
|
+
"SC",
|
|
35
|
+
"ONT",
|
|
36
|
+
"DGB",
|
|
37
|
+
"NANO",
|
|
38
|
+
"LSK",
|
|
39
|
+
"GNO",
|
|
40
|
+
"XVG",
|
|
41
|
+
"ARDR",
|
|
42
|
+
"SNT",
|
|
43
|
+
"MAID",
|
|
44
|
+
"REP",
|
|
45
|
+
"ARK",
|
|
46
|
+
"FUN",
|
|
47
|
+
"STEEM",
|
|
48
|
+
"SYS",
|
|
49
|
+
"BTS",
|
|
50
|
+
"KMD",
|
|
51
|
+
"MONA",
|
|
52
|
+
"GAS",
|
|
53
|
+
"RDD",
|
|
54
|
+
"BTM",
|
|
55
|
+
"WTC",
|
|
56
|
+
"AION",
|
|
57
|
+
"GRS",
|
|
58
|
+
"BCN",
|
|
59
|
+
"VERI",
|
|
60
|
+
"DGD",
|
|
61
|
+
"PIVX",
|
|
62
|
+
"NXS",
|
|
63
|
+
"PPT",
|
|
64
|
+
"QASH",
|
|
65
|
+
"NEBL",
|
|
66
|
+
"SALT",
|
|
67
|
+
"GAME",
|
|
68
|
+
"FCT",
|
|
69
|
+
"NXT",
|
|
70
|
+
"PAY",
|
|
71
|
+
"XDN",
|
|
72
|
+
"SUB",
|
|
73
|
+
"KNC",
|
|
74
|
+
"GNT",
|
|
75
|
+
"REQ",
|
|
76
|
+
"VGX",
|
|
77
|
+
"TNB",
|
|
78
|
+
"ZCL",
|
|
79
|
+
"HSR",
|
|
80
|
+
"ICN",
|
|
81
|
+
"POWR",
|
|
82
|
+
"ENG",
|
|
83
|
+
"SAN",
|
|
84
|
+
"ELF",
|
|
85
|
+
"STRAT",
|
|
86
|
+
"QSP",
|
|
87
|
+
"RDN",
|
|
88
|
+
];
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.currencyCodesSymbols = void 0;
|
|
4
|
+
exports.currencyCodesSymbols = [
|
|
5
|
+
"AED",
|
|
6
|
+
"AFN",
|
|
7
|
+
"ALL",
|
|
8
|
+
"AMD",
|
|
9
|
+
"ANG",
|
|
10
|
+
"ARS",
|
|
11
|
+
"AUD",
|
|
12
|
+
"AWG",
|
|
13
|
+
"AZN",
|
|
14
|
+
"BAM",
|
|
15
|
+
"BDT",
|
|
16
|
+
"BGN",
|
|
17
|
+
"BHD",
|
|
18
|
+
"BND",
|
|
19
|
+
"BOB",
|
|
20
|
+
"BRL",
|
|
21
|
+
"BTN",
|
|
22
|
+
"BWP",
|
|
23
|
+
"BYN",
|
|
24
|
+
"BZD",
|
|
25
|
+
"CAD",
|
|
26
|
+
"CHF",
|
|
27
|
+
"CLP",
|
|
28
|
+
"CNY",
|
|
29
|
+
"COP",
|
|
30
|
+
"CRC",
|
|
31
|
+
"CSD",
|
|
32
|
+
"CZK",
|
|
33
|
+
"CVE",
|
|
34
|
+
"DEM",
|
|
35
|
+
"DKK",
|
|
36
|
+
"DOP",
|
|
37
|
+
"DZD",
|
|
38
|
+
"EEK",
|
|
39
|
+
"EGP",
|
|
40
|
+
"EUR",
|
|
41
|
+
"FJD",
|
|
42
|
+
"FRF",
|
|
43
|
+
"GBP",
|
|
44
|
+
"GEL",
|
|
45
|
+
"GHS",
|
|
46
|
+
"HKD",
|
|
47
|
+
"HNL",
|
|
48
|
+
"HRK",
|
|
49
|
+
"HUF",
|
|
50
|
+
"IDR",
|
|
51
|
+
"ILS",
|
|
52
|
+
"INR",
|
|
53
|
+
"IQD",
|
|
54
|
+
"ISK",
|
|
55
|
+
"JMD",
|
|
56
|
+
"JOD",
|
|
57
|
+
"JPY",
|
|
58
|
+
"KES",
|
|
59
|
+
"KGS",
|
|
60
|
+
"KRW",
|
|
61
|
+
"KWD",
|
|
62
|
+
"KYD",
|
|
63
|
+
"KZT",
|
|
64
|
+
"LAK",
|
|
65
|
+
"LBP",
|
|
66
|
+
"LKR",
|
|
67
|
+
"LTL",
|
|
68
|
+
"MAD",
|
|
69
|
+
"MDL",
|
|
70
|
+
"MKD",
|
|
71
|
+
"MMK",
|
|
72
|
+
"MOP",
|
|
73
|
+
"MTL",
|
|
74
|
+
"MUR",
|
|
75
|
+
"MVR",
|
|
76
|
+
"MXN",
|
|
77
|
+
"MYR",
|
|
78
|
+
"NAD",
|
|
79
|
+
"NGN",
|
|
80
|
+
"NIO",
|
|
81
|
+
"NOK",
|
|
82
|
+
"NPR",
|
|
83
|
+
"NZD",
|
|
84
|
+
"OMR",
|
|
85
|
+
"PAB",
|
|
86
|
+
"PEN",
|
|
87
|
+
"PHP",
|
|
88
|
+
"PKR",
|
|
89
|
+
"PLN",
|
|
90
|
+
"PYG",
|
|
91
|
+
"QAR",
|
|
92
|
+
"RON",
|
|
93
|
+
"ROL",
|
|
94
|
+
"RSD",
|
|
95
|
+
"RUB",
|
|
96
|
+
"SAR",
|
|
97
|
+
"SCR",
|
|
98
|
+
"SEK",
|
|
99
|
+
"SGD",
|
|
100
|
+
"SIT",
|
|
101
|
+
"SKK",
|
|
102
|
+
"SVC",
|
|
103
|
+
"THB",
|
|
104
|
+
"TND",
|
|
105
|
+
"TRL",
|
|
106
|
+
"TRY",
|
|
107
|
+
"TTD",
|
|
108
|
+
"TWD",
|
|
109
|
+
"TZS",
|
|
110
|
+
"UAH",
|
|
111
|
+
"UGX",
|
|
112
|
+
"USD",
|
|
113
|
+
"UYU",
|
|
114
|
+
"UZS",
|
|
115
|
+
"VEB",
|
|
116
|
+
"VEF",
|
|
117
|
+
"VES",
|
|
118
|
+
"VND",
|
|
119
|
+
"WST",
|
|
120
|
+
"XCD",
|
|
121
|
+
"XOF",
|
|
122
|
+
"XPF",
|
|
123
|
+
"YER",
|
|
124
|
+
"ZAR",
|
|
125
|
+
];
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.symbols = void 0;
|
|
4
|
+
exports.symbols = [
|
|
5
|
+
// Currency Codes
|
|
6
|
+
"AED",
|
|
7
|
+
"AFN",
|
|
8
|
+
"ALL",
|
|
9
|
+
"AMD",
|
|
10
|
+
"ANG",
|
|
11
|
+
"ARS",
|
|
12
|
+
"AUD",
|
|
13
|
+
"AWG",
|
|
14
|
+
"AZN",
|
|
15
|
+
"BAM",
|
|
16
|
+
"BDT",
|
|
17
|
+
"BGN",
|
|
18
|
+
"BHD",
|
|
19
|
+
"BND",
|
|
20
|
+
"BOB",
|
|
21
|
+
"BRL",
|
|
22
|
+
"BTN",
|
|
23
|
+
"BWP",
|
|
24
|
+
"BYN",
|
|
25
|
+
"BZD",
|
|
26
|
+
"CAD",
|
|
27
|
+
"CHF",
|
|
28
|
+
"CLP",
|
|
29
|
+
"CNY",
|
|
30
|
+
"COP",
|
|
31
|
+
"CRC",
|
|
32
|
+
"CSD",
|
|
33
|
+
"CZK",
|
|
34
|
+
"CVE",
|
|
35
|
+
"DEM",
|
|
36
|
+
"DKK",
|
|
37
|
+
"DOP",
|
|
38
|
+
"DZD",
|
|
39
|
+
"EEK",
|
|
40
|
+
"EGP",
|
|
41
|
+
"EUR",
|
|
42
|
+
"FJD",
|
|
43
|
+
"FRF",
|
|
44
|
+
"GBP",
|
|
45
|
+
"GEL",
|
|
46
|
+
"GHS",
|
|
47
|
+
"HKD",
|
|
48
|
+
"HNL",
|
|
49
|
+
"HRK",
|
|
50
|
+
"HUF",
|
|
51
|
+
"IDR",
|
|
52
|
+
"ILS",
|
|
53
|
+
"INR",
|
|
54
|
+
"IQD",
|
|
55
|
+
"ISK",
|
|
56
|
+
"JMD",
|
|
57
|
+
"JOD",
|
|
58
|
+
"JPY",
|
|
59
|
+
"KES",
|
|
60
|
+
"KGS",
|
|
61
|
+
"KRW",
|
|
62
|
+
"KWD",
|
|
63
|
+
"KYD",
|
|
64
|
+
"KZT",
|
|
65
|
+
"LAK",
|
|
66
|
+
"LBP",
|
|
67
|
+
"LKR",
|
|
68
|
+
"LTL",
|
|
69
|
+
"MAD",
|
|
70
|
+
"MDL",
|
|
71
|
+
"MKD",
|
|
72
|
+
"MMK",
|
|
73
|
+
"MOP",
|
|
74
|
+
"MTL",
|
|
75
|
+
"MUR",
|
|
76
|
+
"MVR",
|
|
77
|
+
"MXN",
|
|
78
|
+
"MYR",
|
|
79
|
+
"NAD",
|
|
80
|
+
"NGN",
|
|
81
|
+
"NIO",
|
|
82
|
+
"NOK",
|
|
83
|
+
"NPR",
|
|
84
|
+
"NZD",
|
|
85
|
+
"OMR",
|
|
86
|
+
"PAB",
|
|
87
|
+
"PEN",
|
|
88
|
+
"PHP",
|
|
89
|
+
"PKR",
|
|
90
|
+
"PLN",
|
|
91
|
+
"PYG",
|
|
92
|
+
"QAR",
|
|
93
|
+
"RON",
|
|
94
|
+
"ROL",
|
|
95
|
+
"RSD",
|
|
96
|
+
"RUB",
|
|
97
|
+
"SAR",
|
|
98
|
+
"SCR",
|
|
99
|
+
"SEK",
|
|
100
|
+
"SGD",
|
|
101
|
+
"SIT",
|
|
102
|
+
"SKK",
|
|
103
|
+
"SVC",
|
|
104
|
+
"THB",
|
|
105
|
+
"TND",
|
|
106
|
+
"TRL",
|
|
107
|
+
"TRY",
|
|
108
|
+
"TTD",
|
|
109
|
+
"TWD",
|
|
110
|
+
"TZS",
|
|
111
|
+
"UAH",
|
|
112
|
+
"UGX",
|
|
113
|
+
"USD",
|
|
114
|
+
"UYU",
|
|
115
|
+
"UZS",
|
|
116
|
+
"VEB",
|
|
117
|
+
"VEF",
|
|
118
|
+
"VES",
|
|
119
|
+
"VND",
|
|
120
|
+
"WST",
|
|
121
|
+
"XCD",
|
|
122
|
+
"XOF",
|
|
123
|
+
"XPF",
|
|
124
|
+
"YER",
|
|
125
|
+
"ZAR",
|
|
126
|
+
// Cryptocurrency Codes
|
|
127
|
+
"BTC",
|
|
128
|
+
"ETH",
|
|
129
|
+
"ADA",
|
|
130
|
+
"BNB",
|
|
131
|
+
"USDT",
|
|
132
|
+
"XRP",
|
|
133
|
+
"DOGE",
|
|
134
|
+
"LINK",
|
|
135
|
+
"LTC",
|
|
136
|
+
"BCH",
|
|
137
|
+
"XLM",
|
|
138
|
+
"TRX",
|
|
139
|
+
"ETC",
|
|
140
|
+
"XMR",
|
|
141
|
+
"EOS",
|
|
142
|
+
"NEO",
|
|
143
|
+
"WAVES",
|
|
144
|
+
"DASH",
|
|
145
|
+
"OMG",
|
|
146
|
+
"DCR",
|
|
147
|
+
"XEM",
|
|
148
|
+
"MANA",
|
|
149
|
+
"ICX",
|
|
150
|
+
"QTUM",
|
|
151
|
+
"ZIL",
|
|
152
|
+
"BAT",
|
|
153
|
+
"BTG",
|
|
154
|
+
"BNT",
|
|
155
|
+
"ZRX",
|
|
156
|
+
"SC",
|
|
157
|
+
"ONT",
|
|
158
|
+
"DGB",
|
|
159
|
+
"NANO",
|
|
160
|
+
"LSK",
|
|
161
|
+
"GNO",
|
|
162
|
+
"XVG",
|
|
163
|
+
"ARDR",
|
|
164
|
+
"SNT",
|
|
165
|
+
"MAID",
|
|
166
|
+
"REP",
|
|
167
|
+
"ARK",
|
|
168
|
+
"FUN",
|
|
169
|
+
"STEEM",
|
|
170
|
+
"SYS",
|
|
171
|
+
"BTS",
|
|
172
|
+
"KMD",
|
|
173
|
+
"MONA",
|
|
174
|
+
"GAS",
|
|
175
|
+
"RDD",
|
|
176
|
+
"BTM",
|
|
177
|
+
"WTC",
|
|
178
|
+
"AION",
|
|
179
|
+
"GRS",
|
|
180
|
+
"BCN",
|
|
181
|
+
"VERI",
|
|
182
|
+
"DGD",
|
|
183
|
+
"PIVX",
|
|
184
|
+
"NXS",
|
|
185
|
+
"PPT",
|
|
186
|
+
"QASH",
|
|
187
|
+
"NEBL",
|
|
188
|
+
"SALT",
|
|
189
|
+
"GAME",
|
|
190
|
+
"FCT",
|
|
191
|
+
"NXT",
|
|
192
|
+
"PAY",
|
|
193
|
+
"XDN",
|
|
194
|
+
"SUB",
|
|
195
|
+
"KNC",
|
|
196
|
+
"GNT",
|
|
197
|
+
"REQ",
|
|
198
|
+
"VGX",
|
|
199
|
+
"TNB",
|
|
200
|
+
"ZCL",
|
|
201
|
+
"HSR",
|
|
202
|
+
"ICN",
|
|
203
|
+
"POWR",
|
|
204
|
+
"ENG",
|
|
205
|
+
"SAN",
|
|
206
|
+
"ELF",
|
|
207
|
+
"STRAT",
|
|
208
|
+
"QSP",
|
|
209
|
+
"RDN",
|
|
210
|
+
];
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import axios from "axios";
|
|
2
|
+
import { API_URL } from "./lib/config";
|
|
3
|
+
import { symbols } from "./lib/symbols";
|
|
4
|
+
import { currencyCodesSymbols } from "./lib/currencyCodesSymbols";
|
|
5
|
+
import { cryptocurrencyCodesSymbols } from "./lib/cryptoCurrencyCodesSymbols";
|
|
6
|
+
export class Finance {
|
|
7
|
+
constructor(p) {
|
|
8
|
+
var _a, _b, _c, _d;
|
|
9
|
+
if (typeof p === "object" &&
|
|
10
|
+
(symbols.includes(((_a = p === null || p === void 0 ? void 0 : p.from) === null || _a === void 0 ? void 0 : _a.toUpperCase()) || "") ||
|
|
11
|
+
symbols.includes(((_b = p === null || p === void 0 ? void 0 : p.to) === null || _b === void 0 ? void 0 : _b.toUpperCase()) || ""))) {
|
|
12
|
+
this.param = {
|
|
13
|
+
from: ((_c = p.from) === null || _c === void 0 ? void 0 : _c.toUpperCase()) || null,
|
|
14
|
+
to: ((_d = p.to) === null || _d === void 0 ? void 0 : _d.toUpperCase()) || null,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
else if (typeof p === "undefined") {
|
|
18
|
+
this.param = { from: null, to: null };
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
throw new Error("Invalid parameters.");
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
setFrom(from) {
|
|
25
|
+
if (!symbols.includes(from === null || from === void 0 ? void 0 : from.toUpperCase()))
|
|
26
|
+
throw new Error("invalid from.");
|
|
27
|
+
this.param.from = from.toUpperCase();
|
|
28
|
+
return this;
|
|
29
|
+
}
|
|
30
|
+
setTo(to) {
|
|
31
|
+
if (!symbols.includes(to === null || to === void 0 ? void 0 : to.toUpperCase()))
|
|
32
|
+
throw new Error("invalid to.");
|
|
33
|
+
this.param.to = to.toUpperCase();
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
getParam() {
|
|
37
|
+
return this.param;
|
|
38
|
+
}
|
|
39
|
+
async quote(amount = 1) {
|
|
40
|
+
const result = { success: false, rate: 0 };
|
|
41
|
+
try {
|
|
42
|
+
if (typeof amount !== "number")
|
|
43
|
+
throw new Error("amount must be number.");
|
|
44
|
+
const from = this.param.from;
|
|
45
|
+
const to = this.param.to;
|
|
46
|
+
if (!(symbols === null || symbols === void 0 ? void 0 : symbols.includes(from || "")) || !(symbols === null || symbols === void 0 ? void 0 : symbols.includes(to || "")))
|
|
47
|
+
throw new Error("from and/or to are invalid.");
|
|
48
|
+
const url = `${API_URL}${from}-${to}`;
|
|
49
|
+
const response = await axios.get(url);
|
|
50
|
+
const html = response.data;
|
|
51
|
+
const startIndex = html.indexOf(`data-source="${from}" data-target="${to}" data-last-price="`) +
|
|
52
|
+
`data-source="${from}" data-target="${to}" data-last-price="`.length;
|
|
53
|
+
const endIndex = html.indexOf('"', startIndex);
|
|
54
|
+
const rate = Number(html.substring(startIndex, endIndex));
|
|
55
|
+
if (isNaN(rate))
|
|
56
|
+
throw new Error("Failed to get the current finance.");
|
|
57
|
+
result.success = true;
|
|
58
|
+
result.rate = rate * amount;
|
|
59
|
+
return result;
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
return result;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
export { symbols, currencyCodesSymbols, cryptocurrencyCodesSymbols };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const API_URL = "https://www.google.com/finance/quote/";
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
export const cryptocurrencyCodesSymbols = [
|
|
2
|
+
"BTC",
|
|
3
|
+
"ETH",
|
|
4
|
+
"ADA",
|
|
5
|
+
"BNB",
|
|
6
|
+
"USDT",
|
|
7
|
+
"XRP",
|
|
8
|
+
"DOGE",
|
|
9
|
+
"LINK",
|
|
10
|
+
"LTC",
|
|
11
|
+
"BCH",
|
|
12
|
+
"XLM",
|
|
13
|
+
"TRX",
|
|
14
|
+
"ETC",
|
|
15
|
+
"XMR",
|
|
16
|
+
"EOS",
|
|
17
|
+
"NEO",
|
|
18
|
+
"WAVES",
|
|
19
|
+
"DASH",
|
|
20
|
+
"OMG",
|
|
21
|
+
"DCR",
|
|
22
|
+
"XEM",
|
|
23
|
+
"MANA",
|
|
24
|
+
"ICX",
|
|
25
|
+
"QTUM",
|
|
26
|
+
"ZIL",
|
|
27
|
+
"BAT",
|
|
28
|
+
"BTG",
|
|
29
|
+
"BNT",
|
|
30
|
+
"ZRX",
|
|
31
|
+
"SC",
|
|
32
|
+
"ONT",
|
|
33
|
+
"DGB",
|
|
34
|
+
"NANO",
|
|
35
|
+
"LSK",
|
|
36
|
+
"GNO",
|
|
37
|
+
"XVG",
|
|
38
|
+
"ARDR",
|
|
39
|
+
"SNT",
|
|
40
|
+
"MAID",
|
|
41
|
+
"REP",
|
|
42
|
+
"ARK",
|
|
43
|
+
"FUN",
|
|
44
|
+
"STEEM",
|
|
45
|
+
"SYS",
|
|
46
|
+
"BTS",
|
|
47
|
+
"KMD",
|
|
48
|
+
"MONA",
|
|
49
|
+
"GAS",
|
|
50
|
+
"RDD",
|
|
51
|
+
"BTM",
|
|
52
|
+
"WTC",
|
|
53
|
+
"AION",
|
|
54
|
+
"GRS",
|
|
55
|
+
"BCN",
|
|
56
|
+
"VERI",
|
|
57
|
+
"DGD",
|
|
58
|
+
"PIVX",
|
|
59
|
+
"NXS",
|
|
60
|
+
"PPT",
|
|
61
|
+
"QASH",
|
|
62
|
+
"NEBL",
|
|
63
|
+
"SALT",
|
|
64
|
+
"GAME",
|
|
65
|
+
"FCT",
|
|
66
|
+
"NXT",
|
|
67
|
+
"PAY",
|
|
68
|
+
"XDN",
|
|
69
|
+
"SUB",
|
|
70
|
+
"KNC",
|
|
71
|
+
"GNT",
|
|
72
|
+
"REQ",
|
|
73
|
+
"VGX",
|
|
74
|
+
"TNB",
|
|
75
|
+
"ZCL",
|
|
76
|
+
"HSR",
|
|
77
|
+
"ICN",
|
|
78
|
+
"POWR",
|
|
79
|
+
"ENG",
|
|
80
|
+
"SAN",
|
|
81
|
+
"ELF",
|
|
82
|
+
"STRAT",
|
|
83
|
+
"QSP",
|
|
84
|
+
"RDN",
|
|
85
|
+
];
|