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 body schema and remove unused code

Kiran Kf25cd4d22f26
diff --git a/apps/web/app/api/links/[linkId]/route.ts b/apps/web/app/api/links/[linkId]/route.ts
index 33d4cba5dc..7be3de8e74 100644
--- a/apps/web/app/api/links/[linkId]/route.ts
+++ b/apps/web/app/api/links/[linkId]/route.ts
@@ -7,7 +7,7 @@ import {
handleAndReturnErrorResponse,
} from "@/lib/errors";
import { LinkProps } from "@/lib/types";
-import { createLinkBodySchema } from "@/lib/zod/schemas/links";
+import { updateLinkBodySchema } from "@/lib/zod/schemas/links";
import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
import { NextResponse } from "next/server";
@@ -22,7 +22,7 @@ export const GET = withAuth(async ({ headers, link }) => {
// PUT /api/links/[linkId] – update a link
export const PUT = withAuth(async ({ req, headers, project, link }) => {
try {
- const body = createLinkBodySchema.parse(await req.json());
+ const body = updateLinkBodySchema.parse(await req.json());
const updatedLink = {
...link,
diff --git a/apps/web/app/api/openapi/route.ts b/apps/web/app/api/openapi/route.ts
deleted file mode 100644
index 89726be824..0000000000
--- a/apps/web/app/api/openapi/route.ts
+++ /dev/null
@@ -1,8 +0,0 @@
-import { NextResponse } from "next/server";
-import { createDocument } from "zod-openapi";
-
-import { openApiObject } from "@/lib/openapi";
-
-export const GET = async () => {
- return NextResponse.json(createDocument(openApiObject));
-};
diff --git a/apps/web/app/api/route.ts b/apps/web/app/api/route.ts
index fd29591348..f5253384e3 100644
--- a/apps/web/app/api/route.ts
+++ b/apps/web/app/api/route.ts
@@ -1,9 +1,13 @@
import { NextResponse } from "next/server";
-import { OpenAPIV3 } from "openapi-types";
+import { createDocument } from "zod-openapi";
+import { openApiObject } from "@/lib/openapi";
export const runtime = "edge";
-export function GET(): NextResponse<OpenAPIV3.Document> {
+export function GET() {
+ return NextResponse.json(createDocument(openApiObject));
+
+ // Remove this after the above code is merged
return NextResponse.json({
openapi: "3.0.3",
info: {
diff --git a/apps/web/lib/errors.ts b/apps/web/lib/errors.ts
index 53a2745952..27ccd16db0 100644
--- a/apps/web/lib/errors.ts
+++ b/apps/web/lib/errors.ts
@@ -75,7 +75,7 @@ const docErrorUrl = "https://dub.co/docs/api-reference/errors";
export function fromZodError(error: ZodError): ErrorResponse {
return {
error: {
- code: "bad_request",
+ code: "unprocessable_entity",
message: generateErrorMessage(error.issues, {
maxErrors: 1,
delimiter: {
@@ -95,7 +95,7 @@ export function fromZodError(error: ZodError): ErrorResponse {
label: "",
},
}),
- doc_url: `${docErrorUrl}#bad_request`,
+ doc_url: `${docErrorUrl}#unprocessable_entity`,
},
};
}
@@ -103,7 +103,10 @@ export function fromZodError(error: ZodError): ErrorResponse {
export function handleApiError(error: any): ErrorResponse & { status: number } {
// Zod errors
if (error instanceof ZodError) {
- return { ...fromZodError(error), status: 400 };
+ return {
+ ...fromZodError(error),
+ status: errorCodeToHttpStatus.unprocessable_entity,
+ };
}
// DubApiError errors
diff --git a/apps/web/lib/zod/schemas/links.ts b/apps/web/lib/zod/schemas/links.ts
index 3d43d49950..afa9ef12cb 100644
--- a/apps/web/lib/zod/schemas/links.ts
+++ b/apps/web/lib/zod/schemas/links.ts
@@ -49,16 +49,19 @@ export const getLinksQuerySchema = z.object({
export const getLinkInfoQuerySchema = z.object({
projectSlug: z
.string()
+ .min(1, "Project slug is required.")
.describe(
"The slug for the project that the link belongs to. E.g. for `app.dub.co/acme`, the projectSlug is `acme`.",
),
domain: z
.string()
+ .min(1, "Domain is required.")
.describe(
"The domain of the link to retrieve. E.g. for `dub.sh/github`, the domain is `dub.sh`.",
),
key: z
.string()
+ .min(1, "Key is required.")
.describe(
"The key of the link to retrieve. E.g. for `dub.sh/github`, the key is `github`.",
),
@@ -164,6 +167,8 @@ export const createLinkBodySchema = z.object({
comments: z.string().nullish().describe("The comments for the short link."),
});
+export const updateLinkBodySchema = createLinkBodySchema.partial();
+
export const bulkCreateLinksBodySchema = z
.array(createLinkBodySchema)
.max(100, "You can only create up to 100 links at a time.");