@daiso-tech/core
Version:
The library offers flexible, framework-agnostic solutions for modern web applications, built on adaptable components that integrate seamlessly with popular frameworks like Next Js.
46 lines (45 loc) • 1.49 kB
TypeScript
/**
* @module RateLimiter
*/
import { type BackoffPolicy } from "../../../../backoff-policies/_module.js";
import { type IRateLimiterStorageAdapter } from "../../../../rate-limiter/contracts/_module.js";
import { type AllRateLimiterState, type RateLimiterPolicy } from "../../../../rate-limiter/implementations/adapters/database-rate-limiter-adapter/rate-limiter-policy.js";
import { type InvokableFn } from "../../../../utilities/_module.js";
/**
* @internal
*/
export type RateLimiterStorageSettings<TMetrics> = {
adapter: IRateLimiterStorageAdapter<AllRateLimiterState<TMetrics>>;
rateLimiterPolicy: RateLimiterPolicy<TMetrics>;
backoffPolicy: BackoffPolicy;
};
/**
* @internal
*/
export type IRateLimiterStorageState = {
success: boolean;
attempt: number;
resetTime: Date | null;
};
/**
* @internal
*/
export type AtomicUpdateArgs<TMetrics> = {
key: string;
update: InvokableFn<[
currentState: AllRateLimiterState<TMetrics>
], AllRateLimiterState<TMetrics>>;
};
/**
* @internal
*/
export declare class RateLimiterStorage<TMetrics = unknown> {
private readonly adapter;
private readonly rateLimiterPolicy;
private readonly backoffPolicy;
constructor(settings: RateLimiterStorageSettings<TMetrics>);
atomicUpdate(args: AtomicUpdateArgs<TMetrics>): Promise<IRateLimiterStorageState>;
private toAdapterState;
find(key: string): Promise<IRateLimiterStorageState | null>;
remove(key: string): Promise<void>;
}