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.

document edit domain endpoint

Kiran K36a907ca7d71
diff --git a/apps/web/app/api/domains/[domain]/route.ts b/apps/web/app/api/domains/[domain]/route.ts
index fa4a2a6182..9b978638b3 100644
--- a/apps/web/app/api/domains/[domain]/route.ts
+++ b/apps/web/app/api/domains/[domain]/route.ts
@@ -7,6 +7,7 @@ import {
} from "@/lib/api/domains";
import { withAuth } from "@/lib/auth";
import prisma from "@/lib/prisma";
+import { updateDomainBodySchema } from "@/lib/zod/schemas";
import { NextResponse } from "next/server";
// GET /api/domains/[domain] – get a workspace's domain
@@ -49,7 +50,7 @@ export const PUT = withAuth(
type,
placeholder,
expiredUrl,
- } = await req.json();
+ } = updateDomainBodySchema.parse(await req.json());
if (newDomain !== domain) {
const validDomain = await validateDomain(newDomain);
diff --git a/apps/web/app/api/domains/route.ts b/apps/web/app/api/domains/route.ts
index 9ea94a94ac..9a03a554ac 100644
--- a/apps/web/app/api/domains/route.ts
+++ b/apps/web/app/api/domains/route.ts
@@ -36,6 +36,8 @@ export const POST = withAuth(async ({ req, workspace }) => {
slug: domain,
target,
type,
+ expiredUrl,
+ placeholder,
} = addDomainBodySchema.parse(await req.json());
if (workspace.domains.length >= workspace.domainsLimit) {
@@ -71,10 +73,14 @@ export const POST = withAuth(async ({ req, workspace }) => {
const response = await prisma.domain.create({
data: {
slug: domain,
- target,
type,
projectId: workspace.id,
primary: workspace.domains.length === 0,
+ placeholder,
+ ...(workspace.plan !== "free" && {
+ target,
+ expiredUrl,
+ }),
},
});
diff --git a/apps/web/lib/openapi/domains/edit-domain.ts b/apps/web/lib/openapi/domains/edit-domain.ts
new file mode 100644
index 0000000000..6673c6bc19
--- /dev/null
+++ b/apps/web/lib/openapi/domains/edit-domain.ts
@@ -0,0 +1,34 @@
+import { openApiErrorResponses } from "@/lib/openapi/responses";
+import { DomainSchema, addDomainBodySchema, updateDomainBodySchema } from "@/lib/zod/schemas";
+import { ZodOpenApiOperationObject } from "zod-openapi";
+import { workspaceParamsSchema } from "../request";
+
+export const editDomain: ZodOpenApiOperationObject = {
+ operationId: "editDomain",
+ "x-speakeasy-name-override": "update",
+ summary: "Edit a domain",
+ description: "Edit a domain for the authenticated workspace.",
+ requestParams: {
+ query: workspaceParamsSchema,
+ },
+ requestBody: {
+ content: {
+ "application/json": {
+ schema: updateDomainBodySchema,
+ },
+ },
+ },
+ responses: {
+ "200": {
+ description: "The domain was updated.",
+ content: {
+ "application/json": {
+ schema: DomainSchema,
+ },
+ },
+ },
+ ...openApiErrorResponses,
+ },
+ tags: ["Domains"],
+ security: [{ token: [] }],
+};
diff --git a/apps/web/lib/openapi/domains/index.ts b/apps/web/lib/openapi/domains/index.ts
index 8774dc2d65..99d2b188a8 100644
--- a/apps/web/lib/openapi/domains/index.ts
+++ b/apps/web/lib/openapi/domains/index.ts
@@ -2,6 +2,7 @@ import { ZodOpenApiPathsObject } from "zod-openapi";
import { addDomain } from "./add-domain";
import { listDomains } from "./list-domains";
import { deleteDomain } from "./delete-domain";
+import { editDomain } from "./edit-domain";
export const domainsPaths: ZodOpenApiPathsObject = {
"/domains": {
@@ -10,5 +11,6 @@ export const domainsPaths: ZodOpenApiPathsObject = {
},
"/domains/{slug}": {
delete: deleteDomain,
+ put: editDomain,
},
};
diff --git a/apps/web/lib/zod/schemas/domains.ts b/apps/web/lib/zod/schemas/domains.ts
index 63e0d510a0..b0b615fe44 100644
--- a/apps/web/lib/zod/schemas/domains.ts
+++ b/apps/web/lib/zod/schemas/domains.ts
@@ -1,41 +1,5 @@
import z from "@/lib/zod";
-
-// TODO
-// Add max and min
-
-export const addDomainBodySchema = z.object({
- slug: z
- .string({ required_error: "Domain name is required" })
- .min(1)
- .max(50, "Domain name is too long. Max 50 characters long")
- .describe("Name of the domain."),
- target: z
- .string()
- .url("Target URL must be a valid URL.")
- .optional()
- .describe(
- "The page your users will get redirected to when they visit your domain.",
- ),
- type: z
- .enum(["redirect", "rewrite"])
- .optional()
- .default("redirect")
- .describe("The type of redirect to use for this domain."),
- expiredUrl: z
- .string()
- .url("Expired URL must be a valid URL")
- .optional()
- .describe(
- "Redirect users to a specific URL when any link under this domain has expired.",
- ),
- placeholder: z
- .string()
- .url("Placeholder URL must be a valid URL")
- .optional()
- .describe(
- "Provide context to your teammates in the link creation modal by showing them an example of a link to be shortened.",
- ),
-});
+import { isValidUrl } from "@dub/utils";
export const DomainSchema = z
.object({
@@ -83,4 +47,41 @@ export const DomainSchema = z
})
.strict();
-export type Domain = z.infer<typeof DomainSchema>;
+export const addDomainBodySchema = z.object({
+ slug: z
+ .string({ required_error: "Domain name is required" })
+ .min(1)
+ .max(50, "Domain name is too long. Max 50 characters long")
+ .describe("Name of the domain."),
+ target: z
+ .string()
+ // .url("target must be a valid URL")
+ .refine((value) => (value ? isValidUrl(value) : true), {
+ message: "Target URL must be a valid URL.",
+ })
+ .optional()
+ .describe(
+ "The page your users will get redirected to when they visit your domain.",
+ ),
+ type: z
+ .enum(["redirect", "rewrite"])
+ .optional()
+ .default("redirect")
+ .describe("The type of redirect to use for this domain."),
+ expiredUrl: z
+ .string()
+ .url("expiredUrl must be a valid URL")
+ .optional()
+ .describe(
+ "Redirect users to a specific URL when any link under this domain has expired.",
+ ),
+ placeholder: z
+ .string()
+ .url("placeholder must be a valid URL")
+ .optional()
+ .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;