A TypeScript middleware library for intelligent API rate limiting with adaptive thresholds that automatically adjusts based on endpoint usage patterns and server load.
npm install throttlegateimport express from 'express';
import { createThrottleGate } from 'throttlegate';
const app = express();
// Apply rate limiting: 100 requests per 15 minutes
const rateLimiter = createThrottleGate({
rateLimit: 100,
windowMs: 15 * 60 * 1000 // 15 minutes
});
app.use('/api', rateLimiter);
app.listen(3000);import { ThrottleGate } from 'throttlegate';
const throttle = new ThrottleGate({
rateLimit: 1000,
windowMs: 60 * 1000, // 1 minute
adaptive: true, // Automatically adjust limits based on load
analytics: true, // Enable usage tracking
algorithm: 'sliding-window'
});
app.use('/api', throttle.middleware());
// Monitor analytics
setInterval(() => {
const stats = throttle.getAnalytics()();
console.log('API Stats:', stats);
}, 30000);const rateLimiter = createThrottleGate({
rateLimit: 50,
windowMs: 10 * 60 * 1000,
onLimitReached: (req, res) => {
res.status(429).json({
error: 'Rate limit exceeded',
retryAfter: 600
});
}
});rateLimit: Maximum requests per windowwindowMs: Time window in millisecondsalgorithm: 'fixed-window' | 'sliding-window' | 'token-bucket' (default: 'fixed-window')adaptive: Enable adaptive rate limiting (default: false)analytics: Enable usage analytics (default: false)onLimitReached: Custom handler for rate limit exceeded
- Adaptive Thresholds: Automatically adjusts rate limits based on server load
- Multiple Algorithms: Support for different throttling strategies
- Real-time Analytics: Track request patterns and performance metrics
- TypeScript Support: Full type safety and IntelliSense
- Express Integration: Easy middleware integration
MIT