@messari/sdk
Version:
Messari SDK provides a type-safe, intuitive interface for accessing Messari's suite of crypto data and AI APIs.
154 lines • 5.3 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MessariClientBase = void 0;
const logging_1 = require("../logging");
/**
* Abstract base class for the Messari client
* Defines the structure and common functionality that all client implementations must provide
*/
class MessariClientBase {
/**
* Constructor for the MessariClientBase class
* Initializes the logger and logging state
*/
constructor() {
this.isLoggingDisabled = false;
this.logger = (0, logging_1.makeConsoleLogger)("messari-client");
}
/**
* Disable all logging from the client.
* This will prevent any log messages from being output, regardless of their level.
*/
disableLogging() {
this.isLoggingDisabled = true;
this.logger = logging_1.noOpLogger;
}
/**
* Enable logging with the specified log level.
* This will restore logging functionality if it was previously disabled.
*
* @param level The minimum log level to display (defaults to INFO)
*/
enableLogging(level = logging_1.LogLevel.INFO) {
this.isLoggingDisabled = false;
const baseLogger = (0, logging_1.makeConsoleLogger)("messari-client");
this.logger = (0, logging_1.createFilteredLogger)(baseLogger, level);
}
/**
* Set a custom logger for the client.
* This allows you to integrate with your application's logging system.
*
* @param logger The logger implementation to use
* @param level Optional minimum log level to filter messages
*/
setLogger(logger, level) {
this.isLoggingDisabled = false;
this.logger = level ? (0, logging_1.createFilteredLogger)(logger, level) : logger;
}
/**
* Check if logging is currently enabled for the client.
*
* @returns true if logging is enabled, false if it has been disabled
*/
isLoggingEnabled() {
return !this.isLoggingDisabled;
}
/**
* Execute an asynchronous function with logging temporarily disabled.
* After the function completes, the previous logging state will be restored.
*
* @param fn The asynchronous function to execute with logging disabled
* @returns A promise that resolves to the result of the function
* @example
* // Perform a sensitive operation without logging
* const result = await client.withLoggingDisabled(async () => {
* return await client.ai.createChatCompletion({ ... });
* });
*/
async withLoggingDisabled(fn) {
const wasDisabled = this.isLoggingDisabled;
try {
// Disable logging if it was enabled
if (!wasDisabled) {
this.disableLogging();
}
// Execute the function
return await fn();
}
finally {
// Restore previous logging state if it was enabled
if (!wasDisabled) {
this.enableLogging();
}
}
}
/**
* Execute a synchronous function with logging temporarily disabled.
* After the function completes, the previous logging state will be restored.
*
* @param fn The synchronous function to execute with logging disabled
* @returns The result of the function
* @example
* // Perform a sensitive operation without logging
* const result = client.withLoggingDisabledSync(() => {
* return processPrivateData(data);
* });
*/
withLoggingDisabledSync(fn) {
const wasDisabled = this.isLoggingDisabled;
try {
// Disable logging if it was enabled
if (!wasDisabled) {
this.disableLogging();
}
// Execute the function
return fn();
}
finally {
// Restore previous logging state if it was enabled
if (!wasDisabled) {
this.enableLogging();
}
}
}
/**
* Register an event handler
* @param event The event type to listen for
* @param handler The handler function to call when the event occurs
*/
on(event, handler) {
if (!this.eventHandlers.has(event)) {
this.eventHandlers.set(event, new Set());
}
this.eventHandlers.get(event)?.add(handler);
}
/**
* Remove an event handler
* @param event The event type to remove the handler from
* @param handler The handler function to remove
*/
off(event, handler) {
if (this.eventHandlers.has(event)) {
this.eventHandlers.get(event)?.delete(handler);
}
}
/**
* Emit an event to all registered handlers
* @param event The event type to emit
* @param data The event data to pass to handlers
*/
emit(event, data) {
if (this.eventHandlers.has(event)) {
for (const handler of this.eventHandlers.get(event) || []) {
try {
handler(data);
}
catch (error) {
this.logger(logging_1.LogLevel.ERROR, `Error in ${event} handler`, { error });
}
}
}
}
}
exports.MessariClientBase = MessariClientBase;
//# sourceMappingURL=base.js.map