it/web/app/api/free/dns-health/route.ts
2025-10-25 20:37:00 +02:00

23 lines
1.2 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
import { promises as dns } from "dns";
import { isValidDomain, normalizeDomain } from "@/lib/validators";
export const runtime = "nodejs";
export async function POST(req: NextRequest){
try{
const {domain} = await req.json();
const d = normalizeDomain(String(domain||""));
if(!isValidDomain(d)) return NextResponse.json({ok:false,error:"Invalid domain."},{status:400});
const res:any={ok:true,domain:d,notes:[]};
try{ res.a=await dns.resolve4(d);}catch{}
try{ res.aaaa=await dns.resolve6(d);}catch{}
try{ res.mx=(await dns.resolveMx(d)).sort((a,b)=>a.priority-b.priority);}catch{}
try{ res.ns=await dns.resolveNs(d);}catch{}
try{ const caa=(dns as any).resolveCaa?await (dns as any).resolveCaa(d):null; if(caa) res.caa=caa;}catch{}
if(!(res.a?.length||res.aaaa?.length)) res.notes.push("No A/AAAA at apex.");
if(!res.mx?.length) res.notes.push("No MX records found.");
if(!res.caa?.length) res.notes.push("No CAA records.");
if(!res.ns?.length) res.notes.push("No NS records detected.");
return NextResponse.json(res);
}catch{ return NextResponse.json({ok:false,error:"Unexpected error."},{status:500}); }
}