The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Clean up tagId from schema while maintaining backwards compatibility
Tim Wilson8ebe416f27f0
diff --git a/apps/web/app/api/route.ts b/apps/web/app/api/route.tsindex 45aacd7378..7927d7a015 100644--- a/apps/web/app/api/route.ts+++ b/apps/web/app/api/route.ts@@ -201,8 +201,15 @@ export function GET(): NextResponse<OpenAPIV3.Document> {content: {"application/json": {schema: {- $ref: "#/components/schemas/LinkBody",- required: ["url"],+ allOf: [+ {+ $ref: "#/components/schemas/LinkBody",+ required: ["url"],+ },+ {+ $ref: "#/components/schemas/LinkTagIdsBody",+ },+ ],},},},@@ -326,7 +333,14 @@ export function GET(): NextResponse<OpenAPIV3.Document> {content: {"application/json": {schema: {- $ref: "#/components/schemas/LinkBody",+ allOf: [+ {+ $ref: "#/components/schemas/LinkBody",+ },+ {+ $ref: "#/components/schemas/LinkTagIdsBody",+ },+ ],},},},@@ -426,7 +440,15 @@ export function GET(): NextResponse<OpenAPIV3.Document> {schema: {type: "array",items: {- $ref: "#/components/schemas/LinkBody",+ allOf: [+ {+ $ref: "#/components/schemas/LinkBody",+ required: ["url"],+ },+ {+ $ref: "#/components/schemas/LinkTagIdsBody",+ },+ ],},},},@@ -842,6 +864,17 @@ export function GET(): NextResponse<OpenAPIV3.Document> {"The unique ID of the tag assigned to the short link.",deprecated: true,},+ comments: {+ type: "string",+ description: "The comments for the short link.",+ default: null,+ nullable: true,+ },+ },+ },+ LinkTagIdsBody: {+ type: "object",+ properties: {tagIds: {type: "array",items: {@@ -852,12 +885,6 @@ export function GET(): NextResponse<OpenAPIV3.Document> {"The unique IDs of the tags assigned to the short link.",default: [],},- comments: {- type: "string",- description: "The comments for the short link.",- default: null,- nullable: true,- },},},LinkResponse: {diff --git a/apps/web/lib/api/links.ts b/apps/web/lib/api/links.tsindex 8d8fa44bd3..8e9b4982d9 100644--- a/apps/web/lib/api/links.ts+++ b/apps/web/lib/api/links.ts@@ -27,8 +27,23 @@ import {LinkWithTagIdsProps,ProjectProps,RedisLinkProps,+ TagProps,} from "../types";+const includeTags = {+ tags: {+ include: {+ tag: {+ select: {+ id: true,+ name: true,+ color: true,+ },+ },+ },+ },+};+export async function getLinksForProject({projectId,domain,@@ -79,17 +94,7 @@ export async function getLinksForProject({},include: {user: true,- tags: {- include: {- tag: {- select: {- id: true,- name: true,- color: true,- },- },- },- },+ ...includeTags,},orderBy: {[sort]: "desc",@@ -529,6 +534,7 @@ export async function addLink(link: LinkWithTagIdsProps) {},}),},+ include: includeTags,});const shortLink = linkConstructor({@@ -537,6 +543,7 @@ export async function addLink(link: LinkWithTagIdsProps) {});return {...response,+ tags: response.tags.map(({ tag }) => tag),shortLink,qrCode: `https://api.dub.co/qr?url=${shortLink}`,};@@ -590,6 +597,7 @@ export async function bulkCreateLinks({key,},},+ include: includeTags,});if (!data) return null;// combine tagIds for creation later@@ -599,10 +607,10 @@ export async function bulkCreateLinks({);return {...data,- tagIds: combinedTagIds,+ tags: data.tags.map(({ tag }) => tag),};}),- )) as LinkWithTagIdsProps[];+ )) as (LinkProps & { tags: TagProps[] })[];}const pipeline = redis.pipeline();@@ -713,6 +721,7 @@ export async function editLink({where: {id,},+ include: includeTags,data: {...rest,key,@@ -790,6 +799,7 @@ export async function editLink({return {...response,+ tags: response.tags.map(({ tag }) => tag),shortLink,qrCode: `https://api.dub.co/qr?url=${shortLink}`,};diff --git a/apps/web/lib/auth/index.ts b/apps/web/lib/auth/index.tsindex 63a720cdf1..bc554a3475 100644--- a/apps/web/lib/auth/index.ts+++ b/apps/web/lib/auth/index.ts@@ -5,7 +5,7 @@ import { isAdmin } from "app/admin.dub.co/actions";import { createHash } from "crypto";import { getServerSession } from "next-auth/next";import { exceededLimitError } from "../api/errors";-import { PlanProps, ProjectProps } from "../types";+import { PlanProps, ProjectProps, TagProps } from "../types";import { ratelimit } from "../upstash";import { authOptions } from "./options";@@ -53,7 +53,7 @@ interface WithAuthHandler {session: Session;project: ProjectProps;domain: string;- link?: LinkProps;+ link?: LinkProps & { tags: TagProps[] };}): Promise<Response>;}export const withAuth =@@ -198,6 +198,20 @@ 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: {@@ -242,6 +256,7 @@ export const withAuth =where: {id: linkId,},+ include: includeTags,}): domain &&key &&@@ -258,8 +273,12 @@ export const withAuth =key,},},+ include: includeTags,})),- ])) as [ProjectProps, LinkProps | undefined];+ ])) as [+ ProjectProps,+ (LinkProps & { tags?: { tag: TagProps }[] }) | undefined,+ ];if (!project || !project.users) {// project doesn't exist@@ -403,7 +422,7 @@ export const withAuth =session,project,domain,- link,+ link: link && { ...link, tags: link?.tags?.map(({ tag }) => tag) ?? [] },});};diff --git a/apps/web/lib/prisma.ts b/apps/web/lib/prisma.tsindex 10082dc2b2..912e814e7b 100644--- a/apps/web/lib/prisma.ts+++ b/apps/web/lib/prisma.ts@@ -4,7 +4,23 @@ declare global {var prisma: PrismaClient | undefined;}-const prisma = global.prisma || new PrismaClient();+const prisma =+ global.prisma ||+ (new PrismaClient().$extends({+ result: {+ link: {+ tagId: {+ // @ts-ignore https://github.com/prisma/prisma/issues/20091#issuecomment-1824063594+ needs: { tags: true },+ compute(link) {+ if (!link.tags || !link.tags.length) return null;++ return link.tags[0].tagId;+ },+ },+ },+ },+ }) as PrismaClient);if (process.env.NODE_ENV === "development") global.prisma = prisma;diff --git a/apps/web/prisma/schema.prisma b/apps/web/prisma/schema.prismaindex e3cb051f7f..1e6f75918b 100644--- a/apps/web/prisma/schema.prisma+++ b/apps/web/prisma/schema.prisma@@ -227,10 +227,6 @@ model Link {updatedAt DateTime @updatedAt// Link tags- tag Tag? @relation(fields: [tagId], references: [id])- tagId String?-- // (Upcoming) multiple link tagstags LinkTag[]// Comments on the particular shortlink@@ -241,7 +237,6 @@ model Link {@@unique([domain, key])@@index(projectId)@@index(domain)- @@index(tagId)@@index(proxy)@@index(password)@@index(createdAt(sort: Desc))@@ -260,8 +255,7 @@ model Tag {updatedAt DateTime @updatedAtproject Project @relation(fields: [projectId], references: [id], onUpdate: Cascade, onDelete: Cascade)projectId String- links Link[]- linksNew LinkTag[]+ links LinkTag[]@@unique([name, projectId])@@index(projectId)