UNPKG

rate-keeper

Version:

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

32 lines 1.41 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'. */ interface QueueSettings { id: number; maxQueueSize?: number; dropPolicy?: DropPolicy; } /** * Extend Promise with a cancel method */ export interface CancelablePromise<T> extends Promise<T> { 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>; export {}; //# sourceMappingURL=index.d.ts.map