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.

add `externalId` to link

Kiran K170cf78ae745
diff --git a/apps/web/app/api/links/route.ts b/apps/web/app/api/links/route.ts
index abbdbd55d3..466072c314 100644
--- a/apps/web/app/api/links/route.ts
+++ b/apps/web/app/api/links/route.ts
@@ -1,5 +1,6 @@
import { DubApiError, ErrorCodes } from "@/lib/api/errors";
import { createLink, getLinksForWorkspace, processLink } from "@/lib/api/links";
+import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
import { ratelimit } from "@/lib/upstash";
import { createLinkBodySchema, getLinksQuerySchema } from "@/lib/zod/schemas";
@@ -43,15 +44,7 @@ export const GET = withWorkspace(async ({ req, headers, workspace }) => {
// POST /api/links – create a new link
export const POST = withWorkspace(
async ({ req, headers, session, workspace }) => {
- let bodyRaw;
- try {
- bodyRaw = await req.json();
- } catch (error) {
- throw new DubApiError({
- code: "bad_request",
- message: "Invalid body – body must be a valid JSON.",
- });
- }
+ const bodyRaw = await parseRequestBody(req);
const body = createLinkBodySchema.parse(bodyRaw);
if (!session) {
diff --git a/apps/web/lib/auth/workspace.ts b/apps/web/lib/auth/workspace.ts
index cc4fe175b0..6ef318609b 100644
--- a/apps/web/lib/auth/workspace.ts
+++ b/apps/web/lib/auth/workspace.ts
@@ -221,7 +221,9 @@ export const withWorkspace = (
linkId
? prisma.link.findUnique({
where: {
- id: linkId,
+ ...(linkId.startsWith("ext_")
+ ? { externalId: linkId.replace("ext_", "") }
+ : { id: linkId }),
},
})
: domain &&
diff --git a/apps/web/lib/zod/schemas/links.ts b/apps/web/lib/zod/schemas/links.ts
index c87291bdc2..7934d70952 100644
--- a/apps/web/lib/zod/schemas/links.ts
+++ b/apps/web/lib/zod/schemas/links.ts
@@ -117,6 +117,11 @@ export const domainKeySchema = z.object({
});
export const createLinkBodySchema = z.object({
+ url: parseUrlSchema
+ .describe("The destination URL of the short link.")
+ .openapi({
+ example: "https://google/com",
+ }),
domain: z
.string()
.optional()
@@ -129,17 +134,21 @@ export const createLinkBodySchema = z.object({
.describe(
"The short link slug. If not provided, a random 7-character slug will be generated.",
),
+ externalId: z
+ .string()
+ .min(1)
+ .max(255)
+ .nullish()
+ .describe(
+ "This is the ID of the link in the your system. If set, it will be used to identify the link in the future.",
+ )
+ .openapi({ example: "123456" }),
prefix: z
.string()
.optional()
.describe(
"The prefix of the short link slug for randomly-generated keys (e.g. if prefix is `/c/`, generated keys will be in the `/c/:key` format). Will be ignored if `key` is provided.",
),
- url: parseUrlSchema
- .describe("The destination URL of the short link.")
- .openapi({
- example: "https://google/com",
- }),
archived: z
.boolean()
.optional()
@@ -250,6 +259,12 @@ export const LinkSchema = z
.describe(
"The short link slug. If not provided, a random 7-character slug will be generated.",
),
+ externalId: z
+ .string()
+ .nullable()
+ .describe(
+ "This is the ID of the link in the your system. If set, it will be used to identify the link in the future.",
+ ),
url: z.string().url().describe("The destination URL of the short link."),
archived: z
.boolean()
diff --git a/apps/web/prisma/schema.prisma b/apps/web/prisma/schema.prisma
index 38178bb258..1005eedc69 100644
--- a/apps/web/prisma/schema.prisma
+++ b/apps/web/prisma/schema.prisma
@@ -211,6 +211,7 @@ model Link {
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? @unique // external ID for the link (e.g. 12345)
proxy Boolean @default(false) // Proxy to use custom OG tags (stored on redis) – if false, will use OG tags from target url
title String? // OG title for the link (e.g. Dub.co - Open-Source Link Management Infrastructure)