-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pages router test to nextjs example (#128)
* feat: add pages router example * feat: add test and rename * fix: typo --------- Co-authored-by: CahidArda <[email protected]>
- Loading branch information
Showing
3 changed files
with
45 additions
and
0 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
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,33 @@ | ||
import { NextApiRequest, NextApiResponse } from "next"; | ||
|
||
import { waitUntil } from '@vercel/functions'; | ||
import { Ratelimit } from "@upstash/ratelimit"; | ||
import { Redis } from "@upstash/redis"; | ||
|
||
const redis = new Redis({ | ||
url: process.env.UPSTASH_REDIS_REST_URL!, | ||
token: process.env.UPSTASH_REDIS_REST_TOKEN!, | ||
}) | ||
|
||
const ratelimit = new Ratelimit({ | ||
redis, | ||
limiter: Ratelimit.slidingWindow(10, "10 s"), | ||
prefix: "@upstash/ratelimit", | ||
analytics: true | ||
}); | ||
|
||
export default async function handler(_req: NextApiRequest, res: NextApiResponse) { | ||
const identifier = "pages-api"; | ||
const { success, limit, remaining, pending } = await ratelimit.limit(identifier); | ||
const response = { | ||
success: success, | ||
limit: limit, | ||
remaining: remaining | ||
} | ||
|
||
// pending is a promise for handling the analytics submission | ||
waitUntil(pending) | ||
|
||
res.status(success ? 200 : 429).json(response); | ||
|
||
} |