congress-dot-gov
Version:
TypeScript SDK for the Congress.gov API
52 lines (49 loc) • 2.04 kB
TypeScript
import { R as RateLimitInfo } from '../shared/congress-dot-gov.CMneUPHP.js';
export { a as RateLimiter } from '../shared/congress-dot-gov.CMneUPHP.js';
interface RateLimiterConfig {
/**
* Safety margin as a percentage (0-1).
* 0.1 means we'll try to use only 90% of available requests
*/
safetyMargin?: number;
/**
* Minimum delay between requests in milliseconds
*/
minDelay?: number;
/**
* Maximum delay between requests in milliseconds
*/
maxDelay?: number;
}
/**
* An adaptive rate limiter that adjusts the delay between requests based on the current rate limit status.
* It uses a safety margin to ensure that the rate limit is not exceeded.
* It also has a minimum and maximum delay to prevent the delay from becoming too small or too large.
* @constructor config - The configuration for the rate limiter
* @param config.safetyMargin - The safety margin as a percentage (0-1). 0.1 means we'll try to use only 90% of available requests
* @param config.minDelay - The minimum delay between requests in milliseconds
* @param config.maxDelay - The maximum delay between requests in milliseconds
*/
declare class AdaptiveRateLimiter {
private config;
private lastRequestTime;
private currentDelay;
private hourStartTime;
constructor(config?: RateLimiterConfig);
/**
* Call this before making an API request. It will wait the appropriate
* amount of time based on current rate limit status.
* @returns A promise that resolves when the next request can be made
*/
waitForNextRequest(): Promise<void>;
/**
* Calculate delay based on amount of time remaining in the hour and number of requests remaining
* @param rateLimitInfo - The current rate limit info
* @param currentTime - The current time
* @returns The delay in milliseconds
*/
private calculateDelay;
private sleep;
updateRateLimitInfo(rateLimitInfo: RateLimitInfo): void;
}
export { AdaptiveRateLimiter, RateLimitInfo };