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.

Refactor domain route and archive route in web app API

Kiran K612c7de56c8f
diff --git a/apps/web/app/api/domains/[domain]/archive/route.ts b/apps/web/app/api/domains/[domain]/archive/route.ts
deleted file mode 100644
index d75e14db60..0000000000
--- a/apps/web/app/api/domains/[domain]/archive/route.ts
+++ /dev/null
@@ -1,27 +0,0 @@
-import { archiveDomain } from "@/lib/api/domains";
-import { withAuth } from "@/lib/auth";
-import { NextResponse } from "next/server";
-
-// POST /api/domains/[domain]/archive – archive a domain
-export const POST = withAuth(
- async ({ headers, domain }) => {
- const response = await archiveDomain({ domain, archived: true });
- return NextResponse.json(response, { headers });
- },
- {
- domainChecks: true,
- requiredRole: ["owner"],
- },
-);
-
-// DELETE /api/domains/[domain]/archive – unarchive a domain
-export const DELETE = withAuth(
- async ({ headers, domain }) => {
- const response = await archiveDomain({ domain, archived: false });
- return NextResponse.json(response, { headers });
- },
- {
- domainChecks: true,
- requiredRole: ["owner"],
- },
-);
diff --git a/apps/web/app/api/domains/[domain]/route.ts b/apps/web/app/api/domains/[domain]/route.ts
index 50cdc37ee9..6ba3c08c06 100644
--- a/apps/web/app/api/domains/[domain]/route.ts
+++ b/apps/web/app/api/domains/[domain]/route.ts
@@ -8,7 +8,7 @@ import {
import { DubApiError } from "@/lib/api/errors";
import { withAuth } from "@/lib/auth";
import prisma from "@/lib/prisma";
-import { updateDomainBodySchema } from "@/lib/zod/schemas";
+import { DomainSchema, updateDomainBodySchema } from "@/lib/zod/schemas";
import { NextResponse } from "next/server";
// GET /api/domains/[domain] – get a workspace's domain
@@ -46,7 +46,7 @@ export const GET = withAuth(
);
// PUT /api/domains/[domain] – edit a workspace's domain
-export const PUT = withAuth(
+export const PATCH = withAuth(
async ({ req, workspace, domain }) => {
const {
slug: newDomain,
@@ -54,9 +54,10 @@ export const PUT = withAuth(
type,
placeholder,
expiredUrl,
+ archived,
} = updateDomainBodySchema.parse(await req.json());
- if (newDomain !== domain) {
+ if (newDomain && newDomain !== domain) {
const validDomain = await validateDomain(newDomain);
if (validDomain !== true) {
throw new DubApiError({
@@ -79,15 +80,13 @@ export const PUT = withAuth(
slug: domain,
},
data: {
- ...(newDomain !== domain && {
- slug: newDomain,
- }),
+ slug: newDomain,
type,
- placeholder,
- // only set target and expiredUrl if the workspace is not free
+ archived,
+ ...(placeholder && { placeholder }),
...(workspace.plan !== "free" && {
- target: target || null,
- expiredUrl: expiredUrl || null,
+ target,
+ expiredUrl,
}),
},
}),
@@ -108,7 +107,7 @@ export const PUT = withAuth(
projectId: workspace.id,
});
- return NextResponse.json(response);
+ return NextResponse.json(DomainSchema.parse(response[0]));
},
{
domainChecks: true,
diff --git a/apps/web/app/api/domains/route.ts b/apps/web/app/api/domains/route.ts
index bd3bc62e32..830e9afa25 100644
--- a/apps/web/app/api/domains/route.ts
+++ b/apps/web/app/api/domains/route.ts
@@ -76,7 +76,7 @@ export const POST = withAuth(async ({ req, workspace }) => {
type,
projectId: workspace.id,
primary: workspace.domains.length === 0,
- placeholder,
+ ...(placeholder && { placeholder }),
...(workspace.plan !== "free" && {
target,
expiredUrl,
diff --git a/apps/web/lib/api/errors.ts b/apps/web/lib/api/errors.ts
index 2db72c4b17..07d0ab331c 100644
--- a/apps/web/lib/api/errors.ts
+++ b/apps/web/lib/api/errors.ts
@@ -118,6 +118,8 @@ export function fromZodError(error: ZodError): ErrorResponse {
}
export function handleApiError(error: any): ErrorResponse & { status: number } {
+ console.error("API error occurred", error);
+
// Zod errors
if (error instanceof ZodError) {
return {
@@ -139,10 +141,12 @@ export function handleApiError(error: any): ErrorResponse & { status: number } {
}
// Fallback
+ // Unhandled errors are not user-facing, so we don't expose the actual error
return {
error: {
code: "internal_server_error",
- message: error instanceof Error ? error.message : "Internal Server Error",
+ message:
+ "An internal server error occurred. Please contact our support if the problem persists.",
doc_url: `${docErrorUrl}#internal_server_error`,
},
status: 500,
diff --git a/apps/web/lib/zod/schemas/domains.ts b/apps/web/lib/zod/schemas/domains.ts
index fb4408e01b..c81c2cb170 100644
--- a/apps/web/lib/zod/schemas/domains.ts
+++ b/apps/web/lib/zod/schemas/domains.ts
@@ -67,14 +67,26 @@ export const addDomainBodySchema = z.object({
"Redirect users to a specific URL when any link under this domain has expired.",
),
placeholder: parseUrlSchema
- .optional()
+ .nullish()
.default("https://dub.co/help/article/what-is-dub")
.describe(
"Provide context to your teammates in the link creation modal by showing them an example of a link to be shortened.",
),
});
-export const updateDomainBodySchema = addDomainBodySchema;
+export const updateDomainBodySchema = addDomainBodySchema
+ .merge(
+ z.object({
+ archived: z
+ .boolean()
+ .optional()
+ .default(false)
+ .describe(
+ "Whether to archive this repository. `false` will unarchive a previously archived domain.",
+ ),
+ }),
+ )
+ .partial();
export const transferDomainBodySchema = z.object({
newWorkspaceId: z
diff --git a/apps/web/ui/modals/add-edit-domain-modal/index.tsx b/apps/web/ui/modals/add-edit-domain-modal/index.tsx
index abe506be91..0db4192722 100644
--- a/apps/web/ui/modals/add-edit-domain-modal/index.tsx
+++ b/apps/web/ui/modals/add-edit-domain-modal/index.tsx
@@ -87,7 +87,7 @@ function AddEditDomainModal({
const endpoint = useMemo(() => {
if (props) {
return {
- method: "PUT",
+ method: "PATCH",
url: `/api/domains/${domain}?workspaceId=${id}`,
successMessage: "Successfully updated domain!",
};
@@ -132,7 +132,12 @@ function AddEditDomainModal({
headers: {
"Content-Type": "application/json",
},
- body: JSON.stringify(data),
+ body: JSON.stringify({
+ ...data,
+ target: target || null,
+ placeholder: placeholder || null,
+ expiredUrl: expiredUrl || null,
+ }),
}).then(async (res) => {
if (res.ok) {
await mutate(`/api/domains?workspaceId=${id}`);
diff --git a/apps/web/ui/modals/archive-domain-modal.tsx b/apps/web/ui/modals/archive-domain-modal.tsx
index ef659e4a0a..2ddac97e60 100644
--- a/apps/web/ui/modals/archive-domain-modal.tsx
+++ b/apps/web/ui/modals/archive-domain-modal.tsx
@@ -22,12 +22,13 @@ const sendArchiveRequest = ({
archive: boolean;
workspaceId?: string;
}) => {
- const baseUrl = `/api/domains/${domain}/archive`;
+ const baseUrl = `/api/domains/${domain}`;
return fetch(`${baseUrl}?workspaceId=${workspaceId}`, {
- method: archive ? "POST" : "DELETE",
+ method: "PATCH",
headers: {
"Content-Type": "application/json",
},
+ body: JSON.stringify({ archived: archive }),
});
};