Skip to content

Email for professionals and teams

1.5 KB
Raw Blame
import { authFetch } from "@/lib/auth/client";
import type {
	DomainCreateResult,
	DomainListResult,
	DomainPreflightResponse,
	MailboxCreateResult,
} from "./types";

export async function getDomains(): Promise<DomainListResult> {
	const res = await authFetch("/api/domains");
	return (await res.json()) as DomainListResult;
}

export async function checkDomain(hostname: string): Promise<DomainPreflightResponse> {
	const res = await authFetch("/api/domains/check", {
		method: "POST",
		headers: { "Content-Type": "application/json" },
		body: JSON.stringify({ hostname }),
	});
	const data = (await res.json()) as Omit<DomainPreflightResponse, "ok">;
	return { ok: res.ok, ...data };
}

export async function createDomain(
	hostname: string,
	enableSending: boolean,
	replaceMxRecords = false,
	skipDns = false,
): Promise<{ ok: boolean; data: DomainCreateResult }> {
	const res = await authFetch("/api/domains", {
		method: "POST",
		headers: { "Content-Type": "application/json" },
		body: JSON.stringify({ hostname, enableRouting: true, enableSending, replaceMxRecords, skipDns }),
	});

	return {
		ok: res.ok,
		data: (await res.json()) as DomainCreateResult,
	};
}

export async function createMailbox(
	domainId: string,
	localPart: string,
): Promise<{ ok: boolean; data: MailboxCreateResult }> {
	const res = await authFetch("/api/mailboxes", {
		method: "POST",
		headers: { "Content-Type": "application/json" },
		body: JSON.stringify({ domainId, localPart, displayName: localPart }),
	});

	return {
		ok: res.ok,
		data: (await res.json()) as MailboxCreateResult,
	};
}