UNPKG

@cardql/node

Version:

CardQL SDK for Node.js and serverless applications

143 lines (124 loc) 3.53 kB
import { CardQL as CoreCardQL, CardQLClient, CardQLApi } from "@cardql/core"; import type { CardQLConfig } from "@cardql/core"; import { createConfigFromEnv, validateConfig, environmentConfigs, } from "./config"; import type { NodeCardQLConfig } from "./config"; import { Logger, defaultLogger } from "./logger"; export class NodeCardQLClient extends CardQLClient { private logger: Logger; constructor(config: CardQLConfig, logger?: Logger) { super(config); this.logger = logger || defaultLogger; } async request<T = any>(query: string, variables?: any): Promise<T> { this.logger.debug("Making GraphQL request", { query: query.substring(0, 100) + "...", variables, }); try { const result = await super.request<T>(query, variables); this.logger.debug("GraphQL request successful"); return result; } catch (error: any) { this.logger.error("GraphQL request failed", { error: error.message, code: error.code, }); throw error; } } } export class NodeCardQL extends CoreCardQL { public client: NodeCardQLClient; public api: CardQLApi; private logger: Logger; constructor(config?: NodeCardQLConfig) { // Create configuration from environment if not provided const finalConfig = config ? { ...createConfigFromEnv(), ...config, } : createConfigFromEnv(); // Validate configuration validateConfig(finalConfig); // Setup logger const logger = new Logger( config?.enableLogging ?? false, config?.logLevel ?? "info" ); // Create enhanced client const nodeClient = new NodeCardQLClient(finalConfig, logger); // Call parent constructor with dummy config since we override the client super(finalConfig); // Replace with our enhanced client this.client = nodeClient; this.api = new CardQLApi(this.client); this.logger = logger; this.logger.info("CardQL Node.js client initialized", { endpoint: finalConfig.endpoint, timeout: finalConfig.timeout, retries: finalConfig.retries, }); } /** * Create instance with environment-specific defaults */ static forEnvironment( env: keyof typeof environmentConfigs, overrides: NodeCardQLConfig = {} ): NodeCardQL { const envConfig = environmentConfigs[env]; return new NodeCardQL({ ...envConfig, ...overrides, env, }); } /** * Enable or disable logging */ setLogging( enabled: boolean, level?: "error" | "warn" | "info" | "debug" ): void { this.logger.setEnabled(enabled); if (level) { this.logger.setLevel(level); } } /** * Health check method to verify connectivity */ async healthCheck(): Promise<boolean> { try { this.logger.info("Performing health check..."); // Try a simple query to test connectivity await this.api.getAccounts(); this.logger.info("Health check passed"); return true; } catch (error: any) { this.logger.error("Health check failed", { error: error.message }); return false; } } /** * Get client statistics */ getStats(): { endpoint: string; timeout: number; retries: number; loggingEnabled: boolean; } { return { endpoint: this.client["config"].endpoint, timeout: this.client["config"].timeout || 30000, retries: this.client["config"].retries || 3, loggingEnabled: this.logger["enabled"], }; } }