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.

update tests + implementation

Steven Tey5dfef9bbadc6
diff --git a/apps/web/app/(ee)/api/track/open/route.ts b/apps/web/app/(ee)/api/track/open/route.ts
index c235424b77..9e6efa64e3 100644
--- a/apps/web/app/(ee)/api/track/open/route.ts
+++ b/apps/web/app/(ee)/api/track/open/route.ts
@@ -6,6 +6,7 @@ import { getLinkViaEdge } from "@/lib/planetscale";
import { recordClick } from "@/lib/tinybird";
import { RedisLinkProps } from "@/lib/types";
import { formatRedisLink, redis } from "@/lib/upstash";
+import { parseUrlSchema } from "@/lib/zod/schemas/utils";
import { LOCALHOST_IP, nanoid } from "@dub/utils";
import { ipAddress, waitUntil } from "@vercel/functions";
import { AxiomRequest, withAxiom } from "next-axiom";
@@ -18,26 +19,6 @@ const CORS_HEADERS = {
"Access-Control-Allow-Headers": "Content-Type, Authorization",
};
-// Utility function to parse deep link URL and extract domain and key
-const parseDeepLink = (deepLink: string): { domain: string; key: string } => {
- try {
- const url = new URL(deepLink);
- const domain = url.hostname.replace(/^www\./, "").toLowerCase();
- const key = url.pathname.slice(1) || "_root"; // Remove leading slash, default to _root if empty
-
- return { domain, key };
- } catch (error) {
- throw new DubApiError({
- code: "bad_request",
- message: "Invalid deep link URL format",
- });
- }
-};
-
-const trackOpenRequestSchema = z.object({
- deepLink: z.string().trim().url("Invalid deep link URL format"),
-});
-
const trackOpenResponseSchema = z.object({
clickId: z.string(),
link: z.object({
@@ -51,11 +32,16 @@ const trackOpenResponseSchema = z.object({
// POST /api/track/open – Track an open event for deep link
export const POST = withAxiom(async (req: AxiomRequest) => {
try {
- const { deepLink } = trackOpenRequestSchema.parse(
- await parseRequestBody(req),
- );
+ const { deepLink: deepLinkUrl } = z
+ .object({
+ deepLink: parseUrlSchema,
+ })
+ .parse(await parseRequestBody(req));
- const { domain, key } = parseDeepLink(deepLink);
+ const deepLink = new URL(deepLinkUrl);
+
+ const domain = deepLink.hostname.replace(/^www\./, "").toLowerCase();
+ const key = deepLink.pathname.slice(1) || "_root"; // Remove leading slash, default to _root if empty
const ip = process.env.VERCEL === "1" ? ipAddress(req) : LOCALHOST_IP;
@@ -70,6 +56,8 @@ export const POST = withAxiom(async (req: AxiomRequest) => {
// else, reuse the cached clickId
const clickId = cachedClickId ?? nanoid(16);
+ console.log({ deepLink, cachedClickId, cachedLink });
+
if (!cachedLink) {
const link = await getLinkViaEdge({
domain,
diff --git a/apps/web/tests/tracks/track-open.test.ts b/apps/web/tests/tracks/track-open.test.ts
index a360ff4e20..398a4c05de 100644
--- a/apps/web/tests/tracks/track-open.test.ts
+++ b/apps/web/tests/tracks/track-open.test.ts
@@ -20,6 +20,8 @@ const expectValidClickResponse = ({
});
};
+const deepLink = `https://${E2E_LINK.domain}/${E2E_LINK.key}`;
+
describe("POST /track/open", async () => {
const h = new IntegrationHarness();
const { http } = await h.init();
@@ -29,7 +31,7 @@ describe("POST /track/open", async () => {
path: "/track/open",
headers: E2E_TRACK_CLICK_HEADERS,
body: {
- deepLink: `https://${E2E_LINK.domain}/${E2E_LINK.key}`,
+ deepLink,
},
});
@@ -40,11 +42,10 @@ describe("POST /track/open", async () => {
test("same clickId should be returned on subsequent requests", async () => {
const response1 = await http.post<{ clickId: string }>({
- path: "/track/click",
+ path: "/track/open",
headers: E2E_TRACK_CLICK_HEADERS,
body: {
- domain: E2E_LINK.domain,
- key: E2E_LINK.key,
+ deepLink,
},
});
@@ -52,7 +53,7 @@ describe("POST /track/open", async () => {
path: "/track/open",
headers: E2E_TRACK_CLICK_HEADERS,
body: {
- deepLink: `https://${E2E_LINK.domain}/${E2E_LINK.key}`,
+ deepLink,
},
});
@@ -101,7 +102,7 @@ describe("POST /track/open", async () => {
path: "/track/open",
headers: E2E_TRACK_CLICK_HEADERS,
body: {
- deepLink: `https://${E2E_LINK.domain}/${E2E_LINK.key}`,
+ deepLink,
},
});
@@ -113,7 +114,7 @@ describe("POST /track/open", async () => {
path: "/track/open",
headers: E2E_TRACK_CLICK_HEADERS,
body: {
- deepLink: `https://${E2E_LINK.domain}/${E2E_LINK.key}`,
+ deepLink,
},
});