@cardql/node
Version:
CardQL SDK for Node.js and serverless applications
132 lines (126 loc) • 4.19 kB
TypeScript
import { CardQLConfig, CardQL, CardQLClient, CardQLApi } from '@cardql/core';
export * from '@cardql/core';
import { Request, Response, NextFunction } from 'express';
interface NodeCardQLConfig extends Omit<CardQLConfig, "apiKey" | "endpoint"> {
apiKey?: string;
endpoint?: string;
enableLogging?: boolean;
logLevel?: "error" | "warn" | "info" | "debug";
env?: "development" | "staging" | "production";
}
/**
* Create CardQL configuration from environment variables
*/
declare function createConfigFromEnv(overrides?: NodeCardQLConfig): CardQLConfig;
/**
* Validate configuration
*/
declare function validateConfig(config: CardQLConfig): void;
/**
* Environment configuration presets
*/
declare const environmentConfigs: {
development: {
timeout: number;
retries: number;
enableLogging: boolean;
logLevel: "debug";
};
staging: {
timeout: number;
retries: number;
enableLogging: boolean;
logLevel: "info";
};
production: {
timeout: number;
retries: number;
enableLogging: boolean;
logLevel: "error";
};
};
type LogLevel = "error" | "warn" | "info" | "debug";
interface LogEntry {
level: LogLevel;
message: string;
timestamp: string;
metadata?: any;
}
declare class Logger {
private enabled;
private level;
constructor(enabled?: boolean, level?: LogLevel);
private shouldLog;
private formatMessage;
error(message: string, metadata?: any): void;
warn(message: string, metadata?: any): void;
info(message: string, metadata?: any): void;
debug(message: string, metadata?: any): void;
setEnabled(enabled: boolean): void;
setLevel(level: LogLevel): void;
}
declare const defaultLogger: Logger;
declare class NodeCardQLClient extends CardQLClient {
private logger;
constructor(config: CardQLConfig, logger?: Logger);
request<T = any>(query: string, variables?: any): Promise<T>;
}
declare class NodeCardQL extends CardQL {
client: NodeCardQLClient;
api: CardQLApi;
private logger;
constructor(config?: NodeCardQLConfig);
/**
* Create instance with environment-specific defaults
*/
static forEnvironment(env: keyof typeof environmentConfigs, overrides?: NodeCardQLConfig): NodeCardQL;
/**
* Enable or disable logging
*/
setLogging(enabled: boolean, level?: "error" | "warn" | "info" | "debug"): void;
/**
* Health check method to verify connectivity
*/
healthCheck(): Promise<boolean>;
/**
* Get client statistics
*/
getStats(): {
endpoint: string;
timeout: number;
retries: number;
loggingEnabled: boolean;
};
}
declare global {
namespace Express {
interface Request {
cardql?: NodeCardQL;
}
}
}
interface MiddlewareOptions extends NodeCardQLConfig {
skipHealthCheck?: boolean;
errorHandler?: (error: Error, req: Request, res: Response, next: NextFunction) => void;
}
/**
* Express middleware to inject CardQL instance into requests
*/
declare function cardqlMiddleware(options?: MiddlewareOptions): (req: Request, res: Response, next: NextFunction) => void;
/**
* Error handling middleware for CardQL errors
*/
declare function cardqlErrorHandler(): (error: any, req: Request, res: Response, next: NextFunction) => void;
/**
* Utility function to create CardQL context for serverless functions
*/
declare function createCardQLContext(config?: NodeCardQLConfig): {
cardql: NodeCardQL;
};
/**
* Higher-order function for serverless function handlers
*/
declare function withCardQL<TEvent = any, TResult = any>(handler: (event: TEvent, context: any & {
cardql: NodeCardQL;
}) => Promise<TResult>, config?: NodeCardQLConfig): (event: TEvent, context: any) => Promise<TResult>;
export { NodeCardQL as CardQL, type LogEntry, type LogLevel, Logger, type MiddlewareOptions, NodeCardQL, NodeCardQLClient, type NodeCardQLConfig, cardqlErrorHandler, cardqlMiddleware, createCardQLContext, createConfigFromEnv, NodeCardQL as default, defaultLogger, environmentConfigs, validateConfig, withCardQL };