ts-prime
Version:
A utility library for JavaScript and Typescript.
23 lines • 1.11 kB
TypeScript
import { ArgsType } from "./cache";
export interface RateLimiterOptions<F extends (...args: unknown[]) => Promise<unknown>> {
rateLimitId: (...args: ArgsType<F>) => string;
concurrentRequests?: ((rateLimitId: string) => number) | number;
maxTotalRequests?: number;
}
/**
* Controls how many concurrent execution can be invoked. At any given time
* @param errorHandler logic when to retry
* @param request request function
* @signature
* P.concurrent(fn, options)
* @example
* const requestToEndpoint = async (endpoint: "A" | "B" | "C" | "D", data: any) => { ... }
* const rate = P.concurrent(requestToEndpoint, ({ rateLimitId: (endpoint) => endpoint, concurrentRequests: 2 })
* // Only two request are fired to endpoint A
* const endpoints = await Promise.all([{ endpoint: A, data: any }, ...].map(async (obj)=>{
* return rate(obj.endpoint, obj.data)
* }))
* @category Utility, Promise
*/
export declare function concurrent<F extends (...args: any[]) => Promise<unknown>>(request: F, options: RateLimiterOptions<F>): F;
//# sourceMappingURL=rateLimiter.d.ts.map