google-finance-quote 1.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 ADDED
@@ -0,0 +1,46 @@
1
+ # google-finance-quote
2
+ Node Google Finance API wrapper for free.
3
+ No API key is required!
4
+
5
+ ## Usage
6
+ ### Get Started
7
+ ```js
8
+ const Finance = require("google-finance-quote");
9
+
10
+ const finance = new Finance(); // You can use this: new Finance({ from 'usd', to: 'jpy' });
11
+ // You can use http(s) proxies.
12
+ /*
13
+ const proxy = {
14
+ host: 'example.com',
15
+ port: 2000,
16
+ protocol: 'http'
17
+ }
18
+ const finance = new Finance({ proxy });
19
+ */
20
+
21
+ finance
22
+ .setFrom('usd');
23
+ .setTo('jpy');
24
+
25
+ (async () => {
26
+ console.log(await finance.quote()); // { success: true, rate: 150.94225699999998 }
27
+ })();
28
+ ```
29
+ ### Class
30
+ <strong>Finance({ from?:string, to?:string, proxy?:object })</strong>
31
+
32
+ ### Functions
33
+ - <strong>.setFrom(from: string)</strong>
34
+ Set the parameter of from.
35
+
36
+ - <strong>.setTo(to: string)</strong>
37
+ Set the parameter of to.
38
+
39
+ - <strong>.getParam(): object</strong>
40
+ Returns the current param.
41
+
42
+ - <strong>.quote(amount?: number): Promise<{ success: boolean, rate: number }></strong>
43
+ Returns the converted amount based on the exchange rate.
44
+ > Note: If the current rate cannot be obtained due to rate limits or network errors, success: false is returned.
45
+ ## Get Support
46
+ <a href="https://discord.gg/yKW8wWKCnS"><img src="https://discordapp.com/api/guilds/1005287561582878800/widget.png?style=banner4" alt="Discord Banner"/></a>
package/index.d.ts ADDED
@@ -0,0 +1,71 @@
1
+ declare module 'google-finance-quote' {
2
+ /**
3
+ * @interface Proxy
4
+ * @description Interface for proxy parameters.
5
+ * @property {string} host - Proxy host name.
6
+ * @property {number | undefined} - Proxy port number.
7
+ * @property {'http' | 'https'} protocol - Proxy protocol type.
8
+ */
9
+ interface Proxy {
10
+ host: string;
11
+ port?: number,
12
+ protocol: 'http' | 'https';
13
+ }
14
+
15
+ /**
16
+ * @interface FinanceParams
17
+ * @description Interface for Finance class constructor parameters.
18
+ * @property {string} from - The original currency unit.
19
+ * @property {string} to - The desired currency unit.
20
+ * @property {Proxy | undefined} proxy - Proxy options.
21
+ */
22
+ interface FinanceParams {
23
+ from: string;
24
+ to: string;
25
+ }
26
+
27
+ /**
28
+ * @class Finance
29
+ * @description The Finance class is designed to easily check the exchange rate.
30
+ * @param {FinanceParams | undefined} p - The parameters, including p.from and p.to.
31
+ */
32
+ class Finance {
33
+ private param: FinanceParams;
34
+
35
+ constructor(p?: FinanceParams);
36
+
37
+ /**
38
+ * @function setFrom
39
+ * @description Set the parameter of from.
40
+ * @param {string} from - The original currency unit.
41
+ * @returns {Finance} Returns the instance of Finance for chaining.
42
+ */
43
+ setFrom(from: string): Finance;
44
+
45
+ /**
46
+ * @function setTo
47
+ * @description Set the parameter of to.
48
+ * @param {string} to - The desired currency unit.
49
+ * @returns {Finance} Returns the instance of Finance for chaining.
50
+ */
51
+ setTo(to: string): Finance;
52
+
53
+ /**
54
+ * @function getParam
55
+ * @description Returns the current param.
56
+ * @returns {FinanceParams} Returns the current param.
57
+ */
58
+ getParam(): FinanceParams;
59
+
60
+ /**
61
+ * @async
62
+ * @function quote
63
+ * @description Returns the current finance rate.
64
+ * @param {number} [amount=1] - The amount to convert.
65
+ * @returns {Promise<{ success: boolean; rate: number }>} Returns the converted amount based on the exchange rate.
66
+ */
67
+ quote(amount?: number): Promise<{ success: boolean; rate: number }>;
68
+ }
69
+
70
+ export = Finance;
71
+ }
package/index.js ADDED
@@ -0,0 +1,134 @@
1
+ const axios = require('axios');
2
+ const HttpsProxyAgent = require('https-proxy-agent');
3
+ const { API_URL } = require('./lib/config');
4
+
5
+ /**
6
+ * @class Finance
7
+ * @description The Finance class is designed to easily check the exchange rate.
8
+ * @param {Object} p - The param includes p.from and p.to.
9
+ * @param {string} p.from - The original currency unit.
10
+ * @param {string} p.to - The desired currency unit.
11
+ * @param {object | undefined} p.proxy - Proxy options.
12
+ */
13
+ class Finance {
14
+ constructor(p) {
15
+ if (
16
+ typeof p === 'object' &&
17
+ typeof p?.from === 'string' &&
18
+ typeof p?.to === 'string'
19
+ ) {
20
+ this.param = {
21
+ from: p.from,
22
+ to: p.to
23
+ };
24
+ } else if (typeof p === 'undefined') {
25
+ this.param = {
26
+ from: undefined,
27
+ to: undefined
28
+ };
29
+ } else throw new Error('Invalid parameters.');
30
+
31
+ this.proxy = {};
32
+ if (typeof p?.proxy === 'object') {
33
+ if (
34
+ typeof p.proxy?.host === 'string' &&
35
+ (
36
+ p.proxy.protocol === 'http' ||
37
+ p.proxy.protocol === 'https'
38
+ )
39
+ ) {
40
+ this.proxy.host = p.proxy.host;
41
+ if (typeof p.proxy?.port === 'number') this.proxy.port = p.proxy.port;
42
+ this.proxy.protocol = p.proxy.protocol;
43
+ }
44
+ }
45
+ }
46
+
47
+ /**
48
+ * @function setFrom
49
+ * @description Set the parameter of from.
50
+ * @param {string} from - The original currency unit.
51
+ * @returns {Finance} Returns the instance of Finance for chaining.
52
+ */
53
+ setFrom(from) {
54
+ if (typeof from !== 'string') throw new Error('from must be string.');
55
+ this.param.from = from;
56
+ return this;
57
+ }
58
+
59
+ /**
60
+ * @function setTo
61
+ * @description Set the parameter of to.
62
+ * @param {string} to - The desired currency unit.
63
+ * @returns {Finance} Returns the instance of Finance for chaining.
64
+ */
65
+ setTo(to) {
66
+ if (typeof to !== 'string') throw new Error('to must be string.');
67
+ this.param.to = to;
68
+ return this;
69
+ }
70
+
71
+ /**
72
+ * @function getParam
73
+ * @description Returns the current param.
74
+ * @returns Returns the current param.
75
+ */
76
+ getParam() {
77
+ return this.param;
78
+ }
79
+
80
+ /**
81
+ * @async
82
+ * @function quote
83
+ * @description Returns the current finance.
84
+ * @param {number} [amount=1] - The amount to convert.
85
+ * @returns Returns the current finance.
86
+ */
87
+ async quote(amount = 1) {
88
+ const result = { success: false, rate: 0 };
89
+ try {
90
+ if (typeof amount !== 'number') throw new Error('amount must be number.');
91
+ const from = this.param.from?.toUpperCase(), to = this.param.to.toUpperCase();
92
+ if (typeof from !== 'string' || typeof to !== 'string') throw new Error('from and/or to are invalid.');
93
+
94
+ const url = `${API_URL}${from}-${to}`;
95
+
96
+ let response;
97
+ if (this.proxy) {
98
+ const axiosConfig = {};
99
+ if (this.proxy.protocol === 'http') {
100
+ axiosConfig.proxy = {
101
+ host: this.proxy.host,
102
+ }
103
+ if (this.proxy.port) axiosConfig.proxy.port = this.proxy.port;
104
+ } else if (this.proxy.protocol === 'https') {
105
+ const proxyUrl = `http://${this.proxy.host}${this.proxy.port ? `:${this.proxy.port}` : ''}`;
106
+ const agent = new HttpsProxyAgent(proxyUrl);
107
+ axiosConfig.httpsAgent = agent;
108
+ axiosConfig.proxy = false;
109
+ }
110
+ response = await axios.get(url, axiosConfig);
111
+ } else {
112
+ response = await axios.get(url);
113
+ }
114
+
115
+ const html = response.data;
116
+ const startIndex =
117
+ html.indexOf(
118
+ `data-source="${from}" data-target="${to}" data-last-price="`
119
+ ) +
120
+ `data-source="${from}" data-target="${to}" data-last-price="`.length;
121
+ const endIndex = html.indexOf('"', startIndex);
122
+ const rate = Number(html.substring(startIndex, endIndex));
123
+ if (isNaN(rate)) throw new Error('Failed to get the current finance.');
124
+
125
+ result.success = true;
126
+ result.rate = rate * amount;
127
+ return result;
128
+ } catch {
129
+ return result;
130
+ }
131
+ }
132
+ }
133
+
134
+ module.exports = Finance;
package/lib/config.js ADDED
@@ -0,0 +1,3 @@
1
+ module.exports = {
2
+ API_URL: 'https://www.google.com/finance/quote/'
3
+ };
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "google-finance-quote",
3
+ "version": "1.0.0",
4
+ "description": "Node Google Finance API wrapper for free. No API key is required!",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/otoneko1102/google-finance-quote.git"
12
+ },
13
+ "keywords": [
14
+ "google",
15
+ "api",
16
+ "finance",
17
+ "exchange",
18
+ "rate",
19
+ "quote",
20
+ "wrapper",
21
+ "free",
22
+ "proxy"
23
+ ],
24
+ "author": "otoneko.",
25
+ "license": "ISC",
26
+ "bugs": {
27
+ "url": "https://github.com/otoneko1102/google-finance-quote/issues"
28
+ },
29
+ "homepage": "https://github.com/otoneko1102/google-finance-quote#readme",
30
+ "dependencies": {
31
+ "axios": "^1.7.7",
32
+ "https-proxy-agent": "^7.0.5"
33
+ }
34
+ }