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.

Refactor API routes for metatags and QR codes

Kiran K34551264dcd0
diff --git a/apps/web/app/api/metatags/route.ts b/apps/web/app/api/metatags/route.ts
index b22fb83b92..02a6bd576c 100644
--- a/apps/web/app/api/metatags/route.ts
+++ b/apps/web/app/api/metatags/route.ts
@@ -1,39 +1,51 @@
+import { DubApiError, handleAndReturnErrorResponse } from "@/lib/errors";
import { ratelimit } from "@/lib/upstash";
+import z from "@/lib/zod";
import { LOCALHOST_IP, isValidUrl } from "@dub/utils";
import { ipAddress } from "@vercel/edge";
import { getToken } from "next-auth/jwt";
import { NextRequest } from "next/server";
import { getMetaTags } from "./utils";
+const getMetaTagQuerySchema = z.object({
+ url: z.string().refine((v) => isValidUrl(v), { message: "Invalid URL" }),
+});
+
export const runtime = "edge";
export async function GET(req: NextRequest) {
- const url = req.nextUrl.searchParams.get("url");
- if (!url || !isValidUrl(url)) {
- return new Response("Invalid URL", { status: 400 });
- }
+ try {
+ const { url } = getMetaTagQuerySchema.parse({
+ url: req.nextUrl.searchParams.get("url"),
+ });
- // Rate limit if user is not logged in
- const session = await getToken({
- req,
- secret: process.env.NEXTAUTH_SECRET,
- });
- if (!session?.email) {
- const ip = ipAddress(req) || LOCALHOST_IP;
- const { success } = await ratelimit().limit(ip);
- if (!success) {
- return new Response("Don't DDoS me pls 🥺", { status: 429 });
+ // Rate limit if user is not logged in
+ const session = await getToken({
+ req,
+ secret: process.env.NEXTAUTH_SECRET,
+ });
+ if (!session?.email) {
+ const ip = ipAddress(req) || LOCALHOST_IP;
+ const { success } = await ratelimit().limit(ip);
+ if (!success) {
+ throw new DubApiError({
+ code: "rate_limit_exceeded",
+ message: "Don't DDoS me pls 🥺",
+ });
+ }
}
- }
- const metatags = await getMetaTags(url);
- return new Response(JSON.stringify(metatags), {
- status: 200,
- headers: {
- "Content-Type": "application/json",
- "Access-Control-Allow-Origin": "*",
- },
- });
+ const metatags = await getMetaTags(url);
+ return new Response(JSON.stringify(metatags), {
+ status: 200,
+ headers: {
+ "Content-Type": "application/json",
+ "Access-Control-Allow-Origin": "*",
+ },
+ });
+ } catch (error) {
+ return handleAndReturnErrorResponse(error);
+ }
}
export function OPTIONS() {
diff --git a/apps/web/app/api/qr/route.tsx b/apps/web/app/api/qr/route.tsx
index 0bd54ff51d..7024a32904 100644
--- a/apps/web/app/api/qr/route.tsx
+++ b/apps/web/app/api/qr/route.tsx
@@ -29,8 +29,8 @@ export async function GET(req: NextRequest) {
}
const params = getSearchParams(req.url);
- const parsed = getQRCodeQuerySchema.parse(params);
- const { url, size, level, fgColor, bgColor, includeMargin } = parsed;
+ const { url, size, level, fgColor, bgColor, includeMargin } =
+ getQRCodeQuerySchema.parse(params);
// const logo = req.nextUrl.searchParams.get("logo") || "https://d2vwwcvoksz7ty.cloudfront.net/logo.png";