UNPKG

@cardql/node

Version:

CardQL SDK for Node.js and serverless applications

98 lines (86 loc) 2.41 kB
import type { CardQLConfig } from "@cardql/core"; export interface NodeCardQLConfig extends Omit<CardQLConfig, "apiKey" | "endpoint"> { apiKey?: string; endpoint?: string; // Node.js specific options enableLogging?: boolean; logLevel?: "error" | "warn" | "info" | "debug"; env?: "development" | "staging" | "production"; } /** * Create CardQL configuration from environment variables */ export function createConfigFromEnv( overrides: NodeCardQLConfig = {} ): CardQLConfig { const config: CardQLConfig = { apiKey: overrides.apiKey || process.env.CARDQL_API_KEY || "", endpoint: overrides.endpoint || process.env.CARDQL_ENDPOINT || "https://api.cardql.com/graphql", timeout: overrides.timeout || parseInt(process.env.CARDQL_TIMEOUT || "30000"), retries: overrides.retries || parseInt(process.env.CARDQL_RETRIES || "3"), }; // Validate required fields if (!config.apiKey) { throw new Error( "CardQL API key is required. Set CARDQL_API_KEY environment variable or provide apiKey in config." ); } if (!config.endpoint) { throw new Error( "CardQL endpoint is required. Set CARDQL_ENDPOINT environment variable or provide endpoint in config." ); } return config; } /** * Validate configuration */ export function validateConfig(config: CardQLConfig): void { if (!config.apiKey?.trim()) { throw new Error("CardQL API key cannot be empty"); } if (!config.endpoint?.trim()) { throw new Error("CardQL endpoint cannot be empty"); } try { new URL(config.endpoint); } catch { throw new Error("CardQL endpoint must be a valid URL"); } if (config.timeout && (config.timeout < 1000 || config.timeout > 300000)) { throw new Error( "CardQL timeout must be between 1000ms and 300000ms (5 minutes)" ); } if (config.retries && (config.retries < 0 || config.retries > 10)) { throw new Error("CardQL retries must be between 0 and 10"); } } /** * Environment configuration presets */ export const environmentConfigs = { development: { timeout: 30000, retries: 3, enableLogging: true, logLevel: "debug" as const, }, staging: { timeout: 30000, retries: 3, enableLogging: true, logLevel: "info" as const, }, production: { timeout: 15000, retries: 2, enableLogging: false, logLevel: "error" as const, }, };