Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

add api limits to PLANS

Steven Tey962e9a3d28ad
diff --git a/apps/web/app/api/stripe/webhook/checkout-session-completed.ts b/apps/web/app/api/stripe/webhook/checkout-session-completed.ts
index 24ab8af4d5..6e6cd65bf6 100644
--- a/apps/web/app/api/stripe/webhook/checkout-session-completed.ts
+++ b/apps/web/app/api/stripe/webhook/checkout-session-completed.ts
@@ -1,8 +1,6 @@
-import { getAPIRateLimitForPlan } from "@/lib/api/tokens/ratelimit";
import { limiter } from "@/lib/cron/limiter";
import { prisma } from "@/lib/prisma";
import { stripe } from "@/lib/stripe";
-import { PlanProps } from "@/lib/types";
import { getPlanFromPriceId, log } from "@dub/utils";
import { sendEmail } from "emails";
import UpgradeEmail from "emails/upgrade-email";
@@ -79,7 +77,7 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {
projectId: workspaceId,
},
data: {
- rateLimit: getAPIRateLimitForPlan(planName as PlanProps),
+ rateLimit: plan.limits.api,
},
});
diff --git a/apps/web/app/api/stripe/webhook/customer-subscription-deleted.ts b/apps/web/app/api/stripe/webhook/customer-subscription-deleted.ts
index aec7354e76..2e41277aba 100644
--- a/apps/web/app/api/stripe/webhook/customer-subscription-deleted.ts
+++ b/apps/web/app/api/stripe/webhook/customer-subscription-deleted.ts
@@ -1,4 +1,3 @@
-import { getAPIRateLimitForPlan } from "@/lib/api/tokens/ratelimit";
import { prisma } from "@/lib/prisma";
import { recordLink } from "@/lib/tinybird";
import { redis } from "@/lib/upstash";
@@ -92,7 +91,7 @@ export async function customerSubscriptionDeleted(event: Stripe.Event) {
projectId: workspace.id,
},
data: {
- rateLimit: getAPIRateLimitForPlan("free"),
+ rateLimit: FREE_PLAN.limits.api,
},
}),
diff --git a/apps/web/app/api/stripe/webhook/customer-subscription-updated.ts b/apps/web/app/api/stripe/webhook/customer-subscription-updated.ts
index 15c8e7419b..b011de8904 100644
--- a/apps/web/app/api/stripe/webhook/customer-subscription-updated.ts
+++ b/apps/web/app/api/stripe/webhook/customer-subscription-updated.ts
@@ -1,6 +1,4 @@
-import { getAPIRateLimitForPlan } from "@/lib/api/tokens/ratelimit";
import { prisma } from "@/lib/prisma";
-import { PlanProps } from "@/lib/types";
import { getPlanFromPriceId, log } from "@dub/utils";
import { NextResponse } from "next/server";
import Stripe from "stripe";
@@ -62,7 +60,7 @@ export async function customerSubscriptionUpdated(event: Stripe.Event) {
projectId: workspace.id,
},
data: {
- rateLimit: getAPIRateLimitForPlan(newPlan as PlanProps),
+ rateLimit: plan.limits.api,
},
});
}
diff --git a/apps/web/app/api/tokens/route.ts b/apps/web/app/api/tokens/route.ts
index 140d448fe4..c33e38101a 100644
--- a/apps/web/app/api/tokens/route.ts
+++ b/apps/web/app/api/tokens/route.ts
@@ -1,9 +1,8 @@
-import { getAPIRateLimitForPlan } from "@/lib/api/tokens/ratelimit";
import { parseRequestBody } from "@/lib/api/utils";
import { hashToken, withWorkspace } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
import { createTokenSchema, tokenSchema } from "@/lib/zod/schemas/token";
-import { nanoid } from "@dub/utils";
+import { getCurrentPlan, nanoid } from "@dub/utils";
import { User } from "@prisma/client";
import { waitUntil } from "@vercel/functions";
import { sendEmail } from "emails";
@@ -51,7 +50,7 @@ export const POST = withWorkspace(
partialKey,
userId: isMachine ? machineUser?.id! : session.user.id,
projectId: workspace.id,
- rateLimit: getAPIRateLimitForPlan(workspace.plan),
+ rateLimit: getCurrentPlan(workspace.plan).limits.api,
scopes:
scopes && scopes.length > 0 ? [...new Set(scopes)].join(" ") : null,
},
diff --git a/apps/web/lib/api/tokens/ratelimit.ts b/apps/web/lib/api/tokens/ratelimit.ts
deleted file mode 100644
index 8ce69af105..0000000000
--- a/apps/web/lib/api/tokens/ratelimit.ts
+++ /dev/null
@@ -1,20 +0,0 @@
-import { PlanProps } from "@/lib/types";
-
-// Get API rate limit for a given plan in requests per minute
-export const getAPIRateLimitForPlan = (plan: PlanProps) => {
- switch (plan) {
- case "free":
- return 600;
- case "pro":
- return 3000;
- case "business":
- case "business plus":
- case "business extra":
- case "business max":
- return 6000;
- // case "enterprise":
- // return ;
- }
-
- return 600;
-};
diff --git a/packages/utils/src/constants/pricing.tsx b/packages/utils/src/constants/pricing.tsx
index 5a144e8aa6..7183f180f1 100644
--- a/packages/utils/src/constants/pricing.tsx
+++ b/packages/utils/src/constants/pricing.tsx
@@ -36,6 +36,7 @@ const BUSINESS_PLAN_MODIFIER = ({
tags,
users,
ai: 1000,
+ api: 6000,
},
colors: {
bg: "bg-sky-900",
@@ -101,6 +102,7 @@ export const PLANS = [
tags: 5,
users: 1,
ai: 10,
+ api: 600,
},
colors: {
bg: "bg-black",
@@ -158,6 +160,7 @@ export const PLANS = [
tags: 25,
users: 5,
ai: 1000,
+ api: 3000,
},
colors: {
bg: "bg-blue-500",
@@ -339,6 +342,12 @@ export const getPlanDetails = (plan: string) => {
)!;
};
+export const getCurrentPlan = (plan: string) => {
+ return (
+ PLANS.find((p) => p.name.toLowerCase() === plan.toLowerCase()) || FREE_PLAN
+ );
+};
+
export const getNextPlan = (plan?: string | null) => {
if (!plan) return PRO_PLAN;
return PLANS[