Skip to content

Commit 655b14d

Browse files
committed
document API
1 parent 7904e69 commit 655b14d

File tree

3 files changed

+58
-25
lines changed

3 files changed

+58
-25
lines changed

README.md

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,57 @@
22

33
[![CI](https://github.com/express-rate-limit/ratelimit-header-parser/actions/workflows/ci.yml/badge.svg)](https://github.com/express-rate-limit/ratelimit-header-parser/actions/workflows/ci.yml)
44

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
67
[draft 7](https://datatracker.ietf.org/doc/html/draft-ietf-httpapi-ratelimit-headers-07)
78
of the
89
[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+
```

examples/github.mjs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
import { parseRateLimit } from 'ratelimit-header-parser'
22

3-
const response = await fetch("https://api.github.com/repos/express-rate-limit/express-rate-limit/contributors?anon=1&per_page=100", {
4-
"credentials": "omit",
5-
"headers": {
6-
"Accept": "application/vnd.github.v3+json",
7-
},
8-
"method": "GET",
9-
"mode": "cors"
10-
});
3+
const response = await fetch(
4+
'https://api.github.com/repos/express-rate-limit/express-rate-limit/contributors?anon=1',
5+
)
116

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

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@
115115
"@typescript-eslint/no-unsafe-argument": 0
116116
}
117117
}
118-
]
118+
],
119+
"ignores": ["examples/"]
119120
},
120121
"prettier": "@express-rate-limit/prettier",
121122
"lint-staged": {

0 commit comments

Comments
 (0)