feat: expose domain dns records via api (#259)
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user