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.

chore: Update getDomainOrLink to getLinkViaEdge in multiple files

Kiran K5e3b8754f1eb
diff --git a/apps/web/app/[domain]/stats/[key]/page.tsx b/apps/web/app/[domain]/stats/[key]/page.tsx
index cbc60f40e8..b8d8b6de6d 100644
--- a/apps/web/app/[domain]/stats/[key]/page.tsx
+++ b/apps/web/app/[domain]/stats/[key]/page.tsx
@@ -1,4 +1,4 @@
-import { getDomainOrLink } from "@/lib/planetscale";
+import { getLinkViaEdge } from "@/lib/planetscale";
import Analytics from "@/ui/analytics";
import { constructMetadata } from "@dub/utils";
import { notFound } from "next/navigation";
@@ -11,7 +11,7 @@ export async function generateMetadata({
}: {
params: { domain: string; key: string };
}) {
- const data = await getDomainOrLink(params);
+ const data = await getLinkViaEdge(params.domain, params.key || "_root");
// if the link doesn't exist or is explicitly private (publicStats === false)
if (!data?.publicStats) {
@@ -29,7 +29,7 @@ export default async function StatsPage({
}: {
params: { domain: string; key: string };
}) {
- const data = await getDomainOrLink(params);
+ const data = await getLinkViaEdge(params.domain, params.key || "_root");
if (!data?.publicStats) {
notFound();
diff --git a/apps/web/app/api/analytics/edge/route.ts b/apps/web/app/api/analytics/edge/route.ts
index 89f15ce823..137e88a5d9 100644
--- a/apps/web/app/api/analytics/edge/route.ts
+++ b/apps/web/app/api/analytics/edge/route.ts
@@ -5,7 +5,7 @@ import {
exceededLimitError,
handleAndReturnErrorResponse,
} from "@/lib/api/errors";
-import { getDomainOrLink, getWorkspaceViaEdge } from "@/lib/planetscale";
+import { getLinkViaEdge, getWorkspaceViaEdge } from "@/lib/planetscale";
import { ratelimit } from "@/lib/upstash";
import { analyticsQuerySchema } from "@/lib/zod/schemas/analytics";
import { DUB_DEMO_LINKS, DUB_WORKSPACE_ID, getSearchParams } from "@dub/utils";
@@ -56,7 +56,8 @@ export const GET = async (req: NextRequest) => {
projectId: DUB_WORKSPACE_ID,
};
} else if (domain) {
- link = await getDomainOrLink({ domain, key });
+ link = await getLinkViaEdge(domain, key);
+
// if the link is explicitly private (publicStats === false)
if (!link?.publicStats) {
throw new DubApiError({
diff --git a/apps/web/app/api/analytics/public-stats/route.ts b/apps/web/app/api/analytics/public-stats/route.ts
index d69ae17592..35078d2317 100644
--- a/apps/web/app/api/analytics/public-stats/route.ts
+++ b/apps/web/app/api/analytics/public-stats/route.ts
@@ -1,5 +1,5 @@
import { withWorkspace } from "@/lib/auth";
-import { getDomainOrLink } from "@/lib/planetscale";
+import { getLinkViaEdge } from "@/lib/planetscale";
import { prisma } from "@/lib/prisma";
import z from "@/lib/zod";
import { domainKeySchema } from "@/lib/zod/schemas/links";
@@ -9,14 +9,14 @@ const updatePublicStatsSchema = z.object({
publicStats: z.boolean(),
});
-// GET /api/analytics – get the publicStats setting for a link
+// GET /api/analytics/public-stats – get the publicStats setting for a link
export const GET = withWorkspace(async ({ searchParams }) => {
const { domain, key } = domainKeySchema.parse(searchParams);
- const response = await getDomainOrLink({ domain, key });
- return NextResponse.json(response);
+ const response = await getLinkViaEdge(domain, key);
+ return NextResponse.json({ publicStats: response?.publicStats });
});
-// PUT /api/analytics – update the publicStats setting for a link
+// PUT /api/analytics/public-stats – update the publicStats setting for a link
export const PUT = withWorkspace(async ({ req, searchParams }) => {
const { domain, key } = domainKeySchema.parse(searchParams);
const { publicStats } = updatePublicStatsSchema.parse(await req.json());
diff --git a/apps/web/app/api/og/analytics/route.tsx b/apps/web/app/api/og/analytics/route.tsx
index e582803429..6d80ac9a1d 100644
--- a/apps/web/app/api/og/analytics/route.tsx
+++ b/apps/web/app/api/og/analytics/route.tsx
@@ -1,5 +1,5 @@
import { getAnalytics } from "@/lib/analytics/get-analytics";
-import { getDomainOrLink } from "@/lib/planetscale";
+import { getLinkViaEdge } from "@/lib/planetscale";
import {
GOOGLE_FAVICON_URL,
getApexDomain,
@@ -19,7 +19,7 @@ export async function GET(req: NextRequest) {
const domain = req.nextUrl.searchParams.get("domain") || "dub.sh";
const key = req.nextUrl.searchParams.get("key") || "github";
- const link = await getDomainOrLink({ domain, key });
+ const link = await getLinkViaEdge(domain, key);
if (!link?.publicStats) {
return new Response(`Stats for this link are not public`, {
status: 403,
diff --git a/apps/web/lib/middleware/root.ts b/apps/web/lib/middleware/root.ts
index ee002dcdff..fa407a18ff 100644
--- a/apps/web/lib/middleware/root.ts
+++ b/apps/web/lib/middleware/root.ts
@@ -2,7 +2,7 @@ import { recordClick } from "@/lib/tinybird";
import { formatRedisDomain, redis } from "@/lib/upstash";
import { DUB_HEADERS } from "@dub/utils";
import { NextFetchEvent, NextRequest, NextResponse } from "next/server";
-import { getDomainViaEdge } from "../planetscale";
+import { getLinkViaEdge } from "../planetscale";
import { RedisDomainProps } from "../types";
import { parse } from "./utils";
@@ -19,7 +19,7 @@ export default async function RootMiddleware(
let link = await redis.hget<RedisDomainProps>(domain, "_root");
if (!link) {
- const linkData = await getDomainViaEdge(domain);
+ const linkData = await getLinkViaEdge(domain, "_root");
if (!linkData) {
// rewrite to placeholder page if domain doesn't exist
diff --git a/apps/web/lib/planetscale.ts b/apps/web/lib/planetscale.ts
index 0cbc2b4495..528dab6b78 100644
--- a/apps/web/lib/planetscale.ts
+++ b/apps/web/lib/planetscale.ts
@@ -1,6 +1,6 @@
import { nanoid, punyEncode } from "@dub/utils";
import { connect } from "@planetscale/database";
-import { DomainProps, WorkspaceProps } from "./types";
+import { WorkspaceProps } from "./types";
export const DATABASE_URL =
process.env.PLANETSCALE_DATABASE_URL || process.env.DATABASE_URL;
@@ -23,17 +23,6 @@ export const getWorkspaceViaEdge = async (workspaceId: string) => {
return rows && Array.isArray(rows) && rows.length > 0 ? rows[0] : null;
};
-export const getDomainViaEdge = async (domain: string) => {
- if (!DATABASE_URL) return null;
-
- const { rows } =
- (await conn.execute("SELECT * FROM Domain WHERE slug = ?", [domain])) || {};
-
- return rows && Array.isArray(rows) && rows.length > 0
- ? (rows[0] as DomainProps)
- : null;
-};
-
export const checkIfKeyExists = async (domain: string, key: string) => {
if (!DATABASE_URL) return null;
@@ -88,26 +77,6 @@ export const getLinkViaEdge = async (domain: string, key: string) => {
: null;
};
-export async function getDomainOrLink({
- domain,
- key,
-}: {
- domain: string;
- key?: string;
-}) {
- if (!key || key === "_root") {
- const data = await getDomainViaEdge(domain);
- if (!data) return null;
- return {
- ...data,
- key: "_root",
- url: data?.target,
- };
- } else {
- return await getLinkViaEdge(domain, key);
- }
-}
-
export async function getRandomKey({
domain,
prefix,
diff --git a/apps/web/lib/upstash.ts b/apps/web/lib/upstash.ts
index 5d6e6f86b3..3ca54d2593 100644
--- a/apps/web/lib/upstash.ts
+++ b/apps/web/lib/upstash.ts
@@ -1,12 +1,7 @@
import { getDomainWithoutWWW, isIframeable } from "@dub/utils";
import { Ratelimit } from "@upstash/ratelimit";
import { Redis } from "@upstash/redis";
-import {
- DomainProps,
- LinkProps,
- RedisDomainProps,
- RedisLinkProps,
-} from "./types";
+import { LinkProps, RedisDomainProps, RedisLinkProps } from "./types";
// Initiate Redis instance by connecting to REST URL
export const redis = new Redis({
@@ -100,20 +95,22 @@ export async function formatRedisLink(
};
}
+// TODO:
+// Can we replace this with the formatRedisLink function?
export async function formatRedisDomain(
- domain: DomainProps,
+ link: LinkProps,
): Promise<RedisDomainProps> {
- const { id, slug, target: url, type, projectId, noindex } = domain;
+ const { id, domain, url, rewrite, projectId, noindex } = link;
return {
id,
...(url && { url }), // on free plans you cannot set a root domain redirect, hence URL is undefined
...(url &&
- type === "rewrite" && {
+ rewrite && {
rewrite: true,
- iframeable: await isIframeable({ url, requestDomain: slug }),
+ iframeable: await isIframeable({ url, requestDomain: domain }),
}),
- projectId,
+ projectId: projectId!,
...(noindex && { noindex: true }),
};
}