@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.
49 lines (48 loc) • 1.65 kB
TypeScript
/**
* @module RateLimiter
*/
import { type BackoffPolicy } from "../../../../backoff-policies/contracts/_module.js";
import { RATE_LIMITER_STATE, type IRateLimiterPolicy } from "../../../../rate-limiter/contracts/_module.js";
/**
* @internal
*/
export type AllowedState<TMetrics = unknown> = {
type: (typeof RATE_LIMITER_STATE)["ALLOWED"];
metrics: TMetrics;
};
/**
* @internal
*/
export type BlockedState = {
type: (typeof RATE_LIMITER_STATE)["BLOCKED"];
/**
* Unix timestamp in miliseconds
*/
startedAt: number;
attempt: number;
};
/**
* @internal
*/
export type AllRateLimiterState<TMetrics = unknown> = AllowedState<TMetrics> | BlockedState;
/**
* @internal
*/
export type BackoffPolicySettings = {
currentDate: Date;
backoffPolicy: BackoffPolicy;
};
/**
* @internal
*/
export declare class InternalRateLimiterPolicy<TMetrics = unknown> {
private readonly rateLimiterPolicy;
constructor(rateLimiterPolicy: IRateLimiterPolicy<TMetrics>);
initialState(currentDate: Date): AllowedState<TMetrics>;
whenAllowed(currentState: AllowedState<TMetrics>, limit: number, currentDate: Date): AllRateLimiterState<TMetrics>;
whenBlocked(currentState: BlockedState, settings: BackoffPolicySettings): AllRateLimiterState<TMetrics>;
trackWhenAllowed(currentState: AllowedState<TMetrics>, currentDate: Date): AllowedState<TMetrics>;
trackWhenBlocked(currentState: BlockedState): BlockedState;
getExpiration(currentState: AllRateLimiterState<TMetrics>, settings: BackoffPolicySettings): Date;
getAttempts(currentState: AllRateLimiterState<TMetrics>, currentDate: Date): number;
}