Skip to content

Commit

Permalink
test: add more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
gamemaker1 committed Sep 19, 2023
1 parent 6c2b2d0 commit e66d904
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 9 deletions.
11 changes: 11 additions & 0 deletions examples/node/axios.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// /examples/node/axios.ts
// Uses `axios` to hit the GitHub API.

import axios from 'axios'
import { getRateLimit } from 'ratelimit-header-parser'

// Make a GET request to the GitHub API.
const response = await axios('https://api.github.com/orgs/express-rate-limit')
console.log('github ratelimit:', getRateLimit(response))

// > github ratelimit: { limit: 60, used: 1, remaining: 59, reset: 2023-08-25T04:16:48.000Z }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// /examples/draft-7-fetch.example.ts
// /examples/node/draft-7-fetch.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
Expand Down
14 changes: 14 additions & 0 deletions examples/node/http-get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// /examples/node/http-get.ts
// Uses `http.get` to hit the Imgur API.

import https from 'node:https'
import { getRateLimit } from 'ratelimit-header-parser'

// Make a GET request to the Imgur API.
https.get('https://api.imgur.com/post/v1/posts/t/aww', (response) => {
console.log('imgur ratelimit:', getRateLimit(response))

return response.resume()
})

// > imgur ratelimit: { limit: 500, used: 1, remaining: 499, reset: 2023-09-19T14:19:30.297Z }
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// /examples/github-fetch.example.ts
// Uses `fetch` to hit the Github API.
// /examples/node/native-fetch.ts
// Uses `fetch` to hit the GitHub API.

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

const response = await fetch(
'https://api.github.com/repos/express-rate-limit/express-rate-limit/contributors?anon=1',
)

// Make a GET request to the GitHub API.
const response = await fetch('https://api.github.com/orgs/express-rate-limit')
console.log('github ratelimit:', getRateLimit(response))

// > github ratelimit: { limit: 60, used: 1, remaining: 59, reset: 2023-08-25T04:16:48.000Z }
73 changes: 73 additions & 0 deletions examples/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
"private": true,
"type": "module",
"scripts": {
"test": "tsx scripts/run-examples.ts"
"test": "tsx run-examples.ts"
},
"dependencies": {
"axios": "1.5.0",
"express": "4.18.2",
"express-rate-limit": "6.9.0",
"ratelimit-header-parser": "../"
Expand Down
2 changes: 1 addition & 1 deletion examples/scripts/run-examples.ts → examples/run-examples.ts
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ console.log(chalk.bold.inverse.green(' examples '))
console.log()

// Collect all the examples.
const files = await globby('*.example.ts')
const files = await globby('node/*.ts', 'deno/*.ts', 'browser/*.ts')
const tasks = []
for (const file of files) {
tasks.push(
Expand Down

0 comments on commit e66d904

Please sign in to comment.