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.

Refactor link retrieval logic and add getLinkInfoQuerySchema

Kiran K461842e546a5
diff --git a/apps/web/app/api/links/info/route.ts b/apps/web/app/api/links/info/route.ts
index 0484430306..6a8c91f3e3 100644
--- a/apps/web/app/api/links/info/route.ts
+++ b/apps/web/app/api/links/info/route.ts
@@ -1,22 +1,38 @@
import { DubApiError } from "@/lib/api/errors";
-import { transformLink } from "@/lib/api/links";
+import { LinkWithTags, transformLink } from "@/lib/api/links";
import { withWorkspace } from "@/lib/auth";
import prisma from "@/lib/prisma";
-import { domainKeySchema } from "@/lib/zod/schemas";
+import { getLinkInfoQuerySchema } from "@/lib/zod/schemas";
import { NextResponse } from "next/server";
// GET /api/links/info – get the info for a link
-export const GET = withWorkspace(async ({ headers, searchParams }) => {
- const { domain, key } = domainKeySchema.parse(searchParams);
+export const GET = withWorkspace(async ({ headers, searchParams, link }) => {
+ const { domain, key, linkId, externalId } =
+ getLinkInfoQuerySchema.parse(searchParams);
- const link = await prisma.link.findUnique({
+ if (!domain && !key && !linkId && !externalId) {
+ throw new DubApiError({
+ code: "bad_request",
+ message:
+ "You must provide a domain and key, or linkId or externalId to retrieve a link.",
+ docUrl: "https://dub.co/docs/api-reference/endpoint/retrieve-a-link",
+ });
+ }
+
+ if (!link) {
+ throw new DubApiError({
+ code: "not_found",
+ message: "Link not found.",
+ });
+ }
+
+ // TODO:
+ // Find a better way, we already have a link object
+ const tagsAndUser = await prisma.link.findUnique({
where: {
- domain_key: {
- domain,
- key,
- },
+ id: link.id,
},
- include: {
+ select: {
user: true,
tags: {
select: {
@@ -32,14 +48,11 @@ export const GET = withWorkspace(async ({ headers, searchParams }) => {
},
});
- if (!link) {
- throw new DubApiError({
- code: "not_found",
- message: "Link not found.",
- });
- }
+ const linkWithTags: LinkWithTags = tagsAndUser
+ ? { ...link, ...tagsAndUser }
+ : { ...link, tags: [] };
- return NextResponse.json(transformLink(link), {
+ return NextResponse.json(transformLink(linkWithTags), {
headers,
});
});
diff --git a/apps/web/lib/api/links/utils.ts b/apps/web/lib/api/links/utils.ts
index 3b736ad0a6..5ad69a4682 100644
--- a/apps/web/lib/api/links/utils.ts
+++ b/apps/web/lib/api/links/utils.ts
@@ -15,7 +15,7 @@ import {
} from "@dub/utils";
import { Link, Tag } from "@prisma/client";
-type LinkWithTags = Link & {
+export type LinkWithTags = Link & {
tags: { tag: Pick<Tag, "id" | "name" | "color"> }[];
};