@cfxdevkit/defillama
Version:
A TypeScript library for interacting with Defillama API
57 lines • 2.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DefiLlamaAPI = void 0;
const logger_1 = require("../utils/logger");
const logger = (0, logger_1.createLogger)("DefiLlamaAPI");
/**
* Base API client for DeFi Llama
* Handles HTTP requests to the DeFi Llama API
* @public
*/
class DefiLlamaAPI {
/**
* Creates a new DeFi Llama API client
* @param customBaseUrl - Optional custom base URL for the API
*/
constructor(customBaseUrl) {
this.customBaseUrl = customBaseUrl;
/**
* Base URL for the DeFi Llama API
*/
this.BASE_URL = "https://api.llama.fi";
}
/**
* Makes a request to the DeFi Llama API
* @param endpoint - API endpoint to call
* @param baseUrl - Base URL to use (defaults to BASE_URL)
* @param params - Query parameters to include in the request
* @returns Promise with the API response
* @internal
*/
async fetchApi(endpoint, baseUrl = this.BASE_URL, params = {}) {
try {
const url = new URL(endpoint, this.customBaseUrl || baseUrl);
Object.entries(params).forEach(([key, value]) => {
if (value !== undefined) {
url.searchParams.append(key, String(value));
}
});
logger.debug(`Fetching DeFiLlama data from: ${url.toString()}`);
const response = await fetch(url.toString());
if (!response.ok) {
logger.error(`DeFiLlama API request failed with status: ${response.status}`);
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
logger.debug("API Response:", { endpoint, data });
logger.debug("Data fetched successfully");
return data;
}
catch (error) {
logger.error(`Error fetching ${endpoint}:`, error);
throw error;
}
}
}
exports.DefiLlamaAPI = DefiLlamaAPI;
//# sourceMappingURL=api.js.map