Skip to content

Commit 3c8fcdf

Browse files
committed
separate out types and add todo
1 parent 8c7390e commit 3c8fcdf

File tree

4 files changed

+42
-19
lines changed

4 files changed

+42
-19
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,13 @@ Parse RateLimit headers of various forms, including the combined form from
44
[draft 7](https://datatracker.ietf.org/doc/html/draft-ietf-httpapi-ratelimit-headers-07) of the
55
[IETF Rate Limit Headers standard](https://github.com/ietf-wg-httpapi/ratelimit-headers),
66
into a normalized format.
7+
8+
9+
### Todo
10+
11+
* [ ] transpiling
12+
* [ ] linting
13+
* [ ] ci
14+
* [ ] documentation
15+
* [ ] more tests
16+
* [ ] publish

source/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export { parseRateLimit } from './ratelimit-header-parser'
2+
export {
3+
RateLimit,
4+
RateLimitOptions,
5+
ResponseOrHeadersObject
6+
} from './types'

source/ratelimit-header-parser.ts

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,11 @@
1-
import { ServerResponse, IncomingHttpHeaders, OutgoingHttpHeaders } from 'node:http'
2-
3-
export type RateLimit = {
4-
limit: number,
5-
current: number,
6-
remaining: number,
7-
reset?: Date,
8-
// todo: policy
9-
}
10-
11-
export type RateLimitOptions = {
12-
reset?: 'date' | 'unix' | 'seconds' | 'milliseconds',
13-
}
14-
15-
// node or fetch
16-
type ResponseObject = ServerResponse | Response;
17-
type HeadersObject = IncomingHttpHeaders | OutgoingHttpHeaders | Headers | Object;
18-
19-
export function parseRateLimit(input: ResponseObject | HeadersObject, options?: RateLimitOptions): RateLimit | undefined {
1+
import {
2+
HeadersObject,
3+
RateLimit,
4+
RateLimitOptions,
5+
ResponseOrHeadersObject
6+
} from './types'
7+
8+
export function parseRateLimit(input: ResponseOrHeadersObject, options?: RateLimitOptions): RateLimit | undefined {
209
if ('headers' in input && typeof input.headers === 'object' && !Array.isArray(input.headers)) {
2110
return parseRateLimit(input.headers, options)
2211
} else if ('getHeaders' in input && typeof input.getHeaders === 'function') {

source/types.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { ServerResponse, IncomingHttpHeaders, OutgoingHttpHeaders } from 'node:http'
2+
3+
export type RateLimit = {
4+
limit: number,
5+
current: number,
6+
remaining: number,
7+
reset?: Date,
8+
// todo: policy
9+
}
10+
11+
export type RateLimitOptions = {
12+
reset?: 'date' | 'unix' | 'seconds' | 'milliseconds',
13+
}
14+
15+
// node or fetch
16+
export type ResponseObject = ServerResponse | Response;
17+
export type HeadersObject = IncomingHttpHeaders | OutgoingHttpHeaders | Headers | Object;
18+
export type ResponseOrHeadersObject = ResponseObject | HeadersObject;

0 commit comments

Comments
 (0)