UNPKG

@openclueo/express-clueobots

Version:

Express.js middleware for ClueoBots AI Security - One line of code, full protection

100 lines (99 loc) 2.78 kB
import { Request, Response, NextFunction } from 'express'; export interface ClueoBotsMiddlewareOptions { /** * ClueoBots API key. If not provided, will read from CLUEOBOTS_API_KEY environment variable */ apiKey?: string; /** * Base URL for ClueoBots API * @default "https://backend.clueobots.com" */ baseUrl?: string; /** * Paths to protect. If not specified, protects all routes * @example ['/api/chat', '/api/generate', '/api/ai/*'] */ paths?: string[]; /** * Paths to exclude from protection * @example ['/api/health', '/api/status'] */ excludePaths?: string[]; /** * Minimum threat level to block requests * @default "medium" */ blockThreshold?: 'low' | 'medium' | 'high' | 'critical'; /** * Whether to block requests with detected threats * @default true */ blockRequests?: boolean; /** * Custom handler for when threats are detected */ onThreat?: (threat: ThreatInfo, req: Request, res: Response) => void; /** * Custom handler for errors */ onError?: (error: Error, req: Request, res: Response) => void; /** * Enable debug logging * @default false */ debug?: boolean; /** * Request timeout in milliseconds * @default 5000 */ timeout?: number; /** * Skip scanning for certain content types * @default ['image/*', 'video/*', 'audio/*'] */ skipContentTypes?: string[]; /** * Maximum request body size to scan (in bytes) * @default 1048576 (1MB) */ maxBodySize?: number; } export interface ThreatInfo { detected: boolean; threatType?: string; riskLevel: 'low' | 'medium' | 'high' | 'critical'; confidence: number; explanation: string; suggestions?: string[]; scanId: string; timestamp: string; } interface RequestWithClueoBots extends Request { clueobots?: { scanned: boolean; threat?: ThreatInfo; scanTime: number; }; } /** * ClueoBots Express.js middleware for AI security protection * * @example * ```typescript * import express from 'express' * import { clueobots } from '@openclueo/express-clueobots' * * const app = express() * * // Protect all routes with one line * app.use(clueobots({ apiKey: 'your-api-key' })) * * // Your routes are now automatically protected * app.post('/api/chat', (req, res) => { * // req.body is already scanned for threats * res.json({ message: 'Safe to proceed!' }) * }) * ``` */ export declare function clueobots(options?: ClueoBotsMiddlewareOptions): (req: RequestWithClueoBots, res: Response, next: NextFunction) => Promise<void | Response<any, Record<string, any>>>; export default clueobots;