TypeScript Types

Complete TypeScript type definitions for hitlimit.

HitlimitOptions

Main configuration options for the hitlimit function.

interface HitlimitOptions {
  /** Maximum requests per window */
  limit: number

  /** Time window (e.g., '1m', '1h', 60000) */
  window: string | number

  /** Key extraction function */
  key?: (req: Request) => string

  /** Storage backend */
  store?: Store

  /** Skip rate limiting for certain requests */
  skip?: (req: Request) => boolean

  /** Include rate limit headers */
  headers?: boolean

  /** HTTP status when rate limited */
  statusCode?: number

  /** Custom error response */
  response?: object | ((req: Request, res: Response) => object)
}

RateLimitInfo

Rate limit state for a specific key.

interface RateLimitInfo {
  /** Current request count */
  count: number

  /** Unix timestamp when window resets */
  resetTime: number
}

Store

Interface for rate limit storage backends.

interface Store {
  /** Get current rate limit info */
  get(key: string): Promise<RateLimitInfo | null>

  /** Increment counter and return updated info */
  increment(key: string, windowMs: number): Promise<RateLimitInfo>

  /** Reset rate limit for a key */
  reset(key: string): Promise<void>

  /** Optional cleanup method */
  close?(): Promise<void>
}

Store Options

MemoryStoreOptions

interface MemoryStoreOptions {
  /** Cleanup interval in ms (default: 60000) */
  cleanupInterval?: number
}

SqliteStoreOptions

interface SqliteStoreOptions {
  /** Path to SQLite database file */
  path: string

  /** Table name (default: 'rate_limits') */
  tableName?: string
}

RedisStoreOptions

interface RedisStoreOptions {
  /** Redis connection URL */
  url: string

  /** Key prefix (default: 'rl:') */
  prefix?: string

  /** Existing Redis client */
  client?: RedisClient
}

Middleware Types

type Middleware = (
  req: Request,
  res: Response,
  next: NextFunction
) => void | Promise<void>

type NextFunction = (err?: any) => void

Augmented Request

The request object is augmented with rate limit info:

interface Request {
  rateLimit?: {
    limit: number
    remaining: number
    resetTime: number
  }
}

Usage Example

import type { HitlimitOptions, Store } from '@joint-ops/hitlimit'

const options: HitlimitOptions = {
  limit: 100,
  window: '1m',
  headers: true
}

const customStore: Store = {
  async get(key) { /* ... */ },
  async increment(key, windowMs) { /* ... */ },
  async reset(key) { /* ... */ }
}