The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
add identifier to API
Kiran K8c8b50d3c31b
diff --git a/apps/web/app/api/links/[linkId]/route.ts b/apps/web/app/api/links/[linkId]/route.tsindex 763aa25db4..2c83265bca 100644--- a/apps/web/app/api/links/[linkId]/route.ts+++ b/apps/web/app/api/links/[linkId]/route.ts@@ -101,6 +101,15 @@ export const PATCH = withWorkspace(});}+ // if domain and key are the same, we don't need to check if the key exists+ const skipKeyChecks =+ link.domain === updatedLink.domain &&+ link.key.toLowerCase() === updatedLink.key?.toLowerCase();++ // if identifier is the same, we don't need to check if it exists+ const skipIdentifierChecks =+ link.identifier?.toLowerCase() === updatedLink.identifier?.toLowerCase();+const {link: processedLink,error,@@ -108,10 +117,8 @@ export const PATCH = withWorkspace(} = await processLink({payload: updatedLink,workspace,- // if domain and key are the same, we don't need to check if the key exists- skipKeyChecks:- link.domain === updatedLink.domain &&- link.key.toLowerCase() === updatedLink.key?.toLowerCase(),+ skipKeyChecks,+ skipIdentifierChecks,});if (error) {diff --git a/apps/web/app/api/links/bulk/route.ts b/apps/web/app/api/links/bulk/route.tsindex 22c9e892b4..5c5f821ee3 100644--- a/apps/web/app/api/links/bulk/route.ts+++ b/apps/web/app/api/links/bulk/route.ts@@ -68,6 +68,7 @@ export const POST = withWorkspace(workspace,userId: session.user.id,bulk: true,+ skipIdentifierChecks: true,}),),);@@ -232,6 +233,7 @@ export const PATCH = withWorkspace(userId: link.userId ?? undefined,bulk: true,skipKeyChecks: true,+ skipIdentifierChecks: true,}),),);diff --git a/apps/web/app/api/links/upsert/route.ts b/apps/web/app/api/links/upsert/route.tsindex d64f1cdfd5..f1654ea78e 100644--- a/apps/web/app/api/links/upsert/route.ts+++ b/apps/web/app/api/links/upsert/route.ts@@ -75,6 +75,16 @@ export const PUT = withWorkspace(});}+ // if domain and key are the same, we don't need to check if the key exists+ const skipKeyChecks =+ link.domain === updatedLink.domain &&+ link.key.toLowerCase() === updatedLink.key?.toLowerCase();++ // if identifier is the same, we don't need to check if it exists+ const skipIdentifierChecks =+ link.identifier?.toLowerCase() ===+ updatedLink.identifier?.toLowerCase();+const {link: processedLink,error,@@ -82,10 +92,8 @@ export const PUT = withWorkspace(} = await processLink({payload: updatedLink,workspace,- // if domain and key are the same, we don't need to check if the key exists- skipKeyChecks:- link.domain === updatedLink.domain &&- link.key.toLowerCase() === updatedLink.key?.toLowerCase(),+ skipKeyChecks,+ skipIdentifierChecks,});if (error) {diff --git a/apps/web/lib/api/links/process-link.ts b/apps/web/lib/api/links/process-link.tsindex ec7837d212..4f5d518447 100644--- a/apps/web/lib/api/links/process-link.ts+++ b/apps/web/lib/api/links/process-link.ts@@ -25,12 +25,14 @@ export async function processLink<T extends Record<string, any>>({userId,bulk = false,skipKeyChecks = false, // only skip when key doesn't change (e.g. when editing a link)+ skipIdentifierChecks = false, // only skip when identifier doesn't change (e.g. when editing a link)}: {payload: NewLinkProps & T;workspace?: Pick<WorkspaceProps, "id" | "plan" | "conversionEnabled">;userId?: string;bulk?: boolean;skipKeyChecks?: boolean;+ skipIdentifierChecks?: boolean;}): Promise<| {link: NewLinkProps & T;@@ -60,6 +62,7 @@ export async function processLink<T extends Record<string, any>>({geo,doIndex,tagNames,+ identifier,} = payload;let expiresAt: string | Date | null | undefined = payload.expiresAt;@@ -257,6 +260,25 @@ export async function processLink<T extends Record<string, any>>({}}+ if (identifier && workspace && !skipIdentifierChecks) {+ const link = await prisma.link.findUnique({+ where: {+ projectId_identifier: {+ projectId: workspace.id,+ identifier,+ },+ },+ });++ if (link) {+ return {+ link: payload,+ error: "The identifier is already taken.",+ code: "unprocessable_entity",+ };+ }+ }+if (bulk) {if (proxy && image && !isStored(image)) {return {diff --git a/apps/web/lib/zod/schemas/links.ts b/apps/web/lib/zod/schemas/links.tsindex 38d2f7a3cf..9f50b4fedf 100644--- a/apps/web/lib/zod/schemas/links.ts+++ b/apps/web/lib/zod/schemas/links.ts@@ -171,6 +171,12 @@ export const createLinkBodySchema = z.object({.optional().default(false).describe("Whether to track conversions for the short link."),+ identifier: z+ .string()+ .nullish()+ .describe(+ "The identifier of the short link that is unique across your workspace. If set, it can be used to identify your short link for client-side click tracking.",+ ),archived: z.boolean().optional()diff --git a/apps/web/prisma/schema/link.prisma b/apps/web/prisma/schema/link.prismaindex 3d6f5b3595..912cec4a52 100644--- a/apps/web/prisma/schema/link.prisma+++ b/apps/web/prisma/schema/link.prisma@@ -1,15 +1,15 @@model Link {- id String @id @default(cuid())- domain String // domain of the link (e.g. dub.sh) – also stored on Redis- key String // key of the link (e.g. /github) – also stored on Redis- url String @db.LongText // target url (e.g. https://github.com/dubinc/dub) – also stored on Redis- archived Boolean @default(false) // whether the link is archived or not- expiresAt DateTime? // when the link expires – stored on Redis via ttl- expiredUrl String? @db.LongText // URL to redirect the user to when the link is expired- password String? // password to access the link- externalId String?- identifier String?- trackConversion Boolean @default(false) // whether to track conversions or not+ id String @id @default(cuid())+ domain String // domain of the link (e.g. dub.sh) – also stored on Redis+ key String // key of the link (e.g. /github) – also stored on Redis+ url String @db.LongText // target url (e.g. https://github.com/dubinc/dub) – also stored on Redis+ archived Boolean @default(false) // whether the link is archived or not+ expiresAt DateTime? // when the link expires – stored on Redis via ttl+ expiredUrl String? @db.LongText // URL to redirect the user to when the link is expired+ password String? // password to access the link+ externalId String?+ identifier String?+ trackConversion Boolean @default(false) // whether to track conversions or notproxy Boolean @default(false) // Proxy to use custom OG tags (stored on redis) – if false, will use OG tags from target urltitle String? // OG title for the link (e.g. Dub.co - Open-Source Link Management Infrastructure)