The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
some cleanup in track click
Kiran Kc4e7da09b9fc
diff --git a/apps/web/app/(ee)/api/track/click/route.ts b/apps/web/app/(ee)/api/track/click/route.tsindex 05cbd644f5..3375fb0885 100644--- a/apps/web/app/(ee)/api/track/click/route.ts+++ b/apps/web/app/(ee)/api/track/click/route.ts@@ -103,13 +103,13 @@ export const POST = withAxiom(async (req: AxiomRequest) => {// if there's no cached clickId, track the click eventif (!cachedClickId) {- console.log("cachedAllowedHostnames", cachedAllowedHostnames);-if (!cachedAllowedHostnames) {- const workspace = await getWorkspaceViaEdge(cachedLink.projectId!);- cachedAllowedHostnames = workspace?.allowedHostnames as string[];+ const workspace = await getWorkspaceViaEdge({+ workspaceId: cachedLink.projectId!,+ includeDomains: true,+ });- console.log("workspace", workspace);+ cachedAllowedHostnames = workspace?.allowedHostnames as string[];waitUntil(allowedHostnamesCache.mset({@@ -119,12 +119,12 @@ export const POST = withAxiom(async (req: AxiomRequest) => {);}- if (- !verifyAnalyticsAllowedHostnames({- allowedHostnames: cachedAllowedHostnames,- req,- })- ) {+ const allowRequest = verifyAnalyticsAllowedHostnames({+ allowedHostnames: cachedAllowedHostnames,+ req,+ });++ if (!allowRequest) {throw new DubApiError({code: "forbidden",message: "Click tracking is not allowed from this referrer.",diff --git a/apps/web/app/(ee)/api/track/visit/route.ts b/apps/web/app/(ee)/api/track/visit/route.tsindex c8feb2a9d6..3b91555d84 100644--- a/apps/web/app/(ee)/api/track/visit/route.ts+++ b/apps/web/app/(ee)/api/track/visit/route.ts@@ -76,7 +76,10 @@ export const POST = withAxiom(async (req: AxiomRequest) => {waitUntil((async () => {- const workspace = await getWorkspaceViaEdge(cachedLink.projectId!);+ const workspace = await getWorkspaceViaEdge({+ workspaceId: cachedLink.projectId!,+ });+const allowedHostnames = workspace?.allowedHostnames as string[];if (diff --git a/apps/web/app/api/links/exists/route.ts b/apps/web/app/api/links/exists/route.tsindex 0443757ecb..d6af1349dc 100644--- a/apps/web/app/api/links/exists/route.ts+++ b/apps/web/app/api/links/exists/route.ts@@ -26,7 +26,9 @@ export const GET = async (req: NextRequest) => {}key = processedKey;- const workspace = await getWorkspaceViaEdge(workspaceId);+ const workspace = await getWorkspaceViaEdge({+ workspaceId,+ });if (!workspace) {throw new DubApiError({diff --git a/apps/web/app/api/qr/route.tsx b/apps/web/app/api/qr/route.tsxindex cf3af7990b..8fee51dbe9 100644--- a/apps/web/app/api/qr/route.tsx+++ b/apps/web/app/api/qr/route.tsx@@ -73,7 +73,9 @@ const getQRCodeLogo = async ({return DUB_QR_LOGO;}- const workspace = await getWorkspaceViaEdge(shortLink.projectId);+ const workspace = await getWorkspaceViaEdge({+ workspaceId: shortLink.projectId,+ });if (workspace?.plan === "free") {return DUB_QR_LOGO;diff --git a/apps/web/lib/planetscale/get-workspace-via-edge.ts b/apps/web/lib/planetscale/get-workspace-via-edge.tsindex f6c689546e..add729962e 100644--- a/apps/web/lib/planetscale/get-workspace-via-edge.ts+++ b/apps/web/lib/planetscale/get-workspace-via-edge.ts@@ -2,12 +2,61 @@ import { normalizeWorkspaceId } from "../api/workspace-id";import { WorkspaceProps } from "../types";import { conn } from "./connection";-export const getWorkspaceViaEdge = async (workspaceId: string) => {+export const getWorkspaceViaEdge = async ({+ workspaceId,+ includeDomains = false,+}: {+ workspaceId: string;+ includeDomains?: boolean;+}) => {+ const query = includeDomains+ ? `+ SELECT+ w.*,+ d.slug,+ d.verified+ FROM Project w+ LEFT JOIN Domain d ON w.id = d.projectId+ WHERE w.id = ?+ `+ : `+ SELECT+ w.*+ FROM Project w+ WHERE w.id = ?+ LIMIT 1+ `;+const { rows } =- (await conn.execute<WorkspaceProps>(- "SELECT * FROM Project WHERE id = ? LIMIT 1",- [normalizeWorkspaceId(workspaceId)],- )) || {};+ (await conn.execute(query, [normalizeWorkspaceId(workspaceId)])) || {};++ if (!rows || !Array.isArray(rows) || rows.length === 0) {+ return null;+ }++ if (!includeDomains) {+ return rows[0] as WorkspaceProps;+ }++ const firstRow = rows[0] as any;+ const workspaceData = { ...firstRow };+ const domains: { slug: string; verified: boolean }[] = [];++ // Process all rows to collect domains+ rows.forEach((row: any) => {+ if (row.slug && row.verified) {+ domains.push({+ slug: row.slug,+ verified: row.verified,+ });+ }+ });++ delete workspaceData.slug;+ delete workspaceData.verified;- return rows && Array.isArray(rows) && rows.length > 0 ? rows[0] : null;+ return {+ ...workspaceData,+ domains,+ } as WorkspaceProps & { domains: { slug: string; verified: boolean }[] };};