The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Refactor link transformation in link utils
Kiran K9d5a461e2319
diff --git a/apps/web/app/api/links/info/route.ts b/apps/web/app/api/links/info/route.tsindex 451f54755d..778d560d86 100644--- a/apps/web/app/api/links/info/route.ts+++ b/apps/web/app/api/links/info/route.ts@@ -1,8 +1,8 @@import { DubApiError } from "@/lib/api/errors";+import { transformLink } from "@/lib/api/links";import { withAuth } from "@/lib/auth";import prisma from "@/lib/prisma";import { getLinkInfoQuerySchema } from "@/lib/zod/schemas/links";-import { linkConstructor } from "@dub/utils";import { NextResponse } from "next/server";// GET /api/links/info – get the info for a link@@ -18,6 +18,17 @@ export const GET = withAuth(async ({ headers, searchParams }) => {},include: {user: true,+ tags: {+ select: {+ tag: {+ select: {+ id: true,+ name: true,+ color: true,+ },+ },+ },+ },},});@@ -28,37 +39,7 @@ export const GET = withAuth(async ({ headers, searchParams }) => {});}- const tags = await prisma.tag.findMany({- where: {- linksNew: {- some: {- linkId: link.id,- },- },- },- select: {- id: true,- name: true,- color: true,- },- });-- const shortLink = linkConstructor({- domain: link.domain,- key: link.key,+ return NextResponse.json(transformLink(link), {+ headers,});-- return NextResponse.json(- {- ...link,- shortLink,- tagId: tags?.[0]?.id ?? null, // backwards compatibility- tags,- qrCode: `https://api.dub.co/qr?url=${shortLink}`,- workspaceId: `ws_${link.projectId}`,- },- {- headers,- },- );});diff --git a/apps/web/lib/api/links/bulk-create-links.ts b/apps/web/lib/api/links/bulk-create-links.tsindex 3c9e262b43..573af8bbbf 100644--- a/apps/web/lib/api/links/bulk-create-links.ts+++ b/apps/web/lib/api/links/bulk-create-links.ts@@ -2,8 +2,8 @@ import prisma from "@/lib/prisma";import { recordLink } from "@/lib/tinybird";import { LinkProps, LinkWithTagIdsProps, RedisLinkProps } from "@/lib/types";import { formatRedisLink, redis } from "@/lib/upstash";-import { getParamsFromURL, linkConstructor, truncate } from "@dub/utils";-import { combineTagIds } from "./utils";+import { getParamsFromURL, truncate } from "@dub/utils";+import { combineTagIds, transformLink } from "./utils";export async function bulkCreateLinks({links,@@ -61,21 +61,7 @@ export async function bulkCreateLinks({await propagateBulkLinkChanges(createdLinks);- return createdLinks.map((link) => {- const shortLink = linkConstructor({- domain: link.domain,- key: link.key,- });- const tags = link.tags.map(({ tag }) => tag);- return {- ...link,- shortLink,- tagId: tags?.[0]?.id ?? null, // backwards compatibility- tags,- qrCode: `https://api.dub.co/qr?url=${shortLink}`,- workspaceId: `ws_${link.projectId}`,- };- });+ return createdLinks.map((link) => transformLink(link));}export async function propagateBulkLinkChanges(diff --git a/apps/web/lib/api/links/create-link.ts b/apps/web/lib/api/links/create-link.tsindex da90bab676..c842f81bb8 100644--- a/apps/web/lib/api/links/create-link.ts+++ b/apps/web/lib/api/links/create-link.ts@@ -4,14 +4,9 @@ import { isStored, storage } from "@/lib/storage";import { recordLink } from "@/lib/tinybird";import { LinkWithTagIdsProps } from "@/lib/types";import { formatRedisLink, redis } from "@/lib/upstash";-import {- APP_DOMAIN_WITH_NGROK,- getParamsFromURL,- linkConstructor,- truncate,-} from "@dub/utils";+import { APP_DOMAIN_WITH_NGROK, getParamsFromURL, truncate } from "@dub/utils";import { Prisma } from "@prisma/client";-import { combineTagIds, dubLinkChecks } from "./utils";+import { combineTagIds, dubLinkChecks, transformLink } from "./utils";export async function createLink(link: LinkWithTagIdsProps) {let { key, url, expiresAt, title, description, image, proxy, geo } = link;@@ -122,20 +117,10 @@ export async function createLink(link: LinkWithTagIdsProps) {link.domain === "dub.sh" && dubLinkChecks(link),]);- const shortLink = linkConstructor({- domain: response.domain,- key: response.key,- });- const tags = response.tags.map(({ tag }) => tag);return {- ...response,+ ...transformLink(response),// optimistically set the image URL to the uploaded image URLimage:proxy && image && !isStored(image) ? uploadedImageUrl : response.image,- tagId: tags?.[0]?.id ?? null, // backwards compatibility- tags,- shortLink,- qrCode: `https://api.dub.co/qr?url=${shortLink}`,- workspaceId: `ws_${response.projectId}`,};}diff --git a/apps/web/lib/api/links/edit-link.ts b/apps/web/lib/api/links/edit-link.tsindex 285e81a24b..f648420c4e 100644--- a/apps/web/lib/api/links/edit-link.ts+++ b/apps/web/lib/api/links/edit-link.ts@@ -3,14 +3,9 @@ import { isStored, storage } from "@/lib/storage";import { recordLink } from "@/lib/tinybird";import { LinkWithTagIdsProps } from "@/lib/types";import { formatRedisLink, redis } from "@/lib/upstash";-import {- SHORT_DOMAIN,- getParamsFromURL,- linkConstructor,- truncate,-} from "@dub/utils";+import { SHORT_DOMAIN, getParamsFromURL, truncate } from "@dub/utils";import { Prisma } from "@prisma/client";-import { combineTagIds } from "./utils";+import { combineTagIds, transformLink } from "./utils";export async function editLink({oldDomain = SHORT_DOMAIN,@@ -125,19 +120,5 @@ export async function editLink({redis.hdel(oldDomain, oldKey.toLowerCase()),]);- const shortLink = linkConstructor({- domain: response.domain,- key: response.key,- });-- const tags = response.tags.map(({ tag }) => tag);-- return {- ...response,- tagId: tags?.[0]?.id ?? null, // backwards compatibility- tags,- shortLink,- qrCode: `https://api.dub.co/qr?url=${shortLink}`,- workspaceId: `ws_${response.projectId}`,- };+ return transformLink(response);}diff --git a/apps/web/lib/api/links/get-links-for-workspace.ts b/apps/web/lib/api/links/get-links-for-workspace.tsindex 82f765cd13..a11190135d 100644--- a/apps/web/lib/api/links/get-links-for-workspace.ts+++ b/apps/web/lib/api/links/get-links-for-workspace.ts@@ -1,8 +1,7 @@import prisma from "@/lib/prisma";import z from "@/lib/zod";import { getLinksQuerySchema } from "@/lib/zod/schemas/links";-import { linkConstructor } from "@dub/utils";-import { combineTagIds } from "./utils";+import { combineTagIds, transformLink } from "./utils";export async function getLinksForWorkspace({workspaceId,@@ -68,21 +67,5 @@ export async function getLinksForWorkspace({}),});- return links.map((link) => {- const shortLink = linkConstructor({- domain: link.domain,- key: link.key,- });-- const tags = link.tags.map(({ tag }) => tag);-- return {- ...link,- tagId: tags?.[0]?.id ?? null, // backwards compatibility- tags,- shortLink,- qrCode: `https://api.dub.co/qr?url=${shortLink}`,- workspaceId: `ws_${link.projectId}`,- };- });+ return links.map((link) => transformLink(link));}diff --git a/apps/web/lib/api/links/utils.ts b/apps/web/lib/api/links/utils.tsindex 3b4715ab1e..4893e11f59 100644--- a/apps/web/lib/api/links/utils.ts+++ b/apps/web/lib/api/links/utils.ts@@ -7,9 +7,15 @@ import {SHORT_DOMAIN,getApexDomain,isDubDomain,+ linkConstructor,log,validKeyRegex,} from "@dub/utils";+import { Link, Tag } from "@prisma/client";++type LinkWithTags = Link & {+ tags: { tag: Pick<Tag, "id" | "name" | "color"> }[];+};export function combineTagIds({tagId,@@ -117,3 +123,22 @@ export async function dubLinkChecks(link: SimpleLinkProps) {}return null;}++// Transform link with additional properties+export const transformLink = (link: LinkWithTags) => {+ const tags = link.tags.map(({ tag }) => tag);++ const shortLink = linkConstructor({+ domain: link.domain,+ key: link.key,+ });++ return {+ ...link,+ shortLink,+ tagId: tags?.[0]?.id ?? null, // backwards compatibility+ tags,+ qrCode: `https://api.dub.co/qr?url=${shortLink}`,+ workspaceId: `ws_${link.projectId}`,+ };+};