1- // Public rate limit helpers.
2- //
3- // Used by handlers and admin tools to inspect, reset, and manage exemptions.
1+ /**
2+ * Public rate limit helpers.
3+ *
4+ * Used by handlers and admin tools to inspect, reset, and manage exemptions.
5+ */
46
57import type { CommandKitEnvironment , Context } from 'commandkit' ;
68import { RATELIMIT_STORE_KEY } from './constants' ;
@@ -51,6 +53,9 @@ export interface ResetAllRateLimitsParams {
5153
5254/**
5355 * Read aggregated rate limit info stored on a CommandKit env or context.
56+ *
57+ * @param envOrCtx - CommandKit environment or context holding the rate-limit store.
58+ * @returns Aggregated rate-limit info or null when no store is present.
5459 */
5560export function getRateLimitInfo (
5661 envOrCtx : CommandKitEnvironment | Context | null | undefined ,
@@ -61,10 +66,22 @@ export function getRateLimitInfo(
6166 return ( store . get ( RATELIMIT_STORE_KEY ) as RateLimitStoreValue ) ?? null ;
6267}
6368
69+ /**
70+ * Resolve the active storage or throw when none is configured.
71+ *
72+ * @returns Configured rate-limit storage.
73+ * @throws Error when storage is not configured.
74+ */
6475function getRequiredStorage ( ) : RateLimitStorage {
6576 return getRuntimeStorage ( ) . storage ;
6677}
6778
79+ /**
80+ * Resolve runtime context plus the effective storage to use.
81+ *
82+ * @returns Runtime context (if any) and the resolved storage.
83+ * @throws Error when storage is not configured.
84+ */
6885function getRuntimeStorage ( ) : {
6986 runtime : ReturnType < typeof getRateLimitRuntime > ;
7087 storage : RateLimitStorage ;
@@ -77,12 +94,22 @@ function getRuntimeStorage(): {
7794 return { runtime, storage } ;
7895}
7996
97+ /**
98+ * Normalize a prefix to include the window suffix marker.
99+ *
100+ * @param prefix - Base key prefix.
101+ * @returns Prefix guaranteed to end with `w:`.
102+ */
80103function toWindowPrefix ( prefix : string ) : string {
81104 return prefix . endsWith ( ':' ) ? `${ prefix } w:` : `${ prefix } :w:` ;
82105}
83106
84107/**
85108 * Reset a single key and its violation/window variants to keep state consistent.
109+ *
110+ * @param params - Reset parameters for a single key or scope-derived key.
111+ * @returns Resolves when deletes and reset hooks (if any) complete.
112+ * @throws Error when required scope identifiers are missing.
86113 */
87114export async function resetRateLimit (
88115 params : ResetRateLimitParams ,
@@ -127,6 +154,11 @@ export async function resetRateLimit(
127154
128155/**
129156 * Reset multiple keys by scope, command name, prefix, or pattern for bulk cleanup.
157+ *
158+ * @param params - Batch reset parameters, defaulting to an empty config.
159+ * @returns Resolves when all matching keys are deleted.
160+ * @throws Error when the storage backend lacks required delete helpers.
161+ * @throws Error when scope identifiers are missing for scope-based resets.
130162 */
131163export async function resetAllRateLimits (
132164 params : ResetAllRateLimitsParams = { } ,
@@ -196,6 +228,10 @@ export async function resetAllRateLimits(
196228
197229/**
198230 * Grant a temporary exemption for a scope/id pair.
231+ *
232+ * @param params - Exemption scope, id, and duration.
233+ * @returns Resolves when the exemption key is written.
234+ * @throws Error when duration is missing or non-positive.
199235 */
200236export async function grantRateLimitExemption (
201237 params : RateLimitExemptionGrantParams ,
@@ -214,6 +250,9 @@ export async function grantRateLimitExemption(
214250
215251/**
216252 * Revoke a temporary exemption for a scope/id pair.
253+ *
254+ * @param params - Exemption scope and id to revoke.
255+ * @returns Resolves when the exemption key is removed.
217256 */
218257export async function revokeRateLimitExemption (
219258 params : RateLimitExemptionRevokeParams ,
@@ -226,6 +265,10 @@ export async function revokeRateLimitExemption(
226265
227266/**
228267 * List exemptions by scope and/or id for admin/reporting.
268+ *
269+ * @param params - Optional scope/id filters and limits.
270+ * @returns Exemption info entries that match the requested filters.
271+ * @throws Error when scope is required but missing or listing is unsupported.
229272 */
230273export async function listRateLimitExemptions (
231274 params : RateLimitExemptionListParams = { } ,
@@ -281,6 +324,13 @@ export async function listRateLimitExemptions(
281324 return results ;
282325}
283326
327+ /**
328+ * Delete windowed variants for a base key using available storage helpers.
329+ *
330+ * @param storage - Storage driver to delete from.
331+ * @param key - Base key to delete window variants for.
332+ * @returns Resolves after window variants are removed.
333+ */
284334async function deleteWindowVariants (
285335 storage : RateLimitStorage ,
286336 key : string ,
0 commit comments