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.

maintain backwards compatibility

Steven Teye123a632b761
diff --git a/apps/web/app/api/links/[linkId]/route.ts b/apps/web/app/api/links/[linkId]/route.ts
index 58eb1d0e9b..b52b09ed0c 100644
--- a/apps/web/app/api/links/[linkId]/route.ts
+++ b/apps/web/app/api/links/[linkId]/route.ts
@@ -6,13 +6,41 @@ import { LinkWithTagIdsProps } from "@/lib/types";
import { updateLinkBodySchema } from "@/lib/zod/schemas/links";
import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
import { NextResponse } from "next/server";
+import prisma from "@/lib/prisma";
// GET /api/links/[linkId] – get a link
export const GET = withAuth(async ({ headers, link }) => {
- // link is guaranteed to exist because if not we will return 404
- return NextResponse.json(link!, {
- headers,
+ if (!link) {
+ throw new DubApiError({
+ code: "not_found",
+ message: "Link not found.",
+ });
+ }
+
+ const tags = await prisma.tag.findMany({
+ where: {
+ links: {
+ some: {
+ id: link.id,
+ },
+ },
+ },
+ select: {
+ id: true,
+ name: true,
+ color: true,
+ },
});
+ // link is guaranteed to exist because if not we will return 404
+ return NextResponse.json(
+ {
+ ...link,
+ tags,
+ },
+ {
+ headers,
+ },
+ );
});
// PUT /api/links/[linkId] – update a link
diff --git a/apps/web/lib/api/links.ts b/apps/web/lib/api/links.ts
index 62b2656309..6143249d8f 100644
--- a/apps/web/lib/api/links.ts
+++ b/apps/web/lib/api/links.ts
@@ -28,6 +28,7 @@ import { recordLink } from "../tinybird";
import {
LinkProps,
LinkWithTagIdsProps,
+ LinkWithTagsProps,
ProjectProps,
RedisLinkProps,
TagProps,
@@ -106,9 +107,11 @@ export async function getLinksForProject({
domain: link.domain,
key: link.key,
});
+ const tags = link.tags.map(({ tag }) => tag);
return {
...link,
- tags: link.tags.map(({ tag }) => tag),
+ tagId: tags?.[0]?.id ?? null, // backwards compatibility
+ tags,
shortLink,
qrCode: `https://api.dub.co/qr?url=${shortLink}`,
};
@@ -550,8 +553,10 @@ export async function addLink(link: LinkWithTagIdsProps) {
domain: response.domain,
key: response.key,
});
+ const tags = response.tags.map(({ tag }) => tag);
return {
...response,
+ tagId: tags?.[0]?.id ?? null, // backwards compatibility
tags: response.tags.map(({ tag }) => tag),
shortLink,
qrCode: `https://api.dub.co/qr?url=${shortLink}`,
@@ -567,11 +572,11 @@ export async function bulkCreateLinks({
}) {
if (links.length === 0) return [];
- let createdLinks: LinkProps[] = [];
+ let createdLinks: LinkWithTagsProps[] = [];
let linkTags: { tagId: string; linkId: string }[] = [];
if (skipPrismaCreate) {
- createdLinks = links;
+ createdLinks = links as unknown as LinkWithTagsProps[];
} else {
await prisma.link.createMany({
data: links.map(({ tagId, tagIds, ...link }) => {
@@ -816,10 +821,12 @@ export async function editLink({
domain: response.domain,
key: response.key,
});
+ const tags = response.tags.map(({ tag }) => tag);
return {
...response,
- tags: response.tags.map(({ tag }) => tag),
+ tags,
+ tagId: tags?.[0]?.id ?? null, // backwards compatibility
shortLink,
qrCode: `https://api.dub.co/qr?url=${shortLink}`,
};
diff --git a/apps/web/lib/auth/index.ts b/apps/web/lib/auth/index.ts
index 803c155209..a5e5bd192d 100644
--- a/apps/web/lib/auth/index.ts
+++ b/apps/web/lib/auth/index.ts
@@ -58,7 +58,7 @@ interface WithAuthHandler {
session: Session;
project: ProjectProps;
domain: string;
- link?: LinkProps & { tags: TagProps[] };
+ link?: LinkProps;
}): Promise<Response>;
}
@@ -210,20 +210,6 @@ export const withAuth = (
}
}
- const includeTags = {
- tags: {
- include: {
- tag: {
- select: {
- id: true,
- name: true,
- color: true,
- },
- },
- },
- },
- };
-
let [project, link] = (await Promise.all([
prisma.project.findUnique({
where: {
@@ -267,7 +253,6 @@ export const withAuth = (
where: {
id: linkId,
},
- include: includeTags,
})
: domain &&
key &&
@@ -284,12 +269,8 @@ export const withAuth = (
key,
},
},
- include: includeTags,
})),
- ])) as [
- ProjectProps,
- (LinkProps & { tags?: { tag: TagProps }[] }) | undefined,
- ];
+ ])) as [ProjectProps, LinkProps | undefined];
if (!project || !project.users) {
// project doesn't exist
@@ -439,10 +420,7 @@ export const withAuth = (
session,
project,
domain,
- link: link && {
- ...link,
- tags: link?.tags?.map(({ tag }) => tag) ?? [],
- },
+ link,
});
} catch (error) {
console.error("withAuth Error -->", error.message);
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index 173aef4fe1..4f3df42afb 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -11,7 +11,6 @@ export interface LinkWithTagsProps extends LinkProps {
export interface LinkWithTagIdsProps extends LinkProps {
tagIds: string[];
- tagId?: string;
}
export interface SimpleLinkProps {