@riocrypto/common-server 1.0.612 → 1.0.613
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.
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { Fiat } from "@riocrypto/common";
|
|
2
2
|
declare class CurrencyAPIClient {
|
|
3
3
|
private apiKey;
|
|
4
|
-
currencyApi: any;
|
|
5
4
|
constructor(apiKey: string);
|
|
6
|
-
convert(from: Fiat, to: Fiat): Promise<
|
|
5
|
+
convert(from: Fiat, to: Fiat): Promise<number>;
|
|
7
6
|
}
|
|
8
7
|
export declare const buildCurrencyAPIClient: () => Promise<CurrencyAPIClient>;
|
|
9
8
|
export {};
|
|
@@ -13,28 +13,29 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.buildCurrencyAPIClient = void 0;
|
|
16
|
+
const axios_1 = __importDefault(require("axios"));
|
|
16
17
|
const secret_manager_client_1 = require("./secret-manager-client");
|
|
17
18
|
const node_cache_1 = __importDefault(require("node-cache"));
|
|
18
|
-
// @ts-ignore
|
|
19
|
-
const currencyapi_js_1 = __importDefault(require("@everapi/currencyapi-js"));
|
|
20
19
|
const cache = new node_cache_1.default();
|
|
21
20
|
class CurrencyAPIClient {
|
|
22
21
|
constructor(apiKey) {
|
|
23
22
|
this.apiKey = apiKey;
|
|
24
|
-
this.currencyApi = new currencyapi_js_1.default(apiKey);
|
|
25
23
|
}
|
|
26
24
|
convert(from, to) {
|
|
27
25
|
return __awaiter(this, void 0, void 0, function* () {
|
|
28
26
|
try {
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
});
|
|
33
|
-
const exchangeRate = response.data.data[to] * 0.9987;
|
|
27
|
+
const currencyAPIResponse = yield axios_1.default.get(`https://api.currencyapi.com/v3/latest?apikey=${this.apiKey}&base_currency=${from}¤cies=${to}`);
|
|
28
|
+
const exchangeRate = currencyAPIResponse.data.data[to].value * 0.9987;
|
|
29
|
+
cache.set(`v3/latest?apikey=${this.apiKey}&base_currency=${from}¤cies=${to}`, currencyAPIResponse.data);
|
|
34
30
|
return exchangeRate;
|
|
35
31
|
}
|
|
36
32
|
catch (e) {
|
|
37
|
-
|
|
33
|
+
const cachedResponse = cache.get(`v3/latest?apikey=${this.apiKey}&base_currency=${from}¤cies=${to}`);
|
|
34
|
+
if (!cachedResponse) {
|
|
35
|
+
throw new Error(`Unable to get exchange rate from currencyAPI for ${from} to ${to}`);
|
|
36
|
+
}
|
|
37
|
+
const exchangeRate = cachedResponse.data[to].value * 0.9987;
|
|
38
|
+
return exchangeRate;
|
|
38
39
|
}
|
|
39
40
|
});
|
|
40
41
|
}
|