Skip to content

Commit 05cc223

Browse files
committed
docs: update license and readme
1 parent d8f9af2 commit 05cc223

File tree

4 files changed

+106
-80
lines changed

4 files changed

+106
-80
lines changed

LICENSE

Lines changed: 0 additions & 21 deletions
This file was deleted.

README.md

Lines changed: 0 additions & 59 deletions
This file was deleted.

license.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# MIT License
2+
3+
Copyright (c) 2023 Express Rate Limit
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

readme.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# <div align="center"> `RateLimit` Header Parser </div>
2+
3+
<div align="center">
4+
5+
[![tests](https://github.com/express-rate-limit/ratelimit-header-parser/actions/workflows/ci.yaml/badge.svg)](https://github.com/express-rate-limit/ratelimit-header-parser/actions/workflows/ci.yaml)
6+
[![npm version](https://img.shields.io/npm/v/ratelimit-header-parser.svg)](https://npmjs.org/package/ratelimit-header-parser 'View this project on NPM')
7+
[![npm downloads](https://img.shields.io/npm/dm/ratelimit-header-parser)](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

Comments
 (0)