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.

Links

Kiran K0552fd33475e
diff --git a/apps/web/app/api/links/[linkId]/archive/route.ts b/apps/web/app/api/links/[linkId]/archive/route.ts
index d62997097b..d4d136670a 100644
--- a/apps/web/app/api/links/[linkId]/archive/route.ts
+++ b/apps/web/app/api/links/[linkId]/archive/route.ts
@@ -1,24 +1,15 @@
import { archiveLink } from "@/lib/api/links";
import { withAuth } from "@/lib/auth";
-import { handleAndReturnErrorResponse } from "@/lib/api/errors";
import { NextResponse } from "next/server";
// POST /api/links/[linkId]/archive – archive a link
export const POST = withAuth(async ({ headers, link }) => {
- try {
- const response = await archiveLink({ linkId: link!.id, archived: true });
- return NextResponse.json(response, { headers });
- } catch (error) {
- return handleAndReturnErrorResponse(error);
- }
+ const response = await archiveLink({ linkId: link!.id, archived: true });
+ return NextResponse.json(response, { headers });
});
// DELETE /api/links/[linkId]/archive – unarchive a link
export const DELETE = withAuth(async ({ headers, link }) => {
- try {
- const response = await archiveLink({ linkId: link!.id, archived: false });
- return NextResponse.json(response, { headers });
- } catch (error) {
- return handleAndReturnErrorResponse(error);
- }
+ const response = await archiveLink({ linkId: link!.id, archived: false });
+ return NextResponse.json(response, { headers });
});
diff --git a/apps/web/app/api/links/[linkId]/route.ts b/apps/web/app/api/links/[linkId]/route.ts
index ed23d105f6..39ff7e81a9 100644
--- a/apps/web/app/api/links/[linkId]/route.ts
+++ b/apps/web/app/api/links/[linkId]/route.ts
@@ -1,11 +1,7 @@
import { deleteLink, editLink, processLink } from "@/lib/api/links";
import { withAuth } from "@/lib/auth";
import { qstash } from "@/lib/cron";
-import {
- DubApiError,
- ErrorCodes,
- handleAndReturnErrorResponse,
-} from "@/lib/api/errors";
+import { DubApiError, ErrorCodes } from "@/lib/api/errors";
import { LinkWithTagIdsProps } from "@/lib/types";
import { updateLinkBodySchema } from "@/lib/zod/schemas/links";
import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
@@ -21,79 +17,71 @@ export const GET = withAuth(async ({ headers, link }) => {
// PUT /api/links/[linkId] – update a link
export const PUT = withAuth(async ({ req, headers, project, link }) => {
- try {
- const body = updateLinkBodySchema.parse(await req.json());
+ const body = updateLinkBodySchema.parse(await req.json());
- const updatedLink = {
- ...link,
- ...body,
- };
+ const updatedLink = {
+ ...link,
+ ...body,
+ };
- if (updatedLink.projectId !== link?.projectId) {
- throw new DubApiError({
- code: "forbidden",
- message: "Transferring links to another project is not yet supported.",
- });
- }
-
- const {
- link: processedLink,
- error,
- code,
- } = await processLink({
- payload: updatedLink as LinkWithTagIdsProps,
- project,
+ if (updatedLink.projectId !== link?.projectId) {
+ throw new DubApiError({
+ code: "forbidden",
+ message: "Transferring links to another project is not yet supported.",
});
+ }
- if (error) {
- throw new DubApiError({
- code: code as ErrorCodes,
- message: error,
- });
- }
+ const {
+ link: processedLink,
+ error,
+ code,
+ } = await processLink({
+ payload: updatedLink as LinkWithTagIdsProps,
+ project,
+ });
- const [response, _] = await Promise.allSettled([
- editLink({
- // link is guaranteed to exist because if not we will return 404
- domain: link!.domain,
- key: link!.key,
- updatedLink: processedLink as any, // TODO: fix types
- }),
- qstash.publishJSON({
- url: `${APP_DOMAIN_WITH_NGROK}/api/cron/links/event`,
- body: {
- linkId: link!.id,
- type: "edit",
- },
- }),
- // @ts-ignore
- ]).then((results) => results.map((result) => result.value));
+ if (error) {
+ throw new DubApiError({
+ code: code as ErrorCodes,
+ message: error,
+ });
+ }
- if (response === null) {
- throw new DubApiError({
- code: "conflict",
- message: "Duplicate key: This short link already exists.",
- });
- }
+ const [response, _] = await Promise.allSettled([
+ editLink({
+ // link is guaranteed to exist because if not we will return 404
+ domain: link!.domain,
+ key: link!.key,
+ updatedLink: processedLink as any, // TODO: fix types
+ }),
+ qstash.publishJSON({
+ url: `${APP_DOMAIN_WITH_NGROK}/api/cron/links/event`,
+ body: {
+ linkId: link!.id,
+ type: "edit",
+ },
+ }),
+ // @ts-ignore
+ ]).then((results) => results.map((result) => result.value));
- return NextResponse.json(response, {
- headers,
+ if (response === null) {
+ throw new DubApiError({
+ code: "conflict",
+ message: "Duplicate key: This short link already exists.",
});
- } catch (error) {
- return handleAndReturnErrorResponse(error, headers);
}
+
+ return NextResponse.json(response, {
+ headers,
+ });
});
// DELETE /api/links/[linkId] – delete a link
export const DELETE = withAuth(async ({ headers, link }) => {
- try {
- // link is guaranteed to exist because if not we will return 404
- const response = await deleteLink(link!);
+ // link is guaranteed to exist because if not we will return 404
+ const response = await deleteLink(link!);
- return NextResponse.json(response[0], {
- headers,
- });
- } catch (error) {
- return handleAndReturnErrorResponse(error);
- }
+ return NextResponse.json(response[0], {
+ headers,
+ });
});
diff --git a/apps/web/app/api/links/[linkId]/transfer/route.ts b/apps/web/app/api/links/[linkId]/transfer/route.ts
index b3840bf1cf..1622726686 100644
--- a/apps/web/app/api/links/[linkId]/transfer/route.ts
+++ b/apps/web/app/api/links/[linkId]/transfer/route.ts
@@ -1,7 +1,7 @@
import { transferLink } from "@/lib/api/links";
import { withAuth } from "@/lib/auth";
import { qstash } from "@/lib/cron";
-import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";
+import { DubApiError } from "@/lib/api/errors";
import prisma from "@/lib/prisma";
import { redis } from "@/lib/upstash";
import z from "@/lib/zod";
@@ -14,60 +14,56 @@ const transferLinkBodySchema = z.object({
// POST /api/links/[linkId]/transfer – transfer a link to another project
export const POST = withAuth(async ({ req, headers, session, link }) => {
- try {
- const { newProjectId } = transferLinkBodySchema.parse(await req.json());
+ const { newProjectId } = transferLinkBodySchema.parse(await req.json());
- const newProject = await prisma.project.findUnique({
- where: { id: newProjectId },
- select: {
- linksUsage: true,
- linksLimit: true,
- users: {
- where: {
- userId: session.user.id,
- },
- select: {
- role: true,
- },
+ const newProject = await prisma.project.findUnique({
+ where: { id: newProjectId },
+ select: {
+ linksUsage: true,
+ linksLimit: true,
+ users: {
+ where: {
+ userId: session.user.id,
+ },
+ select: {
+ role: true,
},
},
- });
-
- if (!newProject || newProject.users.length === 0) {
- throw new DubApiError({
- code: "not_found",
- message: "New project not found.",
- });
- }
-
- if (newProject.linksUsage >= newProject.linksLimit) {
- throw new DubApiError({
- code: "forbidden",
- message: "New project has reached its link limit.",
- });
- }
-
- const response = await Promise.all([
- transferLink({
- linkId: link!.id,
- newProjectId,
- }),
- // set this in redis so we can use it in the event cron job
- redis.set(`transfer:${link!.id}:oldProjectId`, link!.projectId),
- ]);
+ },
+ });
- await qstash.publishJSON({
- url: `${APP_DOMAIN_WITH_NGROK}/api/cron/links/event`,
- body: {
- linkId: link!.id,
- type: "transfer",
- },
+ if (!newProject || newProject.users.length === 0) {
+ throw new DubApiError({
+ code: "not_found",
+ message: "New project not found.",
});
+ }
- return NextResponse.json(response, {
- headers,
+ if (newProject.linksUsage >= newProject.linksLimit) {
+ throw new DubApiError({
+ code: "forbidden",
+ message: "New project has reached its link limit.",
});
- } catch (error) {
- return handleAndReturnErrorResponse(error, headers);
}
+
+ const response = await Promise.all([
+ transferLink({
+ linkId: link!.id,
+ newProjectId,
+ }),
+ // set this in redis so we can use it in the event cron job
+ redis.set(`transfer:${link!.id}:oldProjectId`, link!.projectId),
+ ]);
+
+ await qstash.publishJSON({
+ url: `${APP_DOMAIN_WITH_NGROK}/api/cron/links/event`,
+ body: {
+ linkId: link!.id,
+ type: "transfer",
+ },
+ });
+
+ return NextResponse.json(response, {
+ headers,
+ });
});
diff --git a/apps/web/app/api/links/bulk/route.ts b/apps/web/app/api/links/bulk/route.ts
index ff10cc8b5b..e311b774fe 100644
--- a/apps/web/app/api/links/bulk/route.ts
+++ b/apps/web/app/api/links/bulk/route.ts
@@ -1,7 +1,7 @@
import { exceededLimitError } from "@/lib/api/errors";
import { bulkCreateLinks, processLink } from "@/lib/api/links";
import { withAuth } from "@/lib/auth";
-import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";
+import { DubApiError } from "@/lib/api/errors";
import { LinkWithTagIdsProps } from "@/lib/types";
import { bulkCreateLinksBodySchema } from "@/lib/zod/schemas/links";
import { NextResponse } from "next/server";
@@ -9,61 +9,55 @@ import { NextResponse } from "next/server";
// POST /api/links/bulk – bulk create up to 100 links
export const POST = withAuth(
async ({ req, headers, session, project }) => {
- try {
- if (!project) {
- throw new DubApiError({
- code: "bad_request",
- message:
- "Missing project. Bulk link creation is only available for custom domain projects.",
- });
- }
- const links = bulkCreateLinksBodySchema.parse(await req.json());
- if (
- project.linksUsage + links.length > project.linksLimit &&
- (project.plan === "free" || project.plan === "pro")
- ) {
- throw new DubApiError({
- code: "exceeded_limit",
- message: exceededLimitError({
- plan: project.plan,
- limit: project.linksLimit,
- type: "links",
- }),
- });
- }
+ if (!project) {
+ throw new DubApiError({
+ code: "bad_request",
+ message:
+ "Missing project. Bulk link creation is only available for custom domain projects.",
+ });
+ }
+ const links = bulkCreateLinksBodySchema.parse(await req.json());
+ if (
+ project.linksUsage + links.length > project.linksLimit &&
+ (project.plan === "free" || project.plan === "pro")
+ ) {
+ throw new DubApiError({
+ code: "exceeded_limit",
+ message: exceededLimitError({
+ plan: project.plan,
+ limit: project.linksLimit,
+ type: "links",
+ }),
+ });
+ }
- const processedLinks = await Promise.all(
- links.map(async (link) =>
- processLink({
- payload: link as LinkWithTagIdsProps,
- project,
- userId: session.user.id,
- bulk: true,
- }),
- ),
- );
+ const processedLinks = await Promise.all(
+ links.map(async (link) =>
+ processLink({
+ payload: link as LinkWithTagIdsProps,
+ project,
+ userId: session.user.id,
+ bulk: true,
+ }),
+ ),
+ );
- const validLinks = processedLinks
- .filter(({ error }) => !error)
- .map(({ link }) => link);
+ const validLinks = processedLinks
+ .filter(({ error }) => !error)
+ .map(({ link }) => link);
- const errors = processedLinks
- .filter(({ error }) => error)
- .map(({ link, error, code }) => ({
- link,
- error,
- code,
- }));
+ const errors = processedLinks
+ .filter(({ error }) => error)
+ .map(({ link, error, code }) => ({
+ link,
+ error,
+ code,
+ }));
- const validLinksResponse =
- validLinks.length > 0
- ? await bulkCreateLinks({ links: validLinks })
- : [];
+ const validLinksResponse =
+ validLinks.length > 0 ? await bulkCreateLinks({ links: validLinks }) : [];
- return NextResponse.json([...validLinksResponse, ...errors], { headers });
- } catch (error) {
- return handleAndReturnErrorResponse(error);
- }
+ return NextResponse.json([...validLinksResponse, ...errors], { headers });
},
{
needNotExceededLinks: true,
diff --git a/apps/web/app/api/links/count/route.ts b/apps/web/app/api/links/count/route.ts
index 024bac52da..8bead10a38 100644
--- a/apps/web/app/api/links/count/route.ts
+++ b/apps/web/app/api/links/count/route.ts
@@ -1,24 +1,19 @@
import { getLinksCount } from "@/lib/api/links";
import { withAuth } from "@/lib/auth";
-import { handleAndReturnErrorResponse } from "@/lib/api/errors";
import { getLinksCountQuerySchema } from "@/lib/zod/schemas/links";
import { NextResponse } from "next/server";
// GET /api/links/count – get the number of links for a project
export const GET = withAuth(async ({ headers, searchParams, project }) => {
- try {
- const { userId, ...params } = getLinksCountQuerySchema.parse(searchParams);
+ const { userId, ...params } = getLinksCountQuerySchema.parse(searchParams);
- const count = await getLinksCount({
- searchParams: params,
- projectId: project.id,
- userId,
- });
+ const count = await getLinksCount({
+ searchParams: params,
+ projectId: project.id,
+ userId,
+ });
- return NextResponse.json(count, {
- headers,
- });
- } catch (error) {
- return handleAndReturnErrorResponse(error, headers);
- }
+ return NextResponse.json(count, {
+ headers,
+ });
});
diff --git a/apps/web/app/api/links/exists/route.ts b/apps/web/app/api/links/exists/route.ts
index 537c89f6d7..ba3fd31b38 100644
--- a/apps/web/app/api/links/exists/route.ts
+++ b/apps/web/app/api/links/exists/route.ts
@@ -1,21 +1,16 @@
import { checkIfKeyExists } from "@/lib/api/links";
import { withAuth } from "@/lib/auth";
-import { handleAndReturnErrorResponse } from "@/lib/api/errors";
import { domainKeySchema } from "@/lib/zod";
import { NextResponse } from "next/server";
// GET /api/links/exists – check if a link exists
export const GET = withAuth(
async ({ headers, searchParams }) => {
- try {
- const { domain, key } = domainKeySchema.parse(searchParams);
- const response = await checkIfKeyExists(domain, key);
- return NextResponse.json(response, {
- headers,
- });
- } catch (error) {
- return handleAndReturnErrorResponse(error, headers);
- }
+ const { domain, key } = domainKeySchema.parse(searchParams);
+ const response = await checkIfKeyExists(domain, key);
+ return NextResponse.json(response, {
+ headers,
+ });
},
{ skipLinkChecks: true },
);
diff --git a/apps/web/app/api/links/info/route.ts b/apps/web/app/api/links/info/route.ts
index c5090456c1..cc5b0bd02b 100644
--- a/apps/web/app/api/links/info/route.ts
+++ b/apps/web/app/api/links/info/route.ts
@@ -1,5 +1,5 @@
import { withAuth } from "@/lib/auth";
-import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";
+import { DubApiError } from "@/lib/api/errors";
import prisma from "@/lib/prisma";
import { getLinkInfoQuerySchema } from "@/lib/zod/schemas/links";
import { linkConstructor } from "@dub/utils";
@@ -7,41 +7,37 @@ import { NextResponse } from "next/server";
// GET /api/links/info – get the info for a link
export const GET = withAuth(async ({ headers, searchParams }) => {
- try {
- const { domain, key } = getLinkInfoQuerySchema.parse(searchParams);
+ const { domain, key } = getLinkInfoQuerySchema.parse(searchParams);
- const response = await prisma.link.findUnique({
- where: {
- domain_key: {
- domain,
- key,
- },
+ const response = await prisma.link.findUnique({
+ where: {
+ domain_key: {
+ domain,
+ key,
},
- include: {
- user: true,
- },
- });
+ },
+ include: {
+ user: true,
+ },
+ });
- if (!response) {
- throw new DubApiError({
- code: "not_found",
- message: "Link not found.",
- });
- }
-
- return NextResponse.json(
- {
- ...response,
- shortLink: linkConstructor({
- domain: response.domain,
- key: response.key,
- }),
- },
- {
- headers,
- },
- );
- } catch (error) {
- return handleAndReturnErrorResponse(error, headers);
+ if (!response) {
+ throw new DubApiError({
+ code: "not_found",
+ message: "Link not found.",
+ });
}
+
+ return NextResponse.json(
+ {
+ ...response,
+ shortLink: linkConstructor({
+ domain: response.domain,
+ key: response.key,
+ }),
+ },
+ {
+ headers,
+ },
+ );
});
diff --git a/apps/web/app/api/links/random/route.ts b/apps/web/app/api/links/random/route.ts
index f7eba1c995..82b820e258 100644
--- a/apps/web/app/api/links/random/route.ts
+++ b/apps/web/app/api/links/random/route.ts
@@ -1,20 +1,13 @@
import { getRandomKey } from "@/lib/api/links";
import { withAuth } from "@/lib/auth";
-import { handleAndReturnErrorResponse } from "@/lib/api/errors";
import { domainKeySchema } from "@/lib/zod";
import { NextResponse } from "next/server";
// GET /api/links/random – get a random available link key
export const GET = withAuth(async ({ headers, searchParams }) => {
- try {
- const { domain } = domainKeySchema
- .pick({ domain: true })
- .parse(searchParams);
- const response = await getRandomKey(domain);
- return NextResponse.json(response, {
- headers,
- });
- } catch (error) {
- return handleAndReturnErrorResponse(error);
- }
+ const { domain } = domainKeySchema.pick({ domain: true }).parse(searchParams);
+ const response = await getRandomKey(domain);
+ return NextResponse.json(response, {
+ headers,
+ });
});
diff --git a/apps/web/app/api/links/route.ts b/apps/web/app/api/links/route.ts
index 54f8104d2c..79bf7cd3af 100644
--- a/apps/web/app/api/links/route.ts
+++ b/apps/web/app/api/links/route.ts
@@ -1,11 +1,7 @@
import { addLink, getLinksForProject, processLink } from "@/lib/api/links";
import { withAuth } from "@/lib/auth";
import { qstash } from "@/lib/cron";
-import {
- DubApiError,
- ErrorCodes,
- handleAndReturnErrorResponse,
-} from "@/lib/api/errors";
+import { DubApiError, ErrorCodes } from "@/lib/api/errors";
import { LinkWithTagIdsProps } from "@/lib/types";
import { ratelimit } from "@/lib/upstash";
import {
@@ -17,90 +13,74 @@ import { NextResponse } from "next/server";
// GET /api/links – get all user links
export const GET = withAuth(async ({ headers, searchParams, project }) => {
- try {
- const {
- domain,
- tagId,
- search,
- sort,
- page,
- userId,
- showArchived,
- withTags,
- } = getLinksQuerySchema.parse(searchParams);
+ const { domain, tagId, search, sort, page, userId, showArchived, withTags } =
+ getLinksQuerySchema.parse(searchParams);
- const response = await getLinksForProject({
- projectId: project.id,
- domain,
- tagId,
- search,
- sort,
- page,
- userId,
- showArchived,
- withTags,
- });
- return NextResponse.json(response, {
- headers,
- });
- } catch (error) {
- return handleAndReturnErrorResponse(error);
- }
+ const response = await getLinksForProject({
+ projectId: project.id,
+ domain,
+ tagId,
+ search,
+ sort,
+ page,
+ userId,
+ showArchived,
+ withTags,
+ });
+ return NextResponse.json(response, {
+ headers,
+ });
});
// POST /api/links – create a new link
export const POST = withAuth(
async ({ req, headers, session, project }) => {
- try {
- const body = createLinkBodySchema.parse(await req.json());
-
- if (!session) {
- const ip = req.headers.get("x-forwarded-for") || LOCALHOST_IP;
- const { success } = await ratelimit(10, "1 d").limit(ip);
-
- if (!success) {
- throw new DubApiError({
- code: "rate_limit_exceeded",
- message:
- "Rate limited – you can only create up to 10 links per day without an account.",
- });
- }
- }
+ const body = createLinkBodySchema.parse(await req.json());
- const { link, error, code } = await processLink({
- payload: body as LinkWithTagIdsProps,
- project,
- ...(session && { userId: session.user.id }),
- });
+ if (!session) {
+ const ip = req.headers.get("x-forwarded-for") || LOCALHOST_IP;
+ const { success } = await ratelimit(10, "1 d").limit(ip);
- if (error) {
+ if (!success) {
throw new DubApiError({
- code: code as ErrorCodes,
- message: error,
+ code: "rate_limit_exceeded",
+ message:
+ "Rate limited – you can only create up to 10 links per day without an account.",
});
}
+ }
- const response = await addLink(link);
-
- if (response === null) {
- throw new DubApiError({
- code: "conflict",
- message: "Duplicate key: This short link already exists.",
- });
- }
+ const { link, error, code } = await processLink({
+ payload: body as LinkWithTagIdsProps,
+ project,
+ ...(session && { userId: session.user.id }),
+ });
- await qstash.publishJSON({
- url: `${APP_DOMAIN_WITH_NGROK}/api/cron/links/event`,
- body: {
- linkId: response.id,
- type: "create",
- },
+ if (error) {
+ throw new DubApiError({
+ code: code as ErrorCodes,
+ message: error,
});
+ }
+
+ const response = await addLink(link);
- return NextResponse.json(response, { headers });
- } catch (error) {
- return handleAndReturnErrorResponse(error);
+ if (response === null) {
+ throw new DubApiError({
+ code: "conflict",
+ message: "Duplicate key: This short link already exists.",
+ });
}
+
+ await qstash.publishJSON({
+ url: `${APP_DOMAIN_WITH_NGROK}/api/cron/links/event`,
+ body: {
+ linkId: response.id,
+ type: "create",
+ },
+ });
+
+ return NextResponse.json(response, { headers });
},
{
needNotExceededLinks: true,
diff --git a/apps/web/app/api/metatags/route.ts b/apps/web/app/api/metatags/route.ts
index 806131af63..21180e6c54 100644
--- a/apps/web/app/api/metatags/route.ts
+++ b/apps/web/app/api/metatags/route.ts
@@ -1,4 +1,4 @@
-import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";
+import { DubApiError } from "@/lib/api/errors";
import { ratelimit } from "@/lib/upstash";
import z from "@/lib/zod";
import { LOCALHOST_IP, isValidUrl } from "@dub/utils";
@@ -14,38 +14,34 @@ const getMetaTagQuerySchema = z.object({
export const runtime = "edge";
export async function GET(req: NextRequest) {
- try {
- const { url } = getMetaTagQuerySchema.parse({
- url: req.nextUrl.searchParams.get("url"),
- });
+ 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) {
- throw new DubApiError({
- code: "rate_limit_exceeded",
- message: "Don't DDoS me pls 🥺",
- });
- }
+ // 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": "*",
- },
- });
- } catch (error) {
- return handleAndReturnErrorResponse(error);
}
+
+ const metatags = await getMetaTags(url);
+ return new Response(JSON.stringify(metatags), {
+ status: 200,
+ headers: {
+ "Content-Type": "application/json",
+ "Access-Control-Allow-Origin": "*",
+ },
+ });
}
export function OPTIONS() {
diff --git a/apps/web/app/api/qr/route.tsx b/apps/web/app/api/qr/route.tsx
index d22e851b47..a555ed7ff9 100644
--- a/apps/web/app/api/qr/route.tsx
+++ b/apps/web/app/api/qr/route.tsx
@@ -1,4 +1,4 @@
-import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";
+import { DubApiError } from "@/lib/api/errors";
import { QRCodeSVG } from "@/lib/qr/utils";
import { ratelimit } from "@/lib/upstash";
import { getQRCodeQuerySchema } from "@/lib/zod/schemas/qr";
@@ -11,50 +11,46 @@ import { NextRequest } from "next/server";
export const runtime = "edge";
export async function GET(req: NextRequest) {
- try {
- // 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 🥺",
- });
- }
+ // 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 params = getSearchParams(req.url);
- const { url, size, level, fgColor, bgColor, includeMargin } =
- getQRCodeQuerySchema.parse(params);
+ const params = getSearchParams(req.url);
+ const { url, size, level, fgColor, bgColor, includeMargin } =
+ getQRCodeQuerySchema.parse(params);
- // const logo = req.nextUrl.searchParams.get("logo") || "https://d2vwwcvoksz7ty.cloudfront.net/logo.png";
+ // const logo = req.nextUrl.searchParams.get("logo") || "https://d2vwwcvoksz7ty.cloudfront.net/logo.png";
- return new ImageResponse(
- QRCodeSVG({
- value: url,
- size,
- level,
- includeMargin,
- fgColor,
- bgColor,
- // imageSettings: {
- // src: logo,
- // height: size / 4,
- // width: size / 4,
- // excavate: true,
- // },
- }),
- {
- width: size,
- height: size,
- },
- );
- } catch (error) {
- return handleAndReturnErrorResponse(error);
- }
+ return new ImageResponse(
+ QRCodeSVG({
+ value: url,
+ size,
+ level,
+ includeMargin,
+ fgColor,
+ bgColor,
+ // imageSettings: {
+ // src: logo,
+ // height: size / 4,
+ // width: size / 4,
+ // excavate: true,
+ // },
+ }),
+ {
+ width: size,
+ height: size,
+ },
+ );
}