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 route.ts

Kiran Kc36541923125
diff --git a/apps/web/app/(ee)/api/domains/status/route.ts b/apps/web/app/(ee)/api/domains/status/route.ts
index e161602210..25286fcc48 100644
--- a/apps/web/app/(ee)/api/domains/status/route.ts
+++ b/apps/web/app/(ee)/api/domains/status/route.ts
@@ -1,3 +1,4 @@
+import { DubApiError } from "@/lib/api/errors";
import { withWorkspace } from "@/lib/auth";
import { searchDomainsAvailability } from "@/lib/dynadot/search-domains";
import {
@@ -6,12 +7,20 @@ import {
} from "@/lib/zod/schemas/domains";
import { prisma } from "@dub/prisma";
import { NextResponse } from "next/server";
+import { z } from "zod";
// GET /api/domains/status - checks the availability status of one or more domains
export const GET = withWorkspace(
async ({ searchParams }) => {
let { domains } = searchDomainSchema.parse(searchParams);
+ if (domains.length === 0) {
+ throw new DubApiError({
+ code: "bad_request",
+ message: "You must provide at least one domain to check.",
+ });
+ }
+
const domainsOnDub = await prisma.domain.findMany({
where: {
slug: {
@@ -24,16 +33,17 @@ export const GET = withWorkspace(
},
});
- if (domainsOnDub.length === domains.length) {
- return NextResponse.json(
- DomainStatusSchema.array().parse(
- domains.map((domain) => ({
- domain,
- available: false,
- price: null,
- premium: null,
- })),
- ),
+ let response: z.infer<typeof DomainStatusSchema>[] = [];
+
+ // if all domains are already registered on Dub, return the status for all domains as false
+ if (domainsOnDub.length > 0) {
+ response = DomainStatusSchema.array().parse(
+ domainsOnDub.map(({ slug: domain }) => ({
+ domain,
+ available: false,
+ price: null,
+ premium: null,
+ })),
);
}
@@ -41,18 +51,21 @@ export const GET = withWorkspace(
(domain) => !domainsOnDub.some((d) => d.slug === domain),
);
- const domainsToSearch = domains.reduce(
- (acc, domain, index) => {
- acc[`domain${index}`] = domain;
- return acc;
- },
- {} as Record<string, string>,
- );
+ if (domains.length > 0) {
+ const domainsToSearch = domains.reduce(
+ (acc, domain, index) => {
+ acc[`domain${index}`] = domain;
+ return acc;
+ },
+ {} as Record<string, string>,
+ );
- // search for the domain on Dynadot
- const response = await searchDomainsAvailability({
- domains: domainsToSearch,
- });
+ const searchResponse = await searchDomainsAvailability({
+ domains: domainsToSearch,
+ });
+
+ response = response.concat(searchResponse);
+ }
return NextResponse.json(response);
},