|
2 | 2 |
|
3 | 3 | [](https://github.com/express-rate-limit/ratelimit-header-parser/actions/workflows/ci.yml)
|
4 | 4 |
|
5 |
| -Parse RateLimit headers of various forms, including the combined form from |
| 5 | +Parse RateLimit headers of various forms into a normalized format. Supports the |
| 6 | +combined form from |
6 | 7 | [draft 7](https://datatracker.ietf.org/doc/html/draft-ietf-httpapi-ratelimit-headers-07)
|
7 | 8 | of the
|
8 | 9 | [IETF Rate Limit Headers standard](https://github.com/ietf-wg-httpapi/ratelimit-headers),
|
9 |
| -into a normalized format. |
10 |
| - |
11 |
| -### Todo |
12 |
| - |
13 |
| -- [x] transpiling |
14 |
| -- [x] linting |
15 |
| -- [x] ci |
16 |
| -- [ ] documentation |
17 |
| -- [ ] publish |
18 |
| -- [ ] better test coverage |
19 |
| -- [ ] test in [Deno](https://deno.com/) |
20 |
| -- [ ] test browsers |
21 |
| -- [ ] test in [React Native](https://reactnative.dev/) |
| 10 | +the uncombined `RateLimit-*` format of earlier drafts, traditional |
| 11 | +`X-RateLimit-*` headers, and a few other formats. |
| 12 | + |
| 13 | +## Usage: |
| 14 | + |
| 15 | +```js |
| 16 | +import { parseRateLimit } from 'ratelimit-header-parser' |
| 17 | + |
| 18 | +const response = await fetch( |
| 19 | + 'https://api.github.com/repos/express-rate-limit/express-rate-limit/contributors?anon=1', |
| 20 | +) |
| 21 | + |
| 22 | +console.log('github ratelimit:', parseRateLimit(response)) |
| 23 | +// > github ratelimit: { limit: 60, used: 1, remaining: 59, reset: 2023-08-25T04:16:48.000Z } |
| 24 | +``` |
| 25 | + |
| 26 | +## API |
| 27 | + |
| 28 | +### parseRateLimit(responseOrHeadersObject, [options]) => Object | undefined |
| 29 | + |
| 30 | +Scans the input for ratelimit headers in a variety of formats and returns the |
| 31 | +result in a consistent format, or undefined if it fails to find any ratelimit |
| 32 | +headers. |
| 33 | + |
| 34 | +- `responseOrHeadersObject`: May be either a fetch-style Response or Headers |
| 35 | + object or a node.js-style response or headers object |
| 36 | +- `options`: Optional object with the following optional fields: |
| 37 | + - `reset`: How to parse the reset field. If unset, the parser will guess based |
| 38 | + on the content. Accepts the following strings: |
| 39 | + - `'date'`: past the value to `new Date(...)` to let the JavaScript engine |
| 40 | + parse it |
| 41 | + - `'unix'`: treat the value as the number of seconds since January 1, 1970 |
| 42 | + (A.K.A a unix timestamp) |
| 43 | + - `'seconds'`: treat the value as the number of seconds from the current |
| 44 | + time |
| 45 | + - `'milliseconds'`: treat the value as the number of milliseconds from the |
| 46 | + current time |
| 47 | + |
| 48 | +Returns a object with the following fields, or undefined if it does not find any |
| 49 | +rate-limit headers. |
| 50 | + |
| 51 | +``` |
| 52 | +{ |
| 53 | + limit: number |
| 54 | + used: number |
| 55 | + remaining: number |
| 56 | + reset: Date or undefined |
| 57 | +} |
| 58 | +``` |
0 commit comments