feat: expose domain dns records via api (#259)
This commit is contained in:
@@ -11,6 +11,7 @@ import {
|
||||
createDomain,
|
||||
deleteDomain,
|
||||
getDomain,
|
||||
getDomains,
|
||||
updateDomain,
|
||||
} from "~/server/service/domain-service";
|
||||
import { sendEmail } from "~/server/service/email-service";
|
||||
@@ -29,7 +30,7 @@ export const domainRouter = createTRPCRouter({
|
||||
ctx.team.id,
|
||||
input.name,
|
||||
input.region,
|
||||
ctx.team.sesTenantId ?? undefined,
|
||||
ctx.team.sesTenantId ?? undefined
|
||||
);
|
||||
}),
|
||||
|
||||
@@ -41,20 +42,11 @@ export const domainRouter = createTRPCRouter({
|
||||
}),
|
||||
|
||||
domains: teamProcedure.query(async ({ ctx }) => {
|
||||
const domains = await db.domain.findMany({
|
||||
where: {
|
||||
teamId: ctx.team.id,
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: "desc",
|
||||
},
|
||||
});
|
||||
|
||||
return domains;
|
||||
return getDomains(ctx.team.id);
|
||||
}),
|
||||
|
||||
getDomain: domainProcedure.query(async ({ input }) => {
|
||||
return getDomain(input.id);
|
||||
getDomain: domainProcedure.query(async ({ input, ctx }) => {
|
||||
return getDomain(input.id, ctx.team.id);
|
||||
}),
|
||||
|
||||
updateDomain: domainProcedure
|
||||
@@ -62,7 +54,7 @@ export const domainRouter = createTRPCRouter({
|
||||
z.object({
|
||||
clickTracking: z.boolean().optional(),
|
||||
openTracking: z.boolean().optional(),
|
||||
}),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ input }) => {
|
||||
return updateDomain(input.id, {
|
||||
@@ -104,6 +96,6 @@ export const domainRouter = createTRPCRouter({
|
||||
text: "hello,\n\nuseSend is the best open source sending platform\n\ncheck out https://usesend.com",
|
||||
html: "<p>hello,</p><p>useSend is the best open source sending platform<p><p>check out <a href='https://usesend.com'>usesend.com</a>",
|
||||
});
|
||||
},
|
||||
}
|
||||
),
|
||||
});
|
||||
|
||||
@@ -2,7 +2,6 @@ import { createRoute, z } from "@hono/zod-openapi";
|
||||
import { DomainSchema } from "~/lib/zod/domain-schema";
|
||||
import { PublicAPIApp } from "~/server/public-api/hono";
|
||||
import { createDomain as createDomainService } from "~/server/service/domain-service";
|
||||
import { getTeamFromToken } from "~/server/public-api/auth";
|
||||
|
||||
const route = createRoute({
|
||||
method: "post",
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
import { createRoute, z } from "@hono/zod-openapi";
|
||||
import { DomainSchema } from "~/lib/zod/domain-schema";
|
||||
import { PublicAPIApp } from "~/server/public-api/hono";
|
||||
import { UnsendApiError } from "../../api-error";
|
||||
import { db } from "~/server/db";
|
||||
import { getDomain as getDomainService } from "~/server/service/domain-service";
|
||||
|
||||
const route = createRoute({
|
||||
method: "get",
|
||||
path: "/v1/domains/{id}",
|
||||
request: {
|
||||
params: z.object({
|
||||
id: z.coerce.number().openapi({
|
||||
param: { name: "id", in: "path" },
|
||||
example: 1,
|
||||
}),
|
||||
}),
|
||||
},
|
||||
responses: {
|
||||
200: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: DomainSchema,
|
||||
},
|
||||
},
|
||||
description: "Retrieve the domain",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
function getDomain(app: PublicAPIApp) {
|
||||
app.openapi(route, async (c) => {
|
||||
const team = c.var.team;
|
||||
const id = c.req.valid("param").id;
|
||||
|
||||
// Enforce API key domain restriction (if any)
|
||||
if (team.apiKey.domainId && team.apiKey.domainId !== id) {
|
||||
throw new UnsendApiError({
|
||||
code: "NOT_FOUND",
|
||||
message: "Domain not found",
|
||||
});
|
||||
}
|
||||
|
||||
// Re-use service logic to enrich domain (verification status, DNS records, etc.)
|
||||
let enriched;
|
||||
try {
|
||||
enriched = await getDomainService(id, team.id);
|
||||
} catch (e) {
|
||||
throw new UnsendApiError({
|
||||
code: "INTERNAL_SERVER_ERROR",
|
||||
message: e instanceof Error ? e.message : "Internal server error",
|
||||
});
|
||||
}
|
||||
|
||||
return c.json(enriched);
|
||||
});
|
||||
}
|
||||
|
||||
export default getDomain;
|
||||
@@ -1,7 +1,7 @@
|
||||
import { createRoute, z } from "@hono/zod-openapi";
|
||||
import { DomainSchema } from "~/lib/zod/domain-schema";
|
||||
import { PublicAPIApp } from "~/server/public-api/hono";
|
||||
import { db } from "~/server/db";
|
||||
import { getDomains as getDomainsService } from "~/server/service/domain-service";
|
||||
|
||||
const route = createRoute({
|
||||
method: "get",
|
||||
@@ -23,11 +23,9 @@ function getDomains(app: PublicAPIApp) {
|
||||
const team = c.var.team;
|
||||
|
||||
// If API key is restricted to a specific domain, only return that domain; else return all team domains
|
||||
const domains = team.apiKey.domainId
|
||||
? await db.domain.findMany({
|
||||
where: { teamId: team.id, id: team.apiKey.domainId },
|
||||
})
|
||||
: await db.domain.findMany({ where: { teamId: team.id } });
|
||||
const domains = await getDomainsService(team.id, {
|
||||
domainId: team.apiKey.domainId ?? undefined,
|
||||
});
|
||||
|
||||
return c.json(domains);
|
||||
});
|
||||
|
||||
@@ -13,6 +13,7 @@ import upsertContact from "./api/contacts/upsert-contact";
|
||||
import createDomain from "./api/domains/create-domain";
|
||||
import deleteContact from "./api/contacts/delete-contact";
|
||||
import verifyDomain from "./api/domains/verify-domain";
|
||||
import getDomain from "./api/domains/get-domain";
|
||||
import sendBatch from "./api/emails/batch-email";
|
||||
|
||||
export const app = getApp();
|
||||
@@ -21,6 +22,7 @@ export const app = getApp();
|
||||
getDomains(app);
|
||||
createDomain(app);
|
||||
verifyDomain(app);
|
||||
getDomain(app);
|
||||
|
||||
/**Email related APIs */
|
||||
getEmail(app);
|
||||
|
||||
@@ -6,8 +6,79 @@ import { db } from "~/server/db";
|
||||
import { SesSettingsService } from "./ses-settings-service";
|
||||
import { UnsendApiError } from "../public-api/api-error";
|
||||
import { logger } from "../logger/log";
|
||||
import { ApiKey } from "@prisma/client";
|
||||
import { ApiKey, DomainStatus, type Domain } from "@prisma/client";
|
||||
import { LimitService } from "./limit-service";
|
||||
import type { DomainDnsRecord } from "~/types/domain";
|
||||
|
||||
const DOMAIN_STATUS_VALUES = new Set(Object.values(DomainStatus));
|
||||
|
||||
function parseDomainStatus(status?: string | null): DomainStatus {
|
||||
if (!status) {
|
||||
return DomainStatus.NOT_STARTED;
|
||||
}
|
||||
|
||||
const normalized = status.toUpperCase();
|
||||
|
||||
if (DOMAIN_STATUS_VALUES.has(normalized as DomainStatus)) {
|
||||
return normalized as DomainStatus;
|
||||
}
|
||||
|
||||
return DomainStatus.NOT_STARTED;
|
||||
}
|
||||
|
||||
function buildDnsRecords(domain: Domain): DomainDnsRecord[] {
|
||||
const subdomainSuffix = domain.subdomain ? `.${domain.subdomain}` : "";
|
||||
const mailDomain = `mail${subdomainSuffix}`;
|
||||
const dkimSelector = domain.dkimSelector ?? "usesend";
|
||||
|
||||
const spfStatus = parseDomainStatus(domain.spfDetails);
|
||||
const dkimStatus = parseDomainStatus(domain.dkimStatus);
|
||||
const dmarcStatus = domain.dmarcAdded
|
||||
? DomainStatus.SUCCESS
|
||||
: DomainStatus.NOT_STARTED;
|
||||
|
||||
return [
|
||||
{
|
||||
type: "MX",
|
||||
name: mailDomain,
|
||||
value: `feedback-smtp.${domain.region}.amazonses.com`,
|
||||
ttl: "Auto",
|
||||
priority: "10",
|
||||
status: spfStatus,
|
||||
},
|
||||
{
|
||||
type: "TXT",
|
||||
name: `${dkimSelector}._domainkey${subdomainSuffix}`,
|
||||
value: `p=${domain.publicKey}`,
|
||||
ttl: "Auto",
|
||||
status: dkimStatus,
|
||||
},
|
||||
{
|
||||
type: "TXT",
|
||||
name: mailDomain,
|
||||
value: "v=spf1 include:amazonses.com ~all",
|
||||
ttl: "Auto",
|
||||
status: spfStatus,
|
||||
},
|
||||
{
|
||||
type: "TXT",
|
||||
name: "_dmarc",
|
||||
value: "v=DMARC1; p=none;",
|
||||
ttl: "Auto",
|
||||
status: dmarcStatus,
|
||||
recommended: true,
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
function withDnsRecords<T extends Domain>(
|
||||
domain: T
|
||||
): T & { dnsRecords: DomainDnsRecord[] } {
|
||||
return {
|
||||
...domain,
|
||||
dnsRecords: buildDnsRecords(domain),
|
||||
};
|
||||
}
|
||||
|
||||
const dnsResolveTxt = util.promisify(dns.resolveTxt);
|
||||
|
||||
@@ -63,12 +134,12 @@ export async function validateApiKeyDomainAccess(
|
||||
) {
|
||||
// First validate the domain exists and is verified
|
||||
const domain = await validateDomainFromEmail(email, teamId);
|
||||
|
||||
|
||||
// If API key has no domain restriction (domainId is null), allow all domains
|
||||
if (!apiKey.domainId) {
|
||||
return domain;
|
||||
}
|
||||
|
||||
|
||||
// If API key is restricted to a specific domain, check if it matches
|
||||
if (apiKey.domainId !== domain.id) {
|
||||
throw new UnsendApiError({
|
||||
@@ -76,7 +147,7 @@ export async function validateApiKeyDomainAccess(
|
||||
message: `API key does not have access to domain: ${domain.name}`,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
return domain;
|
||||
}
|
||||
|
||||
@@ -128,21 +199,27 @@ export async function createDomain(
|
||||
region,
|
||||
sesTenantId,
|
||||
dkimSelector,
|
||||
dkimStatus: DomainStatus.NOT_STARTED,
|
||||
spfDetails: DomainStatus.NOT_STARTED,
|
||||
},
|
||||
});
|
||||
|
||||
return domain;
|
||||
return withDnsRecords(domain);
|
||||
}
|
||||
|
||||
export async function getDomain(id: number) {
|
||||
export async function getDomain(id: number, teamId: number) {
|
||||
let domain = await db.domain.findUnique({
|
||||
where: {
|
||||
id,
|
||||
teamId,
|
||||
},
|
||||
});
|
||||
|
||||
if (!domain) {
|
||||
throw new Error("Domain not found");
|
||||
throw new UnsendApiError({
|
||||
code: "NOT_FOUND",
|
||||
message: "Domain not found",
|
||||
});
|
||||
}
|
||||
|
||||
if (domain.isVerifying) {
|
||||
@@ -178,17 +255,30 @@ export async function getDomain(id: number) {
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
const normalizedDomain = {
|
||||
...domain,
|
||||
dkimStatus: dkimStatus?.toString() ?? null,
|
||||
spfDetails: spfDetails?.toString() ?? null,
|
||||
verificationError: verificationError?.toString() ?? null,
|
||||
lastCheckedTime,
|
||||
dmarcAdded: dmarcRecord ? true : false,
|
||||
} satisfies Domain;
|
||||
|
||||
const domainWithDns = withDnsRecords(normalizedDomain);
|
||||
const normalizedLastCheckedTime =
|
||||
lastCheckedTime instanceof Date
|
||||
? lastCheckedTime.toISOString()
|
||||
: (lastCheckedTime ?? null);
|
||||
|
||||
return {
|
||||
...domainWithDns,
|
||||
dkimStatus: normalizedDomain.dkimStatus,
|
||||
spfDetails: normalizedDomain.spfDetails,
|
||||
verificationError: verificationError?.toString() ?? null,
|
||||
lastCheckedTime: normalizedLastCheckedTime,
|
||||
dmarcAdded: normalizedDomain.dmarcAdded,
|
||||
};
|
||||
}
|
||||
|
||||
return domain;
|
||||
return withDnsRecords(domain);
|
||||
}
|
||||
|
||||
export async function updateDomain(
|
||||
@@ -225,15 +315,21 @@ export async function deleteDomain(id: number) {
|
||||
return deletedRecord;
|
||||
}
|
||||
|
||||
export async function getDomains(teamId: number) {
|
||||
return db.domain.findMany({
|
||||
export async function getDomains(
|
||||
teamId: number,
|
||||
options?: { domainId?: number }
|
||||
) {
|
||||
const domains = await db.domain.findMany({
|
||||
where: {
|
||||
teamId,
|
||||
...(options?.domainId ? { id: options.domainId } : {}),
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: "desc",
|
||||
},
|
||||
});
|
||||
|
||||
return domains.map((d) => withDnsRecords(d));
|
||||
}
|
||||
|
||||
async function getDmarcRecord(domain: string) {
|
||||
|
||||
Reference in New Issue
Block a user