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.

103 lines (102 loc) 4.04 kB
/** * @module RateLimiter */ import { type ClientSession, type CollectionOptions, type Db, type MongoClient, type ObjectId } from "mongodb"; import { type IReadableContext } from "../../../../execution-context/contracts/_module.js"; import { type IRateLimiterData, type IRateLimiterStorageAdapter, type IRateLimiterStorageAdapterTransaction } from "../../../../rate-limiter/contracts/_module.js"; import { type ISerde } from "../../../../serde/contracts/_module.js"; import { type IDeinitizable, type IInitizable, type InvokableFn } from "../../../../utilities/_module.js"; /** * Configuration for `MongodbRateLimiterStorageAdapter`. * Requires a MongoDB `Db` instance. * * IMPORT_PATH: `"@daiso-tech/core/rate-limiter/mongodb-rate-limiter-storage-adapter"` * @group Adapters */ export type MongodbRateLimiterStorageAdapterSettings = { /** * The MongoDB `MongoClient` instance, required for transaction support. */ client: MongoClient; /** * The MongoDB `Db` instance to store rate-limiter state in. */ database: Db; /** * Name of the MongoDB collection used to store rate-limiter state records. * @default "rateLimiter" */ collectionName?: string; /** * Additional options passed when creating or accessing the MongoDB collection. */ collectionSettings?: CollectionOptions; /** * Serde instance for serializing and deserializing rate-limiter state to and from strings. */ serde: ISerde<string>; /** * When `true`, operations are wrapped in MongoDB transactions for atomicity. * Requires a Replica Set or sharded cluster that supports transactions. * @default true */ enableTransactions?: boolean; }; /** * IMPORT_PATH: `"@daiso-tech/core/rate-limiter/mongodb-rate-limiter-storage-adapter"` * @group Adapters */ export type MongodbRateLimiterDocument = { _id: ObjectId; key: string; state: string; expiration: Date; }; /** * IMPORT_PATH: `"@daiso-tech/core/rate-limiter/mongodb-rate-limiter-storage-adapter"` * @group Adapters */ export declare class MongodbRateLimiterStorageAdapter<TType> implements IRateLimiterStorageAdapter<TType>, IInitizable, IDeinitizable { private readonly client; private readonly collection; private readonly serde; private readonly enableTransactions; /** * @example * ```ts * import { MongodbRateLimiterStorageAdapter } from "@daiso-tech/core/rate-limiter/mongodb-rate-limiter-storage-adapter"; * import { MongoClient } from "mongodb"; * import { Serde } from "@daiso-tech/core/serde"; * import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/super-json-serde-adapter" * * const client = await MongoClient.connect("YOUR_MONGODB_CONNECTION_STRING"); * const database = client.db("database"); * const serde = new Serde(new SuperJsonSerdeAdapter()); * const rateLimiterStorageAdapter = new MongodbRateLimiterStorageAdapter({ * client, * database, * serde * }); * // You need initialize the adapter once before using it. * await rateLimiterStorageAdapter.init() * ``` */ constructor(settings: MongodbRateLimiterStorageAdapterSettings); /** * Removes the collection where the rate limiter keys are stored and all it's related indexes. * Note all rate limiter data will be removed. */ deInit(): Promise<void>; /** * Creates all related indexes. * Note the `init` method needs to be called once before using the adapter. */ init(): Promise<void>; private upsert; private _transaction; transaction<TValue>(_context: IReadableContext, fn: InvokableFn<[ transaction: IRateLimiterStorageAdapterTransaction<TType> ], Promise<TValue>>): Promise<TValue>; find(_context: IReadableContext, key: string, session?: ClientSession): Promise<IRateLimiterData<TType> | null>; remove(_context: IReadableContext, key: string, session?: ClientSession): Promise<void>; }