UNPKG

@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.

65 lines (64 loc) 2.31 kB
/** * @module RateLimiter */ import { type Redis, type Result } from "ioredis"; import { type BackoffSettingsEnum } from "../../../../backoff-policies/_module.js"; import { type IRateLimiterAdapter, type IRateLimiterAdapterState } from "../../../../rate-limiter/contracts/_module.js"; import { type RateLimiterPolicySettingsEnum } from "../../../../rate-limiter/implementations/policies/_module.js"; declare module "ioredis" { interface RedisCommander<Context> { /** * @returns {string} {@link IRedisJsonRateLimiterState | `IRedisJsonRateLimiterState | null`} as json string. */ daiso_rate_limiter_update_state(key: string, limit: number, backoffSettings: string, policySettings: string, currentDate: number): Result<string, Context>; } } /** * IMPORT_PATH: `"@daiso-tech/core/rate-limiter/redis-rate-limiter-adapter"` * @group Adapters */ export type RedisRateLimiterAdapterSettings = { database: Redis; /** * You can choose between different types of predefined backoff policies. * @default * ```ts * { type: BACKOFFS.EXPONENTIAL } * ``` */ backoff?: BackoffSettingsEnum; /** * You can choose between different types of predefined circuit breaker policies. * @default * ```ts * { type: LIMITER.FIXED_WINDOW } * ``` */ policy?: RateLimiterPolicySettingsEnum; }; /** * IMPORT_PATH: `"@daiso-tech/core/rate-limiter/redis-rate-limiter-adapter"` * @group Adapters */ export declare class RedisRateLimiterAdapter implements IRateLimiterAdapter { private readonly backoff; private readonly policy; private readonly database; /** * @example * ```ts * import { RedisRateLimiterAdapter } from "@daiso-tech/core/rate-limiter/redis-rate-limiter-adapter"; * import Redis from "ioredis"; * * const database = new Redis("YOUR_REDIS_CONNECTION_STRING"); * const rateLimiterAdapter = new RedisRateLimiterAdapter({ * database * }); * ``` */ constructor(settings: RedisRateLimiterAdapterSettings); private initUpdateStateCommmand; getState(key: string): Promise<IRateLimiterAdapterState | null>; updateState(key: string, limit: number): Promise<IRateLimiterAdapterState>; reset(key: string): Promise<void>; }