@riocrypto/common-server 1.0.593 → 1.0.594

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.
@@ -15,20 +15,32 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.buildFixerClient = void 0;
16
16
  const axios_1 = __importDefault(require("axios"));
17
17
  const secret_manager_client_1 = require("./secret-manager-client");
18
+ const node_cache_1 = __importDefault(require("node-cache"));
19
+ const cache = new node_cache_1.default();
18
20
  class FixerClient {
19
21
  constructor(apiKey) {
20
22
  this.apiKey = apiKey;
21
23
  }
22
24
  convert(from, to) {
23
25
  return __awaiter(this, void 0, void 0, function* () {
24
- const fixerResponse = yield axios_1.default.get(`https://api.apilayer.com/fixer/latest?base=${from}&symbols=${to}`, {
25
- headers: {
26
- apikey: this.apiKey,
27
- "Accept-Encoding": "gzip,deflate,compress",
28
- },
29
- });
30
- const exchangeRate = fixerResponse.data.rates[to];
31
- return exchangeRate;
26
+ try {
27
+ const fixerResponse = yield axios_1.default.get(`https://api.apilayer.com/fixer/latest?base=${from}&symbols=${to}`, {
28
+ headers: {
29
+ apikey: this.apiKey,
30
+ "Accept-Encoding": "gzip,deflate,compress",
31
+ },
32
+ });
33
+ const exchangeRate = fixerResponse.data.rates[to];
34
+ cache.set(`fixer/latest?base=${from}&symbols=${to}`, fixerResponse.data, 60);
35
+ return exchangeRate;
36
+ }
37
+ catch (e) {
38
+ const cachedResponse = cache.get(`fixer/latest?base=${from}&symbols=${to}`);
39
+ if (!cachedResponse) {
40
+ throw new Error(`Unable to get exchange rate from Fixer for ${from} to ${to}`);
41
+ }
42
+ return cachedResponse.rates[to];
43
+ }
32
44
  });
33
45
  }
34
46
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@riocrypto/common-server",
3
- "version": "1.0.593",
3
+ "version": "1.0.594",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -32,6 +32,7 @@
32
32
  "fireblocks-sdk": "^4.0.0",
33
33
  "jsonwebtoken": "^8.5.1",
34
34
  "mongoose": "^7.0.2",
35
+ "node-cache": "^5.1.2",
35
36
  "node-nats-streaming": "^0.3.2",
36
37
  "uuid": "^9.0.0"
37
38
  }