diff --git a/app/api/cart/route.ts b/app/api/cart/route.ts index 02a225e..916e589 100644 --- a/app/api/cart/route.ts +++ b/app/api/cart/route.ts @@ -1,16 +1,10 @@ import prismadb from "@/lib/prisma"; -import { redis } from "@/lib/redis"; import { UserRole } from "@prisma/client"; import { NextResponse } from "next/server"; -import { Ratelimit } from "@upstash/ratelimit"; +import { apiRatelimit } from "@/lib/redis"; import { currentRole, currentUser } from "@/lib/auth"; import { CartItemSchema } from "@/lib/validators/cart-item"; -const cartRatelimit = new Ratelimit({ - redis, - limiter: Ratelimit.slidingWindow(5, "1 m"), -}); - export async function POST(request: Request) { try { //Check if there is a current user @@ -33,9 +27,9 @@ export async function POST(request: Request) { ); } - const { success } = await cartRatelimit.limit(user.id); + const { success } = await apiRatelimit?.limit(user.id); - if (!success) { + if (!success && process.env.VERCEL_ENV === "production") { return new NextResponse("Too Many Requests! try again in 1 min", { status: 429, }); diff --git a/lib/redis.ts b/lib/redis.ts index 6c477ff..cb355e8 100644 --- a/lib/redis.ts +++ b/lib/redis.ts @@ -5,3 +5,8 @@ export const redis = new Redis({ url: process.env.UPSTASH_REDIS_REST_URL as string, token: process.env.UPSTASH_REDIS_REST_TOKEN as string, }); + +export const apiRatelimit = new Ratelimit({ + redis, + limiter: Ratelimit.slidingWindow(5, "1 m"), +});