UNPKG

mpesajs

Version:

A Node.js SDK for seamless integration with M-Pesa payment gateway, providing easy-to-use methods for handling transactions, payments, and API interactions

85 lines (84 loc) 2.62 kB
/** * Configuration for retry behavior when requests fail */ interface RetryConfig { maxRetries: number; initialDelayMs: number; maxDelayMs: number; backoffFactor: number; } /** * Configuration for rate limiting behavior */ interface RateLimitConfig { maxConcurrent: number; timeWindowMs: number; } /** * RateLimiter class implements rate limiting and retry logic for API requests * Uses the Singleton pattern to ensure only one instance exists */ export declare class RateLimiter { private static instance; private queue; private activeRequests; private requestTimestamps; private retryConfig; private rateLimitConfig; /** * Private constructor to prevent direct instantiation * Initializes default configurations for retry and rate limiting */ private constructor(); /** * Gets the singleton instance of RateLimiter * Creates the instance if it doesn't exist */ static getInstance(): RateLimiter; /** * Updates the retry configuration * Allows partial updates while maintaining existing values */ setRetryConfig(config: Partial<RetryConfig>): void; /** * Updates the rate limit configuration * Allows partial updates while maintaining existing values */ setRateLimitConfig(config: Partial<RateLimitConfig>): void; /** * Creates a promise that resolves after the specified delay * Used for implementing delays between retries */ private delay; /** * Calculates the delay time for the next retry attempt using exponential backoff * Includes random jitter to prevent thundering herd problem */ private calculateBackoff; /** * Determines if an error should trigger a retry attempt * Retries on network errors and specific HTTP status codes */ private shouldRetry; /** * Executes an operation with automatic retries on failure * Implements exponential backoff between retry attempts */ private executeWithRetry; /** * Removes timestamps older than the configured time window * Helps maintain accurate count of recent requests */ private cleanupOldTimestamps; /** * Waits until a request slot becomes available * Ensures we don't exceed the configured rate limits */ private waitForAvailableSlot; /** * Main method to execute an operation with rate limiting and retry logic * Ensures operations don't exceed rate limits and handles retries on failure */ execute<T>(operation: () => Promise<T>): Promise<T>; } export {};