|
| 1 | +# <div align="center"> `RateLimit` Header Parser </div> |
| 2 | + |
| 3 | +<div align="center"> |
| 4 | + |
| 5 | +[](https://github.com/express-rate-limit/ratelimit-header-parser/actions/workflows/ci.yaml) |
| 6 | +[](https://npmjs.org/package/ratelimit-header-parser 'View this project on NPM') |
| 7 | +[](https://www.npmjs.com/package/ratelimit-header-parser) |
| 8 | + |
| 9 | +This library parses `RateLimit` headers of various forms into a normalized |
| 10 | +format. It supports the combined format specified in |
| 11 | +[draft 7](https://datatracker.ietf.org/doc/html/draft-ietf-httpapi-ratelimit-headers-07) |
| 12 | +of the |
| 13 | +[IETF Rate Limit Headers standard](https://github.com/ietf-wg-httpapi/ratelimit-headers), |
| 14 | +the uncombined `RateLimit-*` format of earlier drafts, traditional |
| 15 | +`X-RateLimit-*` headers, and a few other formats. |
| 16 | + |
| 17 | +</div> |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +```ts |
| 22 | +import { parseRateLimit } from 'ratelimit-header-parser' |
| 23 | + |
| 24 | +const response = await fetch( |
| 25 | + 'https://api.github.com/repos/express-rate-limit/express-rate-limit/contributors?anon=1', |
| 26 | +) |
| 27 | + |
| 28 | +console.log('github ratelimit:', parseRateLimit(response)) |
| 29 | +// > github ratelimit: { limit: 60, used: 1, remaining: 59, reset: 2023-08-25T04:16:48.000Z } |
| 30 | +``` |
| 31 | + |
| 32 | +For more examples, take a look at the [`examples/`](examples/) folder. |
| 33 | + |
| 34 | +## API |
| 35 | + |
| 36 | +### `parseRateLimit(responseOrHeaders, [options]) => object | undefined` |
| 37 | + |
| 38 | +Scans the input for ratelimit headers in a variety of formats and returns the |
| 39 | +result in a consistent format, or undefined if it fails to find any rate-limit |
| 40 | +headers. Returns an object with the following fields, or `undefined` if it does |
| 41 | +not find any rate-limit headers. |
| 42 | + |
| 43 | +```ts |
| 44 | +type RateLimitInfo = { |
| 45 | + limit: number |
| 46 | + used: number |
| 47 | + remaining: number |
| 48 | + reset: Date | undefined |
| 49 | +} |
| 50 | +``` |
| 51 | +
|
| 52 | +#### `responseOrHeaders` |
| 53 | +
|
| 54 | +> A node-style or fetch-style `Response`/`Headers` object. |
| 55 | +
|
| 56 | +#### `options` |
| 57 | +
|
| 58 | +> Options that configure how the library parses the headers. |
| 59 | +
|
| 60 | +```ts |
| 61 | +type Options = { |
| 62 | + // How to parse the `reset` field. If unset, the parser will guess based on |
| 63 | + // the content of the header. |
| 64 | + reset: | |
| 65 | + 'date' | // Pass the value to `new Date(...)` to let the JavaScript engine parse it. |
| 66 | + 'unix' | // Treat the value as the number of seconds since January 1, 1970 (A.K.A a UNIX epoch timestamp). |
| 67 | + 'seconds' | // Treat the value as the number of seconds from the current time. |
| 68 | + 'milliseconds' | // Treat the value as the number of milliseconds from the current time. |
| 69 | +} |
| 70 | +``` |
| 71 | +
|
| 72 | +## Issues and Contributing |
| 73 | +
|
| 74 | +If you encounter a bug or want to see something added/changed, please go ahead |
| 75 | +and |
| 76 | +[open an issue](https://github.com/nfriexpress-rate-limitedly/ratelimit-header-parser/issues/new)! |
| 77 | +If you need help with something, feel free to |
| 78 | +[start a discussion](https://github.com/express-rate-limit/ratelimit-header-parser/discussions/new)! |
| 79 | +
|
| 80 | +If you wish to contribute to the library, thanks! First, please read |
| 81 | +[the contributing guide](contributing.md). Then you can pick up any issue and |
| 82 | +fix/implement it! |
| 83 | +
|
| 84 | +## License |
| 85 | +
|
| 86 | +MIT © Express Rate Limit |
0 commit comments