The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Add missing validation to tags api
Kiran K6ce454dde467
diff --git a/apps/web/app/api/projects/[slug]/tags/[id]/route.ts b/apps/web/app/api/projects/[slug]/tags/[id]/route.tsindex 71e85ac83d..89187a8c13 100644--- a/apps/web/app/api/projects/[slug]/tags/[id]/route.ts+++ b/apps/web/app/api/projects/[slug]/tags/[id]/route.ts@@ -1,21 +1,33 @@import { withAuth } from "@/lib/auth";import prisma from "@/lib/prisma";import { NextResponse } from "next/server";+import z from "@/lib/zod";+import { tagColorSchema } from "@/lib/zod/schemas/tags";+import { handleAndReturnErrorResponse } from "@/lib/errors";++const updateTagSchema = z.object({+ name: z.string().min(1),+ color: tagColorSchema,+});// PUT /api/projects/[slug]/tags/[id] – update a tag for a projectexport const PUT = withAuth(async ({ req, params }) => {const { id } = params;- const { name, color } = await req.json();- const response = await prisma.tag.update({- where: {- id,- },- data: {- name,- color,- },- });- return NextResponse.json(response);+ try {+ const { name, color } = updateTagSchema.parse(await req.json());+ const response = await prisma.tag.update({+ where: {+ id,+ },+ data: {+ name,+ color,+ },+ });+ return NextResponse.json(response);+ } catch (error) {+ return handleAndReturnErrorResponse(error);+ }});// DELETE /api/projects/[slug]/tags/[id] – delete a tag for a projectdiff --git a/apps/web/app/api/projects/[slug]/tags/route.ts b/apps/web/app/api/projects/[slug]/tags/route.tsindex eaaca090d2..3f47eade8a 100644--- a/apps/web/app/api/projects/[slug]/tags/route.ts+++ b/apps/web/app/api/projects/[slug]/tags/route.ts@@ -8,24 +8,20 @@ import { createTagBodySchema } from "@/lib/zod/schemas/tags";// GET /api/projects/[slug]/tags - get all tags for a projectexport const GET = withAuth(async ({ project, headers }) => {- try {- const tags = await prisma.tag.findMany({- where: {- projectId: project.id,- },- select: {- id: true,- name: true,- color: true,- },- orderBy: {- name: "asc",- },- });- return NextResponse.json(tags, { headers });- } catch (err) {- return handleAndReturnErrorResponse(err);- }+ const tags = await prisma.tag.findMany({+ where: {+ projectId: project.id,+ },+ select: {+ id: true,+ name: true,+ color: true,+ },+ orderBy: {+ name: "asc",+ },+ });+ return NextResponse.json(tags, { headers });});// POST /api/projects/[slug]/tags - create a tag for a projectdiff --git a/apps/web/lib/types.ts b/apps/web/lib/types.tsindex 520f8d7e91..abcf558d72 100644--- a/apps/web/lib/types.ts+++ b/apps/web/lib/types.ts@@ -1,8 +1,9 @@import { DirectorySyncProviders } from "@boxyhq/saml-jackson";import z from "@/lib/zod";-import { createLinkBodySchema } from "./zod/schemas/links";import { PlanSchema } from "prisma/zod";+import { createLinkBodySchema } from "./zod/schemas/links";+import { tagColorSchema } from "./zod/schemas/tags";export { type Link as LinkProps } from "@prisma/client";export interface SimpleLinkProps {@@ -37,14 +38,7 @@ export interface TagProps {color: TagColorProps;}-export type TagColorProps =- | "red"- | "yellow"- | "green"- | "blue"- | "purple"- | "pink"- | "brown";+export type TagColorProps = z.infer<typeof tagColorSchema>;export type PlanProps = z.infer<typeof PlanSchema>;diff --git a/apps/web/lib/zod/schemas/tags.ts b/apps/web/lib/zod/schemas/tags.tsindex 26e0cc8ab6..ed529222ca 100644--- a/apps/web/lib/zod/schemas/tags.ts+++ b/apps/web/lib/zod/schemas/tags.ts@@ -13,3 +13,13 @@ export const tagSchema = z.openapi({title: "Tag",});++export const tagColorSchema = z.enum([+ "red",+ "yellow",+ "green",+ "blue",+ "purple",+ "pink",+ "brown",+]);diff --git a/apps/web/ui/links/link-filters.tsx b/apps/web/ui/links/link-filters.tsxindex ca10c363a1..238ae5ca2d 100644--- a/apps/web/ui/links/link-filters.tsx+++ b/apps/web/ui/links/link-filters.tsx@@ -34,6 +34,7 @@ import { useCallback, useEffect, useMemo, useRef, useState } from "react";import { toast } from "sonner";import { mutate } from "swr";import { useDebouncedCallback } from "use-debounce";+import { extractApiErrorMessage } from "../utils";export default function LinkFilters() {const { data: domains } = useLinksCount({ groupBy: "domain" });@@ -403,7 +404,8 @@ const TagPopover = ({ tag, count }: { tag: TagProps; count: number }) => {await mutate(`/api/projects/${slug}/tags`);toast.success("Tag updated");} else {- toast.error("Something went wrong");+ const error = await extractApiErrorMessage(res);+ toast.error(error);}});};