Skip to content

Commit

Permalink
docs: update vercel edge example to use the new api (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
chronark committed Aug 22, 2022
1 parent ee6480f commit be444e5
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 51 deletions.
9 changes: 5 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ dist
.env
redis-server

./**/yarn.lock
./**/package-lock.json
./**/pnpm-lock.yaml
**/yarn.lock
**/package-lock.json
**/pnpm-lock.yaml

*.log
.vercel
.next
.next

33 changes: 33 additions & 0 deletions examples/nextjs/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { NextFetchEvent, NextRequest, NextResponse } from "next/server";
import { Ratelimit } from "@upstash/ratelimit";
import { Redis } from "@upstash/redis";

const ratelimit = new Ratelimit({
redis: Redis.fromEnv(),
limiter: Ratelimit.fixedWindow(10, "10 s"),
});

export default async function middleware(
request: NextRequest,
event: NextFetchEvent,
): Promise<Response | undefined> {
const ip = request.ip ?? "127.0.0.1";

const { success, pending, limit, reset, remaining } = await ratelimit.limit(
`mw_${ip}`,
);
event.waitUntil(pending);

const res = success
? NextResponse.next(request)
: NextResponse.rewrite(new URL("/api/blocked", request.url), request);

res.headers.set("X-RateLimit-Limit", limit.toString());
res.headers.set("X-RateLimit-Remaining", remaining.toString());
res.headers.set("X-RateLimit-Reset", reset.toString());
return res;
}

export const config = {
matcher: "/api/hello",
};
4 changes: 2 additions & 2 deletions examples/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
},
"dependencies": {
"@upstash/ratelimit": "../../dist",
"@upstash/redis": "^1.4.0",
"next": "^12.1.6",
"@upstash/redis": "latest",
"next": "^12.2.5",
"react": "^18.1.0",
"react-dom": "^18.1.0"
},
Expand Down
44 changes: 0 additions & 44 deletions examples/nextjs/pages/api/_middleware.ts

This file was deleted.

9 changes: 9 additions & 0 deletions examples/nextjs/pages/api/blocked.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { NextApiRequest, NextApiResponse } from "next";

export default function handler(
_req: NextApiRequest,
res: NextApiResponse,
) {
res.status(429);
return res.end();
}
8 changes: 8 additions & 0 deletions examples/nextjs/pages/api/hello.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { NextApiRequest, NextApiResponse } from "next";

export default function handler(
_req: NextApiRequest,
res: NextApiResponse,
) {
return res.json({ hello: "world" });
}
5 changes: 4 additions & 1 deletion examples/nextjs/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import type { NextPage } from "next";
import { useRouter } from "next/router";
import { useEffect, useState } from "react";

const Home: NextPage = () => {
const [response, setResponse] = useState<Record<string, unknown> | null>(
null,
);

const router = useRouter();
useEffect(() => {}, []);

const generate = async () => {
const res = await fetch("/api");
const res = await fetch("/api/hello");

if (res.ok) {
setResponse({
Expand All @@ -24,6 +26,7 @@ const Home: NextPage = () => {
} else {
console.log(JSON.stringify(res.headers, null, 2));
setResponse(null);

alert(
`Ratelimit reached, try again after ${
new Date(
Expand Down

0 comments on commit be444e5

Please sign in to comment.