UNPKG

@da440dil/js-counter

Version:

Distributed rate limiting using Redis

24 lines (23 loc) 924 B
import { IRedisClient, INodeRedisClient } from '@da440dil/js-redis-script'; import { Result } from './Result'; /** One of algorithms: 1 stands for the fixed window algorithm, 2 stands for the sliding window algorithm. */ export declare const Algorithm: { /** Fixed window. */ readonly Fixed: 1; /** Sliding window. */ readonly Sliding: 2; }; export declare type Algorithm = typeof Algorithm[keyof typeof Algorithm]; /** Implements distributed counter. */ export declare class Counter { private size; private limit; private script; constructor(client: IRedisClient | INodeRedisClient, size: number, limit: number, algorithm: Algorithm); /** * Increments key value by specified value. * @param key The key to be incremented. * @param value The value the key value to be incremented by. */ count(key: string, value: number): Promise<Result>; }