namecheap/ts/domains-dns.ts
2025-04-02 15:19:18 +00:00

246 lines
7.0 KiB
TypeScript

/**
* Namecheap API Client - Domains DNS Module
*/
import { HttpClient } from './http-client.js';
import type {
IDnsDomainHostsResponse,
IDnsHost,
IDnsSetHostsResponse,
IDnsSetCustomResponse,
IDnsGetEmailForwardingResponse,
IDnsSetEmailForwardingResponse,
IDnsGetListResponse,
IDnsRecord
} from './types.js';
export class DomainsDns {
private client: HttpClient;
/**
* Create a new Domains DNS API handler
* @param client HTTP client instance
*/
constructor(client: HttpClient) {
this.client = client;
}
/**
* Get a list of DNS hosts for a domain
* @param domainName Domain name to get hosts for
* @param sld Second-level domain (optional)
* @param tld Top-level domain (optional)
* @returns Array of DNS host records
*/
async getHosts(domainName: string, sld?: string, tld?: string): Promise<IDnsHost[]> {
// Prepare the request parameters
const params: Record<string, string> = {};
// If SLD and TLD are provided, use them instead of DomainName
if (sld && tld) {
params.SLD = sld;
params.TLD = tld;
} else {
params.DomainName = domainName;
}
// Make the API request
const response = await this.client.request<IDnsDomainHostsResponse>(
'namecheap.domains.dns.getHosts',
params
);
// Extract host records from the response
const commandResponse = response.ApiResponse.CommandResponse[0];
const hosts = commandResponse.DomainDNSGetHostsResult[0].host || [];
// Convert the parsed XML structure to a more usable format
return hosts.map(host => ({
id: parseInt(host.$.HostId, 10),
name: host.$.Name,
type: host.$.Type,
address: host.$.Address,
mxPref: host.$.MXPref ? parseInt(host.$.MXPref, 10) : 0,
ttl: parseInt(host.$.TTL, 10),
isActive: host.$.IsActive?.toLowerCase() === 'true',
isDDNSEnabled: host.$.IsDDNSEnabled?.toLowerCase() === 'true'
}));
}
/**
* Set DNS host records for a domain
* @param domainName Domain name to set hosts for
* @param hosts Array of host records to set
* @param sld Second-level domain (optional)
* @param tld Top-level domain (optional)
* @returns Success status
*/
async setHosts(
domainName: string,
hosts: Array<{
hostName: string;
recordType: 'A' | 'AAAA' | 'CNAME' | 'MX' | 'TXT' | 'URL' | 'FRAME' | 'NS';
address: string;
mxPref?: number;
ttl?: number;
}>,
sld?: string,
tld?: string
): Promise<boolean> {
// Prepare the request parameters
const params: Record<string, string | number> = {};
// If SLD and TLD are provided, use them instead of DomainName
if (sld && tld) {
params.SLD = sld;
params.TLD = tld;
} else {
params.DomainName = domainName;
}
// Add host records to the parameters
hosts.forEach((host, index) => {
params[`HostName${index + 1}`] = host.hostName;
params[`RecordType${index + 1}`] = host.recordType;
params[`Address${index + 1}`] = host.address;
if (host.mxPref !== undefined) {
params[`MXPref${index + 1}`] = host.mxPref;
}
if (host.ttl !== undefined) {
params[`TTL${index + 1}`] = host.ttl;
}
});
// Make the API request
const response = await this.client.request<IDnsSetHostsResponse>(
'namecheap.domains.dns.setHosts',
params
);
// Check if the operation was successful
return response.ApiResponse.$.Status === 'OK';
}
/**
* Set custom nameservers for a domain
* @param domainName Domain name to set nameservers for
* @param nameservers Array of nameserver hostnames
* @param sld Second-level domain (optional)
* @param tld Top-level domain (optional)
* @returns Success status
*/
async setCustom(
domainName: string,
nameservers: string[],
sld?: string,
tld?: string
): Promise<boolean> {
// Prepare the request parameters
const params: Record<string, string> = {};
// If SLD and TLD are provided, use them instead of DomainName
if (sld && tld) {
params.SLD = sld;
params.TLD = tld;
} else {
params.DomainName = domainName;
}
// Add nameservers to the parameters
params.Nameservers = nameservers.join(',');
// Make the API request
const response = await this.client.request<IDnsSetCustomResponse>(
'namecheap.domains.dns.setCustom',
params
);
// Check if the operation was successful
return response.ApiResponse.$.Status === 'OK';
}
/**
* Get email forwarding settings for a domain
* @param domainName Domain name to get email forwarding for
* @returns Email forwarding settings
*/
async getEmailForwarding(domainName: string): Promise<Array<{
from: string;
to: string;
}>> {
// Make the API request
const response = await this.client.request<IDnsGetEmailForwardingResponse>(
'namecheap.domains.dns.getEmailForwarding',
{ DomainName: domainName }
);
// Extract email forwarding settings from the response
const commandResponse = response.ApiResponse.CommandResponse[0];
const forwardings = commandResponse.DomainDNSGetEmailForwardingResult[0].forward || [];
// Convert the parsed XML structure to a more usable format
return forwardings.map(forward => ({
from: forward.$.from,
to: forward.$.to
}));
}
/**
* Set email forwarding settings for a domain
* @param domainName Domain name to set email forwarding for
* @param forwardings Array of email forwarding settings
* @returns Success status
*/
async setEmailForwarding(
domainName: string,
forwardings: Array<{
from: string;
to: string;
}>
): Promise<boolean> {
// Prepare the request parameters
const params: Record<string, string> = {
DomainName: domainName
};
// Add email forwarding settings to the parameters
forwardings.forEach((forward, index) => {
params[`MailBox${index + 1}`] = forward.from;
params[`ForwardTo${index + 1}`] = forward.to;
});
// Make the API request
const response = await this.client.request<IDnsSetEmailForwardingResponse>(
'namecheap.domains.dns.setEmailForwarding',
params
);
// Check if the operation was successful
return response.ApiResponse.$.Status === 'OK';
}
/**
* Get a list of DNS servers for a domain
* @param sld Second-level domain
* @param tld Top-level domain
* @returns Array of DNS server names
*/
async getList(sld: string, tld: string): Promise<string[]> {
// Make the API request
const response = await this.client.request<IDnsGetListResponse>(
'namecheap.domains.dns.getList',
{
SLD: sld,
TLD: tld
}
);
// Extract DNS server list from the response
const commandResponse = response.ApiResponse.CommandResponse[0];
const dnsServers = commandResponse.DomainDNSGetListResult[0].Nameserver || [];
return dnsServers;
}
}