blockchain-api
Version:
API utilities for interacting with the Exatechl2 blockchain
47 lines (46 loc) • 1.94 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.callExplorerApi = callExplorerApi;
const node_fetch_1 = __importDefault(require("node-fetch"));
const config_1 = require("../config");
const logger_1 = require("../utils/logger");
/**
* Make an API call to the Explorer DB API
* @template T - The expected return type
* @param {string} endpoint - The API endpoint (without leading slash)
* @param {Record<string, any>} queryParams - Query parameters for the request
* @returns {Promise<T>} - A promise that resolves with the result from the API
*/
async function callExplorerApi(endpoint, queryParams = {}) {
try {
const apiUrl = (0, config_1.getExplorerApiUrl)();
const queryString = Object.entries(queryParams)
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
.join('&');
const url = `${apiUrl}/${endpoint}${queryString ? `?${queryString}` : ''}`;
logger_1.logger.debug(`Making request to: ${url}`);
const response = await (0, node_fetch_1.default)(url);
if (!response.ok) {
throw new Error(`Explorer API responded with status: ${response.status}`);
}
const data = await response.json();
logger_1.logger.debug('API Response Structure:', JSON.stringify(data, null, 2).substring(0, 200) + '...');
if (data.status === 'error') {
throw new Error(data.message || 'Explorer API call failed');
}
if (data.status === 'success') {
return data;
}
if (data.data !== undefined) {
return data;
}
return data;
}
catch (error) {
logger_1.logger.error('Error calling Explorer API:', error);
throw error;
}
}