UNPKG

@elrondnetwork/erdjs-data-api-client

Version:
86 lines 3.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DataApiClient = void 0; const tslib_1 = require("tslib"); const axios_1 = tslib_1.__importDefault(require("axios")); const agentkeepalive_1 = tslib_1.__importStar(require("agentkeepalive")); const utils_1 = require("./utils"); class DataApiClient { constructor(config) { this.initialize(config); } async executeLastQuery(query) { return await this.executeQuery(query) .then(utils_1.DataApiResponseFormatter.buildLastResponse); } async executeAggregateQuery(query) { return await this.executeQuery(query) .then(utils_1.DataApiResponseFormatter.buildAggregateResponse); } async executeHistoricalQuery(query) { return await this.executeQuery(query) .then(utils_1.DataApiResponseFormatter.buildHistoricalResponse); } async executeQuery(query) { const response = await this.executeRawQuery(query.toJson()); return utils_1.DataApiResponseFormatter.formatResponse(query.responsePath, response); } async executeRawQuery(body) { try { const response = await this.post(body); return response; } catch (error) { throw error; } } async post(payload) { try { const config = await this.getConfig(); const { data } = await axios_1.default.post(this.url, payload, config); if (data.errors) { throw data.errors; } return data.data; } catch (error) { throw error; } } initialize(config) { var _a, _b, _c, _d, _e; this.url = config.dataApiUrl; const keepAliveOptions = { maxSockets: (_a = config.keepAlive) === null || _a === void 0 ? void 0 : _a.maxSockets, maxFreeSockets: (_b = config.keepAlive) === null || _b === void 0 ? void 0 : _b.maxFreeSockets, timeout: (_c = config.keepAlive) === null || _c === void 0 ? void 0 : _c.timeout, freeSocketTimeout: (_d = config.keepAlive) === null || _d === void 0 ? void 0 : _d.freeSocketTimeout, keepAlive: true, }; const httpAgent = new agentkeepalive_1.default(keepAliveOptions); const httpsAgent = new agentkeepalive_1.HttpsAgent(keepAliveOptions); this.config = { timeout: (_e = config.proxyTimeout) !== null && _e !== void 0 ? _e : 10000, httpAgent: config.keepAlive ? httpAgent : null, httpsAgent: config.keepAlive ? httpsAgent : null, }; this.nativeAuthSigner = new utils_1.NativeAuthSigner({ host: config.host, apiUrl: config.multiversXApiUrl, signerPrivateKey: config.signerPrivateKey, }); } async getConfig() { const accessToken = await this.getAccessToken(); return Object.assign(Object.assign({}, this.config), { headers: { Authorization: `Bearer ${accessToken.token}`, }, validateStatus: function () { return true; } }); } async getAccessToken() { return await this.nativeAuthSigner.getToken(); } } exports.DataApiClient = DataApiClient; //# sourceMappingURL=client.js.map