express-rate-limit / ratelimit-header-parser

Parse RateLimit headers of various forms into a normalized format
MIT License
7 stars 0 forks source link
api api-client deno header headers javascript nodejs parser rate-limit rate-limiting rest-api

ratelimit-header-parser

CI npm version

Parse RateLimit headers of various forms into a normalized format. Supports the combined form from draft 7 of the IETF Rate Limit Headers standard, the uncombined RateLimit-* format of earlier drafts, traditional X-RateLimit-* headers, and a few other formats.

Usage:

import { parseRateLimit } from 'ratelimit-header-parser'

const response = await fetch(
    'https://api.github.com/repos/express-rate-limit/express-rate-limit/contributors?anon=1',
)

console.log('github ratelimit:', parseRateLimit(response))
// > github ratelimit: { limit: 60, used: 1, remaining: 59, reset: 2023-08-25T04:16:48.000Z }

API

parseRateLimit(responseOrHeadersObject, [options]) => Object | undefined

Scans the input for ratelimit headers in a variety of formats and returns the result in a consistent format, or undefined if it fails to find any ratelimit headers.

Returns a object with the following fields, or undefined if it does not find any rate-limit headers.

{
    limit: number
    used: number
    remaining: number
    reset: Date or undefined
}