Skip to content

Commit 9a26bf0

Browse files
committed
stash: everything
1 parent ae3dae1 commit 9a26bf0

26 files changed

+1625
-1242
lines changed

examples/deno/fetch.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import { getRateLimit } from '../../dist/index.mjs'
55

66
// Make a GET request to the Imgur API.
77
const response = await fetch('https://api.imgur.com/post/v1/posts/t/aww')
8-
console.log('imgur ratelimit:', getRateLimit(response))
8+
console.log(
9+
'imgur ratelimit:',
10+
JSON.stringify(getRateLimit(response), undefined, 2),
11+
)
912

1013
// > imgur ratelimit: { limit: 500, used: 1, remaining: 499, reset: 2023-08-25T04:16:48.000Z }

examples/node/axios.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import { getRateLimit } from 'ratelimit-header-parser'
66

77
// Make a GET request to the GitHub API.
88
const response = await axios('https://api.github.com/orgs/express-rate-limit')
9-
console.log('github ratelimit:', getRateLimit(response))
9+
console.log(
10+
'github ratelimit:',
11+
JSON.stringify(getRateLimit(response), undefined, 2),
12+
)
1013

1114
// > github ratelimit: { limit: 60, used: 1, remaining: 59, reset: 2023-08-25T04:16:48.000Z }

examples/node/draft-7-fetch.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ import { rateLimit } from 'express-rate-limit'
1515
const app = express()
1616
app.use(
1717
rateLimit({
18-
max: 5,
18+
limit: 5,
1919
windowMs: 60 * 1000, // 1 minute windows.
20-
legacyHeader: false, // Disable the `X-RateLimit-*` headers.
20+
legacyHeaders: false, // Disable the `X-RateLimit-*` headers.
2121
standardHeaders: 'draft-7', // Use the combined `RateLimit` header.
2222
}),
2323
)
@@ -40,8 +40,15 @@ import { getRateLimit } from 'ratelimit-header-parser'
4040
const response = await fetch(`http://localhost:${port}`)
4141

4242
console.log('`RateLimit` header content:', response.headers.get('RateLimit'))
43+
console.log(
44+
'`RateLimit-Policy` header content:',
45+
response.headers.get('RateLimit-Policy'),
46+
)
4347
// > `RateLimit` header content: limit=5, remaining=4, reset=60
44-
console.log('parsed rate limit info:', getRateLimit(response))
48+
console.log(
49+
'parsed rate limit info:',
50+
JSON.stringify(getRateLimit(response), undefined, 2),
51+
)
4552
// > parsed rate limit info: { limit: 5, used: 1, remaining: 4, reset: 2023-08-25T04:41:31.546Z }
4653

4754
// Cleanup the server.

examples/node/draft-8-fetch.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// /examples/node/draft-88-fetch.ts
2+
// Use `fetch`, and parse the `RateLimit` header from the IETF spec's 8th draft.
3+
4+
// Note that example has a server and client together - normally they'd be in
5+
// separate files, likely on separate devices.
6+
7+
// ---
8+
// `server.ts`
9+
// ----
10+
11+
import { default as express } from 'express'
12+
import { rateLimit } from 'express-rate-limit'
13+
14+
// Create a rate-limited server.
15+
const app = express()
16+
app.use(
17+
rateLimit({
18+
limit: 8,
19+
windowMs: 2 * 60 * 1000, // 2 minute windows.
20+
legacyHeaders: false, // Disable the `X-RateLimit-*` headers.
21+
standardHeaders: 'draft-8', // Use the combined `RateLimit` header.
22+
}),
23+
rateLimit({
24+
limit: 45,
25+
windowMs: 5 * 60 * 60 * 1000, // 2 hour windows.
26+
legacyHeaders: false, // Disable the `X-RateLimit-*` headers.
27+
standardHeaders: 'draft-8', // Use the combined `RateLimit` header.
28+
}),
29+
)
30+
31+
// Register routes, and start the server.
32+
app.get('/', (req, res) => res.send('Hallo there!'))
33+
const { port, server } = await new Promise((resolve) => {
34+
const server = app.listen(0, () =>
35+
resolve({ port: server.address().port, server }),
36+
)
37+
})
38+
39+
// ---
40+
// `client.ts`
41+
// ---
42+
43+
import { getRateLimits } from 'ratelimit-header-parser'
44+
45+
// Fetch a response from the server.
46+
const response = await fetch(`http://localhost:${port}`)
47+
48+
console.log('`RateLimit` header content:', response.headers.get('RateLimit'))
49+
console.log(
50+
'`RateLimit-Policy` header content:',
51+
response.headers.get('RateLimit-Policy'),
52+
)
53+
// > `RateLimit` header content: limit=5, remaining=4, reset=60
54+
console.log(
55+
'parsed rate limit info:',
56+
JSON.stringify(getRateLimits(response), undefined, 2),
57+
)
58+
// > parsed rate limit info: { limit: 5, used: 1, remaining: 4, reset: 2023-08-25T04:41:31.546Z }
59+
60+
// Cleanup the server.
61+
server.close()

examples/node/http-get.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import { getRateLimits } from 'ratelimit-header-parser'
66

77
// Make a GET request to the Imgur API.
88
https.get('https://api.imgur.com/post/v1/posts/t/aww', (response) => {
9-
console.log('imgur ratelimit:', getRateLimits(response))
9+
console.log(
10+
'imgur ratelimit:',
11+
JSON.stringify(getRateLimits(response), undefined, 2),
12+
)
1013
return response.resume()
1114
})
1215

examples/node/native-fetch.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import { getRateLimit } from 'ratelimit-header-parser'
55

66
// Make a GET request to the GitHub API.
77
const response = await fetch('https://api.github.com/orgs/express-rate-limit')
8-
console.log('github ratelimit:', getRateLimit(response))
8+
console.log(
9+
'github ratelimit:',
10+
JSON.stringify(getRateLimit(response), undefined, 2),
11+
)
912

1013
// > github ratelimit: { limit: 60, used: 1, remaining: 59, reset: 2023-08-25T04:16:48.000Z }

examples/node/node-fetch.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import { getRateLimit } from 'ratelimit-header-parser'
66

77
// Make a GET request to the GitHub API.
88
const response = await fetch('https://api.github.com/orgs/express-rate-limit')
9-
console.log('github ratelimit:', getRateLimit(response))
9+
console.log(
10+
'github ratelimit:',
11+
JSON.stringify(getRateLimit(response), undefined, 2),
12+
)
1013

1114
// > github ratelimit: { limit: 60, used: 1, remaining: 59, reset: 2023-08-25T04:16:48.000Z }

0 commit comments

Comments
 (0)