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.

Update link and tag types

Kiran K4a39f79b84d4
diff --git a/apps/web/app/api/links/[linkId]/route.ts b/apps/web/app/api/links/[linkId]/route.ts
index 7be3de8e74..6534b0240c 100644
--- a/apps/web/app/api/links/[linkId]/route.ts
+++ b/apps/web/app/api/links/[linkId]/route.ts
@@ -6,7 +6,7 @@ import {
ErrorCodes,
handleAndReturnErrorResponse,
} from "@/lib/errors";
-import { LinkProps } from "@/lib/types";
+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";
@@ -41,7 +41,7 @@ export const PUT = withAuth(async ({ req, headers, project, link }) => {
error,
code,
} = await processLink({
- payload: updatedLink as LinkProps,
+ payload: updatedLink as LinkWithTagIdsProps,
project,
});
diff --git a/apps/web/app/api/links/route.ts b/apps/web/app/api/links/route.ts
index ff404a2063..8c1603abde 100644
--- a/apps/web/app/api/links/route.ts
+++ b/apps/web/app/api/links/route.ts
@@ -6,7 +6,7 @@ import {
ErrorCodes,
handleAndReturnErrorResponse,
} from "@/lib/errors";
-import { LinkProps } from "@/lib/types";
+import { LinkWithTagIdsProps } from "@/lib/types";
import { ratelimit } from "@/lib/upstash";
import {
createLinkBodySchema,
@@ -68,7 +68,7 @@ export const POST = withAuth(
}
const { link, error, code } = await processLink({
- payload: body as LinkProps,
+ payload: body as LinkWithTagIdsProps,
project,
userId: session.user.id,
});
diff --git a/apps/web/app/api/tags/[id]/route.ts b/apps/web/app/api/tags/[id]/route.ts
index 3f293c4014..098e595587 100644
--- a/apps/web/app/api/tags/[id]/route.ts
+++ b/apps/web/app/api/tags/[id]/route.ts
@@ -1,5 +1,5 @@
import { withAuth } from "@/lib/auth";
-import { handleAndReturnErrorResponse } from "@/lib/errors";
+import { DubApiError, handleAndReturnErrorResponse } from "@/lib/errors";
import prisma from "@/lib/prisma";
import { updateTagBodySchema } from "@/lib/zod/schemas/tags";
import { NextResponse } from "next/server";
@@ -9,6 +9,20 @@ export const PUT = withAuth(async ({ req, params }) => {
const { id } = params;
try {
const { name, color } = updateTagBodySchema.parse(await req.json());
+
+ const tag = await prisma.tag.findUnique({
+ where: {
+ id,
+ },
+ });
+
+ if (!tag) {
+ throw new DubApiError({
+ code: "not_found",
+ message: "Tag not found.",
+ });
+ }
+
const response = await prisma.tag.update({
where: {
id,
diff --git a/apps/web/lib/zod/schemas/links.ts b/apps/web/lib/zod/schemas/links.ts
index afa9ef12cb..8a99cc5525 100644
--- a/apps/web/lib/zod/schemas/links.ts
+++ b/apps/web/lib/zod/schemas/links.ts
@@ -163,7 +163,11 @@ export const createLinkBodySchema = z.object({
tagId: z
.string()
.nullish()
- .describe("The tag ID to assign to the short link."),
+ .describe("The unique ID of the tag assigned to the short link."),
+ tagIds: z
+ .array(z.string())
+ .nullish()
+ .describe("The unique IDs of the tags assigned to the short link."),
comments: z.string().nullish().describe("The comments for the short link."),
});
diff --git a/apps/web/lib/zod/schemas/tags.ts b/apps/web/lib/zod/schemas/tags.ts
index b483a3de63..f9d7cd3623 100644
--- a/apps/web/lib/zod/schemas/tags.ts
+++ b/apps/web/lib/zod/schemas/tags.ts
@@ -2,16 +2,27 @@ import { tagColors } from "@/lib/types";
import z from "@/lib/zod";
export const tagColorSchema = z
- .enum(tagColors)
- .describe("The color of the tag.");
+ .enum(tagColors, {
+ errorMap: (issue, ctx) => {
+ return {
+ message: `Invalid color. Must be one of: ${tagColors.join(", ")}`,
+ };
+ },
+ })
+ .describe("The color of the tag");
export const createTagBodySchema = z.object({
tag: z.string().min(1).describe("The name of the tag to create."),
+ color: tagColorSchema
+ .optional()
+ .describe(
+ `The color of the tag. If not provided, a random color will be used from the list: ${tagColors.join(", ")}.`,
+ ),
});
export const updateTagBodySchema = z.object({
name: z.string().min(1),
- color: tagColorSchema,
+ color: tagColorSchema.optional(),
});
export const tagSchema = z