-
Notifications
You must be signed in to change notification settings - Fork 26.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert
api-routes-rate-limit
example to TypeScript (#38393)
Converted `api-routes-rate-limit` example to TypeScript to match Contribution docs. - Removed dead/non-standard/typo links for "Deploy You Own ▲" and "View Source" ## Documentation / Examples - [X] Make sure the linting passes by running `pnpm lint` - [X] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
- Loading branch information
Showing
8 changed files
with
87 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,3 +32,6 @@ yarn-error.log* | |
|
||
# vercel | ||
.vercel | ||
|
||
# typescript | ||
*.tsbuildinfo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/// <reference types="next" /> | ||
/// <reference types="next/image-types/global" /> | ||
|
||
// NOTE: This file should not be edited | ||
// see https://nextjs.org/docs/basic-features/typescript for more information. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 7 additions & 3 deletions
10
...s/api-routes-rate-limit/pages/api/user.js → ...s/api-routes-rate-limit/pages/api/user.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es5", | ||
"lib": ["dom", "dom.iterable", "esnext"], | ||
"allowJs": true, | ||
"skipLibCheck": true, | ||
"strict": false, | ||
"forceConsistentCasingInFileNames": true, | ||
"noEmit": true, | ||
"esModuleInterop": true, | ||
"module": "esnext", | ||
"moduleResolution": "node", | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
"jsx": "preserve", | ||
"incremental": true | ||
}, | ||
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"], | ||
"exclude": ["node_modules"] | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import type { NextApiResponse } from 'next' | ||
import LRU from 'lru-cache' | ||
|
||
type Options = { | ||
uniqueTokenPerInterval?: number | ||
interval?: number | ||
} | ||
|
||
export default function rateLimit(options?: Options) { | ||
const tokenCache = new LRU({ | ||
max: options?.uniqueTokenPerInterval || 500, | ||
ttl: options?.interval || 60000, | ||
}) | ||
|
||
return { | ||
check: (res: NextApiResponse, limit: number, token: string) => | ||
new Promise<void>((resolve, reject) => { | ||
const tokenCount = (tokenCache.get(token) as number[]) || [0] | ||
if (tokenCount[0] === 0) { | ||
tokenCache.set(token, tokenCount) | ||
} | ||
tokenCount[0] += 1 | ||
|
||
const currentUsage = tokenCount[0] | ||
const isRateLimited = currentUsage >= limit | ||
res.setHeader('X-RateLimit-Limit', limit) | ||
res.setHeader( | ||
'X-RateLimit-Remaining', | ||
isRateLimited ? 0 : limit - currentUsage | ||
) | ||
|
||
return isRateLimited ? reject() : resolve() | ||
}), | ||
} | ||
} |