Skip to content

Commit

Permalink
Handle Dyandot DNS configuration errors
Browse files Browse the repository at this point in the history
  • Loading branch information
TWilson023 committed Feb 4, 2025
1 parent 5b90c22 commit cc905e2
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions apps/web/lib/dynadot/configure-dns.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { z } from "zod";
import { DubApiError } from "../api/errors";
import { DYNADOT_API_KEY, DYNADOT_BASE_URL } from "./constants";

const schema = z.object({
SetDnsResponse: z.object({
ResponseCode: z.string().or(z.number()),
Status: z.string(),
Error: z.string().optional(),
}),
});

export const configureDNS = async ({ domain }: { domain: string }) => {
const searchParams = new URLSearchParams({
key: DYNADOT_API_KEY,
Expand All @@ -9,9 +19,32 @@ export const configureDNS = async ({ domain }: { domain: string }) => {
main_record0: "76.76.21.21",
});

return fetch(`${DYNADOT_BASE_URL}?${searchParams.toString()}`, {
headers: {
"Content-Type": "application/x-www-form-urlencoded",
const response = await fetch(
`${DYNADOT_BASE_URL}?${searchParams.toString()}`,
{
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
},
}).then((res) => res.json());
);

if (!response.ok) {
throw new DubApiError({
code: "bad_request",
message: `Failed to configure DNS for domain "${domain}": ${response.statusText}`,
});
}

const data = schema.parse(await response.json());

const { Status, Error } = data.SetDnsResponse;

if (Status !== "success") {
throw new DubApiError({
code: "bad_request",
message: `Failed to configure DNS for domain "${domain}": ${Error}`,
});
}

return data;
};

0 comments on commit cc905e2

Please sign in to comment.