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 "Update a tag" method to OpenAPI spec

Kiran K07ac0d12c986
diff --git a/apps/web/app/api/tags/[id]/route.ts b/apps/web/app/api/tags/[id]/route.ts
index 8b42bb51da..3c0d817803 100644
--- a/apps/web/app/api/tags/[id]/route.ts
+++ b/apps/web/app/api/tags/[id]/route.ts
@@ -2,7 +2,7 @@ import { DubApiError } from "@/lib/api/errors";
import { withWorkspace } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
import { recordLink } from "@/lib/tinybird";
-import { updateTagBodySchema } from "@/lib/zod/schemas/tags";
+import { TagSchema, updateTagBodySchema } from "@/lib/zod/schemas/tags";
import { NextResponse } from "next/server";
// PATCH /api/workspaces/[idOrSlug]/tags/[id] – update a tag for a workspace
@@ -35,7 +35,8 @@ export const PATCH = withWorkspace(
color,
},
});
- return NextResponse.json(response);
+
+ return NextResponse.json(TagSchema.parse(response));
} catch (error) {
if (error.code === "P2002") {
throw new DubApiError({
@@ -101,7 +102,7 @@ export const DELETE = withWorkspace(
})),
);
- return NextResponse.json(response);
+ return NextResponse.json({ id });
} catch (error) {
if (error.code === "P2025") {
throw new DubApiError({
diff --git a/apps/web/app/api/tags/route.ts b/apps/web/app/api/tags/route.ts
index 887d58867d..d01003942c 100644
--- a/apps/web/app/api/tags/route.ts
+++ b/apps/web/app/api/tags/route.ts
@@ -1,7 +1,7 @@
import { DubApiError, exceededLimitError } from "@/lib/api/errors";
import { withWorkspace } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
-import { createTagBodySchema } from "@/lib/zod/schemas/tags";
+import { TagSchema, createTagBodySchema } from "@/lib/zod/schemas/tags";
import { COLORS_LIST, randomBadgeColor } from "@/ui/links/tag-badge";
import { NextResponse } from "next/server";
@@ -16,11 +16,13 @@ export const GET = withWorkspace(
id: true,
name: true,
color: true,
+ createdAt: true,
},
orderBy: {
name: "asc",
},
});
+
return NextResponse.json(tags, { headers });
},
{
@@ -48,12 +50,12 @@ export const POST = withWorkspace(
});
}
- const { tag, color } = createTagBodySchema.parse(await req.json());
+ const { tag, color, name } = createTagBodySchema.parse(await req.json());
const existingTag = await prisma.tag.findFirst({
where: {
projectId: workspace.id,
- name: tag,
+ name: name || tag,
},
});
@@ -66,7 +68,7 @@ export const POST = withWorkspace(
const response = await prisma.tag.create({
data: {
- name: tag,
+ name: tag || name!,
color:
color && COLORS_LIST.map(({ color }) => color).includes(color)
? color
@@ -75,7 +77,10 @@ export const POST = withWorkspace(
},
});
- return NextResponse.json(response, { headers, status: 201 });
+ return NextResponse.json(TagSchema.parse(response), {
+ headers,
+ status: 201,
+ });
},
{
requiredScopes: ["tags.write"],
diff --git a/apps/web/lib/openapi/tags/index.ts b/apps/web/lib/openapi/tags/index.ts
index 2e52a289f2..be1c4ce9f6 100644
--- a/apps/web/lib/openapi/tags/index.ts
+++ b/apps/web/lib/openapi/tags/index.ts
@@ -2,10 +2,14 @@ import { ZodOpenApiPathsObject } from "zod-openapi";
import { createTag } from "./create-tag";
import { getTags } from "./get-tags";
+import { updateTag } from "./update-tag";
export const tagsPaths: ZodOpenApiPathsObject = {
"/tags": {
post: createTag,
get: getTags,
},
+ "/tags/{id}": {
+ patch: updateTag,
+ },
};
diff --git a/apps/web/lib/openapi/tags/update-tag.ts b/apps/web/lib/openapi/tags/update-tag.ts
new file mode 100644
index 0000000000..9c27507a5c
--- /dev/null
+++ b/apps/web/lib/openapi/tags/update-tag.ts
@@ -0,0 +1,38 @@
+import { openApiErrorResponses } from "@/lib/openapi/responses";
+import z from "@/lib/zod";
+import { TagSchema, updateTagBodySchema } from "@/lib/zod/schemas/tags";
+import { ZodOpenApiOperationObject } from "zod-openapi";
+import { workspaceParamsSchema } from "../request";
+
+export const updateTag: ZodOpenApiOperationObject = {
+ operationId: "updateTag",
+ "x-speakeasy-name-override": "update",
+ summary: "Update a tag",
+ description: "Update a tag in the workspace.",
+ requestParams: {
+ query: workspaceParamsSchema,
+ path: z.object({
+ id: z.string().describe("The ID of the tag"),
+ }),
+ },
+ requestBody: {
+ content: {
+ "application/json": {
+ schema: updateTagBodySchema,
+ },
+ },
+ },
+ responses: {
+ "201": {
+ description: "The updated tag.",
+ content: {
+ "application/json": {
+ schema: TagSchema,
+ },
+ },
+ },
+ ...openApiErrorResponses,
+ },
+ tags: ["Tags"],
+ security: [{ token: [] }],
+};
diff --git a/apps/web/lib/zod/schemas/tags.ts b/apps/web/lib/zod/schemas/tags.ts
index edaa067484..f54a17fe82 100644
--- a/apps/web/lib/zod/schemas/tags.ts
+++ b/apps/web/lib/zod/schemas/tags.ts
@@ -11,25 +11,38 @@ export const tagColorSchema = z
})
.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(
+export const createTagBodySchema = z
+ .object({
+ name: z.string().min(1).trim().describe("The name of the tag to create."),
+ color: tagColorSchema.describe(
`The color of the tag. If not provided, a random color will be used from the list: ${tagColors.join(", ")}.`,
),
-});
+ tag: z
+ .string()
+ .min(1)
+ .trim()
+ .describe("The name of the tag to create.")
+ .openapi({ deprecated: true }),
+ })
+ .partial()
+ .superRefine((data, ctx) => {
+ if (!data.name && !data.tag) {
+ ctx.addIssue({
+ code: z.ZodIssueCode.custom,
+ path: ["name"],
+ message: "Name is required.",
+ });
+ }
+ });
-export const updateTagBodySchema = z.object({
- name: z.string().min(1),
- color: tagColorSchema.optional(),
-});
+export const updateTagBodySchema = createTagBodySchema;
export const TagSchema = z
.object({
id: z.string().describe("The unique ID of the tag."),
name: z.string().describe("The name of the tag."),
color: tagColorSchema.describe("The color of the tag."),
+ createdAt: z.date().describe("The date the tag was created."),
})
.openapi({
title: "Tag",