UNPKG

@juspay/neurolink

Version:

Universal AI Development Platform with working MCP integration, multi-provider support, voice (TTS/STT/realtime), and professional CLI. 58+ external MCP servers discoverable, multimodal file processing, RAG pipelines. Build, test, and deploy AI applicatio

208 lines (207 loc) 6.08 kB
/** * Request Interceptors and Retry Logic * * Provides middleware interceptors for the NeuroLink client SDK including * authentication, logging, retry logic, rate limiting, and request/response transformation. * * @module @neurolink/client/interceptors */ import type { ClientMiddleware, ClientMiddlewareRequest, ClientMiddlewareResponse, CacheInterceptorOptions, ErrorHandlerOptions, LoggingInterceptorOptions, RateLimiterOptions, RetryInterceptorOptions, TimeoutInterceptorOptions } from "../types/index.js"; /** * API Key authentication interceptor * * Adds X-API-Key header to all requests. * * @example * ```typescript * client.use(createApiKeyAuthInterceptor('your-api-key')); * ``` */ export declare function createApiKeyAuthInterceptor(apiKey: string): ClientMiddleware; /** * Bearer token authentication interceptor * * Adds Authorization header with Bearer token. * * @example * ```typescript * client.use(createBearerAuthInterceptor('your-token')); * ``` */ export declare function createBearerAuthInterceptor(token: string): ClientMiddleware; /** * Dynamic authentication interceptor * * Retrieves authentication token dynamically for each request. * Useful for token refresh scenarios. * * @example * ```typescript * client.use(createDynamicAuthInterceptor(async () => { * const token = await getAccessToken(); * return { type: 'bearer', token }; * })); * ``` */ export declare function createDynamicAuthInterceptor(getAuth: () => Promise<{ type: "apiKey"; key: string; } | { type: "bearer"; token: string; } | null>): ClientMiddleware; /** * Logging interceptor * * Logs request and response details for debugging. * * @example * ```typescript * client.use(createLoggingInterceptor({ * logRequest: true, * logResponse: true, * redactFields: ['apiKey', 'password'], * })); * ``` */ export declare function createLoggingInterceptor(options?: LoggingInterceptorOptions): ClientMiddleware; /** * Retry interceptor with exponential backoff * * Automatically retries failed requests with configurable backoff. * * @example * ```typescript * client.use(createRetryInterceptor({ * maxAttempts: 3, * initialDelayMs: 1000, * maxDelayMs: 10000, * backoffMultiplier: 2, * retryableStatusCodes: [429, 500, 502, 503, 504], * onRetry: (attempt, error) => console.log(`Retry ${attempt}:`, error), * })); * ``` */ export declare function createRetryInterceptor(options: RetryInterceptorOptions): ClientMiddleware; /** * Rate limiting interceptor * * Limits the rate of requests to prevent overwhelming the API. * * @example * ```typescript * client.use(createRateLimitInterceptor({ * maxRequests: 100, * windowMs: 60000, // 100 requests per minute * strategy: 'queue', * onRateLimited: (waitTime) => console.log(`Rate limited, waiting ${waitTime}ms`), * })); * ``` */ export declare function createRateLimitInterceptor(options: RateLimiterOptions): ClientMiddleware; /** * Request transformation interceptor * * Transform request before sending. * * @example * ```typescript * client.use(createRequestTransformInterceptor((request) => { * // Add custom header based on request body * if (request.body?.priority === 'high') { * request.headers['X-Priority'] = 'high'; * } * return request; * })); * ``` */ export declare function createRequestTransformInterceptor(transform: (request: ClientMiddlewareRequest) => ClientMiddlewareRequest | Promise<ClientMiddlewareRequest>): ClientMiddleware; /** * Response transformation interceptor * * Transform response before returning. * * @example * ```typescript * client.use(createResponseTransformInterceptor((response) => { * // Add metadata to response * response.context.processedAt = Date.now(); * return response; * })); * ``` */ export declare function createResponseTransformInterceptor(transform: (response: ClientMiddlewareResponse) => ClientMiddlewareResponse | Promise<ClientMiddlewareResponse>): ClientMiddleware; /** * Caching interceptor * * Caches responses to reduce API calls. * * @example * ```typescript * client.use(createCacheInterceptor({ * ttl: 60000, // 1 minute * methods: ['GET'], * includePaths: [/\/api\/tools/, /\/api\/providers/], * })); * ``` */ export declare function createCacheInterceptor(options: CacheInterceptorOptions): ClientMiddleware; /** * Timeout interceptor * * Adds a timeout to requests. * * @example * ```typescript * client.use(createTimeoutInterceptor({ * timeout: 30000, // 30 seconds * onTimeout: (request) => console.log('Request timed out:', request.url), * })); * ``` */ export declare function createTimeoutInterceptor(options: TimeoutInterceptorOptions): ClientMiddleware; /** * Error handling interceptor * * Provides centralized error handling and transformation. * * @example * ```typescript * client.use(createErrorHandlerInterceptor({ * onError: (error, request) => { * console.error('Request failed:', error.message); * }, * reportError: async (error, context) => { * await errorReportingService.report(error, context); * }, * })); * ``` */ export declare function createErrorHandlerInterceptor(options?: ErrorHandlerOptions): ClientMiddleware; /** * Compose multiple middleware into one * * @example * ```typescript * const combinedMiddleware = composeMiddleware( * createLoggingInterceptor(), * createRetryInterceptor({ maxAttempts: 3 }), * createRateLimitInterceptor({ maxRequests: 100, windowMs: 60000 }), * ); * * client.use(combinedMiddleware); * ``` */ export declare function composeMiddleware(...middlewares: ClientMiddleware[]): ClientMiddleware; /** * Conditionally apply middleware * * @example * ```typescript * client.use(conditionalMiddleware( * (request) => request.url.includes('/api/agents'), * createLoggingInterceptor(), * )); * ``` */ export declare function conditionalMiddleware(condition: (request: ClientMiddlewareRequest) => boolean, middleware: ClientMiddleware): ClientMiddleware;