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.

set primary domain + transfer a domain

Kiran K4adaf748bdf5
diff --git a/apps/docs/api-reference/endpoint/set-a-primary-domain.mdx b/apps/docs/api-reference/endpoint/set-a-primary-domain.mdx
new file mode 100644
index 0000000000..d5d75a95fd
--- /dev/null
+++ b/apps/docs/api-reference/endpoint/set-a-primary-domain.mdx
@@ -0,0 +1,4 @@
+---
+openapi: post /domains/{slug}/primary
+og:title: "Set a domain as primary for a workspace with the Dub.co API - API Reference"
+---
diff --git a/apps/docs/api-reference/endpoint/transfer-a-domain.mdx b/apps/docs/api-reference/endpoint/transfer-a-domain.mdx
new file mode 100644
index 0000000000..99e771553a
--- /dev/null
+++ b/apps/docs/api-reference/endpoint/transfer-a-domain.mdx
@@ -0,0 +1,4 @@
+---
+openapi: post /domains/{slug}/transfer
+og:title: "Transfer a domain to the another workspace with the Dub.co API - API Reference"
+---
diff --git a/apps/web/app/api/domains/[domain]/primary/route.ts b/apps/web/app/api/domains/[domain]/primary/route.ts
index 8d3fad6a42..85fa5b2682 100644
--- a/apps/web/app/api/domains/[domain]/primary/route.ts
+++ b/apps/web/app/api/domains/[domain]/primary/route.ts
@@ -1,11 +1,12 @@
import { withAuth } from "@/lib/auth";
import prisma from "@/lib/prisma";
+import { DomainSchema } from "@/lib/zod/schemas";
import { NextResponse } from "next/server";
// POST /api/domains/[domain]/primary – set a domain as primary
export const POST = withAuth(
async ({ headers, workspace, domain }) => {
- const response = await Promise.all([
+ const responses = await Promise.all([
prisma.domain.update({
where: {
slug: domain,
@@ -19,14 +20,17 @@ export const POST = withAuth(
where: {
projectId: workspace.id,
primary: true,
+ slug: {
+ not: domain,
+ },
},
data: {
primary: false,
},
}),
]);
- console.log({ response });
- return NextResponse.json(response, { headers });
+
+ return NextResponse.json(DomainSchema.parse(responses[0]), { headers });
},
{
domainChecks: true,
diff --git a/apps/web/app/api/domains/[domain]/transfer/route.ts b/apps/web/app/api/domains/[domain]/transfer/route.ts
index 55a5a2c308..a70ee2ca5e 100644
--- a/apps/web/app/api/domains/[domain]/transfer/route.ts
+++ b/apps/web/app/api/domains/[domain]/transfer/route.ts
@@ -4,17 +4,10 @@ import { DubApiError } from "@/lib/api/errors";
import { withAuth } from "@/lib/auth";
import { qstash } from "@/lib/cron";
import prisma from "@/lib/prisma";
-import z from "@/lib/zod";
+import { DomainSchema, transferDomainBodySchema } from "@/lib/zod/schemas";
import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
import { NextResponse } from "next/server";
-const transferDomainBodySchema = z.object({
- newWorkspaceId: z
- .string()
- .min(1, "Missing new workspace ID.")
- .transform((v) => v.replace("ws_", "")),
-});
-
// POST /api/domains/[domain]/transfer – transfer a domain to another workspace
export const POST = withAuth(
async ({ req, headers, session, params, workspace }) => {
@@ -156,7 +149,7 @@ export const POST = withAuth(
},
});
- return NextResponse.json(domainResponse, { headers });
+ return NextResponse.json(DomainSchema.parse(domainResponse), { headers });
},
{ requiredRole: ["owner"] },
);
diff --git a/apps/web/lib/openapi/domains/delete-domain.ts b/apps/web/lib/openapi/domains/delete-domain.ts
index d081c128aa..842370d3a3 100644
--- a/apps/web/lib/openapi/domains/delete-domain.ts
+++ b/apps/web/lib/openapi/domains/delete-domain.ts
@@ -5,7 +5,7 @@ import { workspaceParamsSchema } from "../request";
export const deleteDomain: ZodOpenApiOperationObject = {
operationId: "deleteDomain",
- "x-speakeasy-name-override": "deleteDomain",
+ "x-speakeasy-name-override": "delete",
summary: "Delete a domain",
description: "Delete a domain from a workspace.",
requestParams: {
diff --git a/apps/web/lib/openapi/domains/edit-domain.ts b/apps/web/lib/openapi/domains/edit-domain.ts
index 6673c6bc19..355015c2ce 100644
--- a/apps/web/lib/openapi/domains/edit-domain.ts
+++ b/apps/web/lib/openapi/domains/edit-domain.ts
@@ -1,5 +1,5 @@
import { openApiErrorResponses } from "@/lib/openapi/responses";
-import { DomainSchema, addDomainBodySchema, updateDomainBodySchema } from "@/lib/zod/schemas";
+import { DomainSchema, updateDomainBodySchema } from "@/lib/zod/schemas";
import { ZodOpenApiOperationObject } from "zod-openapi";
import { workspaceParamsSchema } from "../request";
diff --git a/apps/web/lib/openapi/domains/index.ts b/apps/web/lib/openapi/domains/index.ts
index 99d2b188a8..9c7ebf353f 100644
--- a/apps/web/lib/openapi/domains/index.ts
+++ b/apps/web/lib/openapi/domains/index.ts
@@ -3,6 +3,8 @@ import { addDomain } from "./add-domain";
import { listDomains } from "./list-domains";
import { deleteDomain } from "./delete-domain";
import { editDomain } from "./edit-domain";
+import { setPrimaryDomain } from "./set-primary-domain";
+import { transferDomain } from "./transfer-domain";
export const domainsPaths: ZodOpenApiPathsObject = {
"/domains": {
@@ -10,7 +12,13 @@ export const domainsPaths: ZodOpenApiPathsObject = {
get: listDomains,
},
"/domains/{slug}": {
- delete: deleteDomain,
put: editDomain,
+ delete: deleteDomain,
+ },
+ "/domains/{slug}/primary": {
+ post: setPrimaryDomain,
+ },
+ "/domains/{slug}/transfer": {
+ post: transferDomain,
},
};
diff --git a/apps/web/lib/openapi/domains/set-primary-domain.ts b/apps/web/lib/openapi/domains/set-primary-domain.ts
new file mode 100644
index 0000000000..f356475d28
--- /dev/null
+++ b/apps/web/lib/openapi/domains/set-primary-domain.ts
@@ -0,0 +1,28 @@
+import { openApiErrorResponses } from "@/lib/openapi/responses";
+import { DomainSchema } from "@/lib/zod/schemas";
+import { ZodOpenApiOperationObject } from "zod-openapi";
+import { workspaceParamsSchema } from "../request";
+
+export const setPrimaryDomain: ZodOpenApiOperationObject = {
+ operationId: "setPrimaryDomain",
+ "x-speakeasy-name-override": "setPrimary",
+ summary: "Set a domain as primary",
+ description: "Set a domain as primary for the authenticated workspace.",
+ requestParams: {
+ query: workspaceParamsSchema,
+ path: DomainSchema.pick({ slug: true }),
+ },
+ responses: {
+ "200": {
+ description: "The domain was set as primary",
+ content: {
+ "application/json": {
+ schema: DomainSchema,
+ },
+ },
+ },
+ ...openApiErrorResponses,
+ },
+ tags: ["Domains"],
+ security: [{ token: [] }],
+};
diff --git a/apps/web/lib/openapi/domains/transfer-domain.ts b/apps/web/lib/openapi/domains/transfer-domain.ts
new file mode 100644
index 0000000000..7976ba2004
--- /dev/null
+++ b/apps/web/lib/openapi/domains/transfer-domain.ts
@@ -0,0 +1,36 @@
+import { openApiErrorResponses } from "@/lib/openapi/responses";
+import { DomainSchema, transferDomainBodySchema } from "@/lib/zod/schemas";
+import { ZodOpenApiOperationObject } from "zod-openapi";
+import { workspaceParamsSchema } from "../request";
+
+export const transferDomain: ZodOpenApiOperationObject = {
+ operationId: "transferDomain",
+ "x-speakeasy-name-override": "transfer",
+ summary: "Transfer a domain",
+ description:
+ "Transfer a domain to another workspace within the authenticated account.",
+ requestParams: {
+ query: workspaceParamsSchema,
+ path: DomainSchema.pick({ slug: true }),
+ },
+ requestBody: {
+ content: {
+ "application/json": {
+ schema: transferDomainBodySchema,
+ },
+ },
+ },
+ responses: {
+ "200": {
+ description: "The domain transfer initiated",
+ content: {
+ "application/json": {
+ schema: DomainSchema,
+ },
+ },
+ },
+ ...openApiErrorResponses,
+ },
+ tags: ["Domains"],
+ security: [{ token: [] }],
+};
diff --git a/apps/web/lib/openapi/index.ts b/apps/web/lib/openapi/index.ts
index f814e18f84..8570a3bf85 100644
--- a/apps/web/lib/openapi/index.ts
+++ b/apps/web/lib/openapi/index.ts
@@ -9,6 +9,7 @@ import { linksPaths } from "./links";
import { qrCodePaths } from "./qr";
import { tagsPaths } from "./tags";
import { workspacesPaths } from "./workspaces";
+import { domainsPaths } from "./domains";
export const openApiObject: ZodOpenApiObject = {
openapi: "3.0.3",
@@ -29,7 +30,7 @@ export const openApiObject: ZodOpenApiObject = {
},
servers: [
{
- url: API_DOMAIN,
+ url: "https://8be2-103-181-40-111.ngrok-free.app/api/",
description: "Production API",
},
],
@@ -39,6 +40,7 @@ export const openApiObject: ZodOpenApiObject = {
...analyticsPaths,
...workspacesPaths,
...tagsPaths,
+ ...domainsPaths,
},
components: {
schemas: {
diff --git a/apps/web/lib/zod/schemas/domains.ts b/apps/web/lib/zod/schemas/domains.ts
index b0b615fe44..df6cfda7bb 100644
--- a/apps/web/lib/zod/schemas/domains.ts
+++ b/apps/web/lib/zod/schemas/domains.ts
@@ -1,51 +1,46 @@
import z from "@/lib/zod";
import { isValidUrl } from "@dub/utils";
-export const DomainSchema = z
- .object({
- slug: z
- .string()
- .describe("The domain name.")
- .openapi({ example: "acme.com" }),
- verified: z.boolean().describe("Whether the domain is verified."),
- primary: z
- .boolean()
- .describe("Whether the domain is the primary domain for the workspace."),
- archived: z
- .boolean()
- .describe("Whether the domain is archived.")
- .default(false),
- placeholder: z
- .string()
- .describe(
- "Provide context to your teammates in the link creation modal by showing them an example of a link to be shortened.",
- )
- .default("https://dub.co/help/article/what-is-dub")
- .openapi({ example: "https://dub.co/help/article/what-is-dub" }),
- expiredUrl: z
- .string()
- .nullable()
- .describe(
- "The URL to redirect to when a link under this domain has expired.",
- )
- .openapi({ example: "https://acme.com/expired" }),
- target: z
- .string()
- .nullable()
- .describe(
- "The page your users will get redirected to when they visit your domain.",
- )
- .openapi({ example: "https://acme.com/landing" }),
- type: z
- .string()
- .describe("The type of redirect to use for this domain.")
- .openapi({ enum: ["redirect", "rewrite"] }),
- clicks: z
- .number()
- .describe("The number of clicks on the domain.")
- .default(0),
- })
- .strict();
+export const DomainSchema = z.object({
+ slug: z
+ .string()
+ .describe("The domain name.")
+ .openapi({ example: "acme.com" }),
+ verified: z.boolean().describe("Whether the domain is verified."),
+ primary: z
+ .boolean()
+ .describe("Whether the domain is the primary domain for the workspace."),
+ archived: z
+ .boolean()
+ .describe("Whether the domain is archived.")
+ .default(false),
+ placeholder: z
+ .string()
+ .describe(
+ "Provide context to your teammates in the link creation modal by showing them an example of a link to be shortened.",
+ )
+ .default("https://dub.co/help/article/what-is-dub")
+ .openapi({ example: "https://dub.co/help/article/what-is-dub" }),
+ expiredUrl: z
+ .string()
+ .nullable()
+ .describe(
+ "The URL to redirect to when a link under this domain has expired.",
+ )
+ .openapi({ example: "https://acme.com/expired" }),
+ target: z
+ .string()
+ .nullable()
+ .describe(
+ "The page your users will get redirected to when they visit your domain.",
+ )
+ .openapi({ example: "https://acme.com/landing" }),
+ type: z
+ .string()
+ .describe("The type of redirect to use for this domain.")
+ .openapi({ enum: ["redirect", "rewrite"] }),
+ clicks: z.number().describe("The number of clicks on the domain.").default(0),
+});
export const addDomainBodySchema = z.object({
slug: z
@@ -85,3 +80,10 @@ export const addDomainBodySchema = z.object({
});
export const updateDomainBodySchema = addDomainBodySchema;
+
+export const transferDomainBodySchema = z.object({
+ newWorkspaceId: z
+ .string({ required_error: "New workspace ID is required." })
+ .min(1, "New workspace ID cannot be empty.")
+ .transform((v) => v.replace("ws_", "")),
+});