UNPKG

rate-keeper

Version:

A lightweight utility for easily adding rate limiting to functions, ideal for preventing API rate limit violations.

37 lines 1.68 kB
/** * @enum {DropPolicy} Defines the behavior of the queue when the maximum size has been reached. */ export declare enum DropPolicy { Reject = 0, DropOldest = 1 } /** * @param {number} id A queue identifier; actions in the same queue are rate-limited and executed sequentially, 0 is a reserved value. * @param {number} maxQueueSize Optional. Max size of the queue. * @param {DropPolicy} dropPolicy Optional. Policy when max size is reached: 'Reject' or 'DropOldest'. */ export interface QueueSettings { id: number; maxQueueSize?: number; dropPolicy?: DropPolicy; } /** * A Promise that can be cancelled before it resolves. * @template T - The type of the resolved value. * @extends {Promise<T>} */ export interface CancelablePromise<T> extends Promise<T> { /** * Cancels the pending action if it hasn't been executed yet. * @param {Error} [reason] - Optional error to reject the promise with. Defaults to "Cancelled by user." */ cancel: (reason?: Error) => void; } /** * @param {(...args: Args) => Result} action The action to be rate-limited. * @param {number} rateLimit The minimum interval in milliseconds between each execution. * @param {QueueSettings} settings Optional. Queue settings for rate limiting and execution. * @returns {(...args: Args) => CancelablePromise<Result>} An asynchronous function that executes the action and returns a promise with the result and a cancel method. */ export default function RateKeeper<Args extends unknown[], Result>(action: (...args: Args) => Result, rateLimit: number, settings?: QueueSettings): (...args: Args) => CancelablePromise<Result>; //# sourceMappingURL=index.d.ts.map