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.

getLinkByIdentifier + getWorkspaceByPublishableKey

Steven Tey14dab377f77d
diff --git a/apps/web/app/api/track/click/route.ts b/apps/web/app/api/track/click/route.ts
index de28a737fa..1930244b57 100644
--- a/apps/web/app/api/track/click/route.ts
+++ b/apps/web/app/api/track/click/route.ts
@@ -1,6 +1,9 @@
import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";
import { extractPublishableKey, parseRequestBody } from "@/lib/api/utils";
-import { getLink, getProjectByPublishableKey } from "@/lib/planetscale";
+import {
+ getLinkByIdentifier,
+ getWorkspaceByPublishableKey,
+} from "@/lib/planetscale";
import { recordClick } from "@/lib/tinybird";
import { redis } from "@/lib/upstash";
import { nanoid } from "@dub/utils";
@@ -13,7 +16,7 @@ export const runtime = "edge";
export const POST = async (req: Request) => {
try {
const publishableKey = extractPublishableKey(req);
- const workspace = await getProjectByPublishableKey(publishableKey);
+ const workspace = await getWorkspaceByPublishableKey(publishableKey);
if (!workspace) {
throw new DubApiError({
@@ -24,7 +27,7 @@ export const POST = async (req: Request) => {
const { identifier } = await parseRequestBody(req);
- const link = await getLink(workspace.id, identifier);
+ const link = await getLinkByIdentifier(workspace.id, identifier);
if (!link) {
throw new DubApiError({
diff --git a/apps/web/lib/planetscale.ts b/apps/web/lib/planetscale.ts
index bc96daf11d..b2c3890189 100644
--- a/apps/web/lib/planetscale.ts
+++ b/apps/web/lib/planetscale.ts
@@ -103,9 +103,8 @@ export async function getRandomKey({
}
}
-// Get project by publishable key
-// TODO: Combine with getWorkspaceViaEdge
-export const getProjectByPublishableKey = async (publishableKey: string) => {
+// Get workspace by publishable key
+export const getWorkspaceByPublishableKey = async (publishableKey: string) => {
if (!DATABASE_URL) return null;
const { rows } =
@@ -119,15 +118,17 @@ export const getProjectByPublishableKey = async (publishableKey: string) => {
: null;
};
-// Get link by projectId and identifier
-// TODO: Combine with getLinkViaEdge
-export const getLink = async (projectId: string, identifier: string) => {
+// Get link by workspaceId and identifier
+export const getLinkByIdentifier = async (
+ workspaceId: string,
+ identifier: string,
+) => {
if (!DATABASE_URL) return null;
const { rows } =
(await conn.execute(
- "SELECT * FROM Link WHERE projectId = ? AND identifier = ? LIMIT 1",
- [projectId, identifier],
+ "SELECT * FROM Link WHERE projectId = ? AND identifier = ?",
+ [workspaceId, identifier],
)) || {};
return rows && Array.isArray(rows) && rows.length > 0