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 domain

Kiran Kb7c3ad2f51ed
diff --git a/apps/web/app/api/domains/[domain]/route.ts b/apps/web/app/api/domains/[domain]/route.ts
index b0587ce3ec..cd2a92183f 100644
--- a/apps/web/app/api/domains/[domain]/route.ts
+++ b/apps/web/app/api/domains/[domain]/route.ts
@@ -6,14 +6,13 @@ import {
validateDomain,
} from "@/lib/api/domains";
import { transformDomain } from "@/lib/api/domains/transform-domain";
-import { DubApiError } from "@/lib/api/errors";
+import { DubApiError, ErrorCodes } from "@/lib/api/errors";
+import { processLink, updateLink } from "@/lib/api/links";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
-import {
- DomainSchema,
- updateDomainBodySchema,
-} from "@/lib/zod/schemas/domains";
+import { NewLinkProps } from "@/lib/types";
+import { updateDomainBodySchema } from "@/lib/zod/schemas/domains";
import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
@@ -60,6 +59,8 @@ export const GET = withWorkspace(
export const PATCH = withWorkspace(
async ({ req, workspace, domain }) => {
const body = await parseRequestBody(req);
+ const payload = updateDomainBodySchema.parse(body);
+
const {
slug: newDomain,
target,
@@ -68,7 +69,7 @@ export const PATCH = withWorkspace(
expiredUrl,
archived,
noindex,
- } = updateDomainBodySchema.parse(body);
+ } = payload;
if (newDomain && newDomain !== domain) {
const validDomain = await validateDomain(newDomain);
@@ -87,7 +88,7 @@ export const PATCH = withWorkspace(
}
}
- const response = await prisma.domain.update({
+ const domainRecord = await prisma.domain.update({
where: {
slug: domain,
},
@@ -95,20 +96,67 @@ export const PATCH = withWorkspace(
slug: newDomain,
archived,
...(placeholder && { placeholder }),
- // ...(workspace.plan !== "free" && {
- // target,
- // expiredUrl,
- // noindex: noindex === undefined ? true : noindex,
- // }),
},
+ include: {
+ links: {
+ take: 1,
+ },
+ },
+ });
+
+ // TODO:
+ // Store noindex
+
+ const link = domainRecord.links[0];
+
+ const updatedLink = {
+ ...link,
+ expiresAt:
+ link.expiresAt instanceof Date
+ ? link.expiresAt.toISOString()
+ : link.expiresAt,
+ geo: link.geo as NewLinkProps["geo"],
+ ...(workspace.plan != "free" && {
+ ...("rewrite" in payload && { rewrite: type === "rewrite" }),
+ ...("target" in payload && { url: target || "" }),
+ ...("expiredUrl" in payload && { expiredUrl }),
+ }),
+ };
+
+ const {
+ link: processedLink,
+ error,
+ code,
+ } = await processLink({
+ payload: updatedLink,
+ workspace,
+ skipKeyChecks: true,
+ });
+
+ if (error != null) {
+ throw new DubApiError({
+ code: code as ErrorCodes,
+ message: error,
+ });
+ }
+
+ const response = await updateLink({
+ oldDomain: link.domain,
+ oldKey: link.key,
+ updatedLink: processedLink,
+ });
+
+ const result = transformDomain({
+ ...domainRecord,
+ ...response,
});
waitUntil(
Promise.all([
setRootDomain({
- id: response.id,
+ id: domainRecord.id,
domain,
- domainCreatedAt: response.createdAt,
+ domainCreatedAt: domainRecord.createdAt,
...(workspace.plan !== "free" && {
url: target || undefined,
noindex: noindex === undefined ? true : noindex,
@@ -124,7 +172,7 @@ export const PATCH = withWorkspace(
]),
);
- return NextResponse.json(DomainSchema.parse(response));
+ return NextResponse.json(result);
},
{
domainChecks: true,
diff --git a/apps/web/app/api/domains/route.ts b/apps/web/app/api/domains/route.ts
index 940c99f2cd..0ccecc4ef4 100644
--- a/apps/web/app/api/domains/route.ts
+++ b/apps/web/app/api/domains/route.ts
@@ -104,17 +104,17 @@ export const POST = withWorkspace(async ({ req, workspace, session }) => {
id: domainRecord.id,
domain: domainRecord.slug,
key: "_root",
- rewrite: type === "rewrite",
createdAt: domainRecord.createdAt,
archived: false,
proxy: false,
publicStats: false,
trackConversion: false,
...(workspace.plan === "free"
- ? { url: "", expiredUrl: null }
+ ? { url: "", expiredUrl: null, rewrite: false }
: {
url: target || "",
expiredUrl: expiredUrl || null,
+ rewrite: type === "rewrite",
}),
},
workspace,
diff --git a/apps/web/lib/api/errors.ts b/apps/web/lib/api/errors.ts
index 0a337ce0a6..bcce883c9a 100644
--- a/apps/web/lib/api/errors.ts
+++ b/apps/web/lib/api/errors.ts
@@ -118,7 +118,7 @@ export function fromZodError(error: ZodError): ErrorResponse {
}
export function handleApiError(error: any): ErrorResponse & { status: number } {
- console.error("API error occurred", error);
+ console.error("API error occurred", error.message);
// Zod errors
if (error instanceof ZodError) {