Documentation Index
Fetch the complete documentation index at: https://mintlify.com/visible/cruel/llms.txt
Use this file to discover all available pages before exploring further.
Function Signature
function createRateLimiter<T extends AnyFn>(
fn: T,
options: RateLimiterOptions
): T
Wraps a function with rate limiting using token bucket algorithm. Throws CruelRateLimitError when rate limit is exceeded.
Options
Number of requests allowed per interval.
Time interval in milliseconds for the rate limit window.
Callback invoked when the rate limit is exceeded.
Return Type
Returns the wrapped function with rate limiting.
Types
interface RateLimiterOptions {
requests: number
interval: number
onLimit?: () => void
}
Example
import { createRateLimiter, CruelRateLimitError } from 'cruel'
const rateLimitedFetch = createRateLimiter(
async (url: string) => {
const response = await fetch(url)
return response.json()
},
{
requests: 10,
interval: 60000, // 10 requests per minute
onLimit: () => {
console.warn('Rate limit exceeded')
}
}
)
try {
const data = await rateLimitedFetch('https://api.example.com/data')
} catch (error) {
if (error instanceof CruelRateLimitError) {
console.log(`Rate limited. Retry after ${error.retryAfter}s`)
}
}