Skip to content

Commit 9e6a84b

Browse files
committed
feat: run examples as an external test
1 parent 49489fe commit 9e6a84b

File tree

9 files changed

+1269
-324
lines changed

9 files changed

+1269
-324
lines changed

.github/workflows/ci.yaml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,29 @@ jobs:
4242
run: |
4343
npm ci
4444
npm run test:lib
45+
test-examples:
46+
name: Test (Examples)
47+
strategy:
48+
matrix:
49+
node-version: [lts/*]
50+
os: [ubuntu-latest]
51+
runs-on: ${{ matrix.os }}
52+
steps:
53+
- name: Checkout the repository
54+
uses: actions/checkout@v3
55+
- name: Use Node ${{ matrix.node-version }}
56+
uses: actions/setup-node@v3
57+
with:
58+
node-version: ${{ matrix.node-version }}
59+
- name: Run tests
60+
run: |
61+
npm ci
62+
cd examples/
63+
npm ci
64+
npm test
4565
publish:
4666
name: Publish
47-
needs: [lint, test-library]
67+
needs: [lint, test-library, test-examples]
4868
if: startsWith(github.ref, 'refs/tags/v')
4969
runs-on: ubuntu-latest
5070
steps:

examples/.npmrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# .npmrc
2+
# Configuration for npm and pnpm
3+
4+
# Uses the exact version instead of any within-patch-range version of an
5+
# installed package
6+
save-exact=true

examples/combined-fetch.js

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

examples/draft-7-fetch.example.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// /examples/draft-7-fetch.example.ts
2+
// Use `fetch`, and parse the `RateLimit` header from the IETF spec's 7th 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+
max: 5,
19+
windowMs: 60 * 1000, // 1 minute windows.
20+
legacyHeader: false, // Disable the `X-RateLimit-*` headers.
21+
standardHeaders: 'draft-7', // Use the combined `RateLimit` header.
22+
}),
23+
)
24+
25+
// Register routes, and start the server.
26+
app.get('/', (req, res) => res.send('Hallo there!'))
27+
const { port, server } = await new Promise((resolve) => {
28+
const server = app.listen(0, () =>
29+
resolve({ port: server.address().port, server }),
30+
)
31+
})
32+
33+
// ---
34+
// `client.ts`
35+
// ---
36+
37+
import { parseRateLimit } from 'ratelimit-header-parser'
38+
39+
// Fetch a response from the server.
40+
const response = await fetch(`http://localhost:${port}`)
41+
42+
console.log('`RateLimit` header content:', response.headers.get('RateLimit'))
43+
// > `RateLimit` header content: limit=5, remaining=4, reset=60
44+
console.log('parsed rate limit info:', parseRateLimit(response))
45+
// > parsed rate limit info: { limit: 5, used: 1, remaining: 4, reset: 2023-08-25T04:41:31.546Z }
46+
47+
// Cleanup the server.
48+
server.close()

examples/github-fetch.mjs renamed to examples/github-fetch.example.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// /examples/github-fetch.example.ts
2+
// Uses `fetch` to hit the Github API.
3+
14
import { parseRateLimit } from 'ratelimit-header-parser'
25

36
const response = await fetch(

0 commit comments

Comments
 (0)