@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.74 kB
TypeScript
/**
* @module RateLimiter
*/
import { type BackoffPolicy } from "../../../../backoff-policies/contracts/_module.js";
import { type IReadableContext } from "../../../../execution-context/contracts/_module.js";
import { type IRateLimiterStorageAdapter } from "../../../../rate-limiter/contracts/_module.js";
import { type AllRateLimiterState, type InternalRateLimiterPolicy } from "../../../../rate-limiter/implementations/adapters/database-rate-limiter-adapter/internal-rate-limiter-policy.js";
import { type InvokableFn } from "../../../../utilities/_module.js";
/**
* @internal
*/
export type RateLimiterStorageSettings<TMetrics> = {
adapter: IRateLimiterStorageAdapter<AllRateLimiterState<TMetrics>>;
rateLimiterPolicy: InternalRateLimiterPolicy<TMetrics>;
backoffPolicy: BackoffPolicy;
};
/**
* @internal
*/
export type IRateLimiterStorageState = {
success: boolean;
attempt: number;
resetTime: Date;
};
/**
* @internal
*/
export type AtomicUpdateArgs<TMetrics> = {
context: IReadableContext;
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>);
private static resolveStorageData;
private toAdapterState;
atomicUpdate(args: AtomicUpdateArgs<TMetrics>): Promise<IRateLimiterStorageState>;
find(context: IReadableContext, key: string): Promise<IRateLimiterStorageState | null>;
remove(context: IReadableContext, key: string): Promise<void>;
}