Skip to content

Commit

Permalink
feat: run examples as an external test
Browse files Browse the repository at this point in the history
  • Loading branch information
gamemaker1 committed Aug 27, 2023
1 parent 49489fe commit 9e6a84b
Show file tree
Hide file tree
Showing 9 changed files with 1,269 additions and 324 deletions.
22 changes: 21 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,29 @@ jobs:
run: |
npm ci
npm run test:lib
test-examples:
name: Test (Examples)
strategy:
matrix:
node-version: [lts/*]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout the repository
uses: actions/checkout@v3
- name: Use Node ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: Run tests
run: |
npm ci
cd examples/
npm ci
npm test
publish:
name: Publish
needs: [lint, test-library]
needs: [lint, test-library, test-examples]
if: startsWith(github.ref, 'refs/tags/v')
runs-on: ubuntu-latest
steps:
Expand Down
6 changes: 6 additions & 0 deletions examples/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# .npmrc
# Configuration for npm and pnpm

# Uses the exact version instead of any within-patch-range version of an
# installed package
save-exact=true
37 changes: 0 additions & 37 deletions examples/combined-fetch.js

This file was deleted.

48 changes: 48 additions & 0 deletions examples/draft-7-fetch.example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// /examples/draft-7-fetch.example.ts
// Use `fetch`, and parse the `RateLimit` header from the IETF spec's 7th draft.

// Note that example has a server and client together - normally they'd be in
// separate files, likely on separate devices.

// ---
// `server.ts`
// ----

import { default as express } from 'express'
import { rateLimit } from 'express-rate-limit'

// Create a rate-limited server.
const app = express()
app.use(
rateLimit({
max: 5,
windowMs: 60 * 1000, // 1 minute windows.
legacyHeader: false, // Disable the `X-RateLimit-*` headers.
standardHeaders: 'draft-7', // Use the combined `RateLimit` header.
}),
)

// Register routes, and start the server.
app.get('/', (req, res) => res.send('Hallo there!'))
const { port, server } = await new Promise((resolve) => {
const server = app.listen(0, () =>
resolve({ port: server.address().port, server }),
)
})

// ---
// `client.ts`
// ---

import { parseRateLimit } from 'ratelimit-header-parser'

// Fetch a response from the server.
const response = await fetch(`http://localhost:${port}`)

console.log('`RateLimit` header content:', response.headers.get('RateLimit'))
// > `RateLimit` header content: limit=5, remaining=4, reset=60
console.log('parsed rate limit info:', parseRateLimit(response))
// > parsed rate limit info: { limit: 5, used: 1, remaining: 4, reset: 2023-08-25T04:41:31.546Z }

// Cleanup the server.
server.close()

This comment has been minimized.

Copy link
@nfriedly

nfriedly Aug 28, 2023

Member

I bet we could move this to the app.get('/', ...) handler, possibly inside a process.nextTick(). Then it wouldn't look like it was part of the client.ts half

Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// /examples/github-fetch.example.ts
// Uses `fetch` to hit the Github API.

import { parseRateLimit } from 'ratelimit-header-parser'

const response = await fetch(
Expand Down
Loading

0 comments on commit 9e6a84b

Please sign in to comment.