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

262 lines
7.6 KiB
TypeScript

/**
* Namecheap API Client - Domains Transfer Module
*/
import { HttpClient } from './http-client.js';
import type {
ITransferGetListResponse,
ITransferGetStatusResponse,
ITransferCreateResponse,
ITransferUpdateStatusResponse,
ITransferGetInfoResponse,
ITransferInfo,
ITransferStatusInfo
} from './types.js';
export class DomainsTransfer {
private client: HttpClient;
/**
* Create a new Domains Transfer API handler
* @param client HTTP client instance
*/
constructor(client: HttpClient) {
this.client = client;
}
/**
* Get a list of domain transfers
* @param page Page number
* @param pageSize Number of items per page
* @param listType Type of transfers to list
* @returns List of domain transfers
*/
async getList(
page: number = 1,
pageSize: number = 20,
listType: 'ALL' | 'INPROGRESS' | 'COMPLETED' | 'FAILED' = 'ALL'
): Promise<{
transfers: ITransferInfo[];
paging: {
totalItems: number;
currentPage: number;
pageSize: number;
};
}> {
// Make the API request
const response = await this.client.request<ITransferGetListResponse>(
'namecheap.domains.transfer.getList',
{
Page: page,
PageSize: pageSize,
ListType: listType
}
);
// Extract transfer information from the response
const commandResponse = response.ApiResponse.CommandResponse[0];
const transferList = commandResponse.TransferGetListResult[0];
const paging = commandResponse.Paging[0];
// Convert the parsed XML structure to a more usable format
const transfers = (transferList.Transfer || []).map(transfer => ({
id: parseInt(transfer.$.ID, 10),
domainName: transfer.$.DomainName,
status: transfer.$.Status,
statusDescription: transfer.$.StatusDescription,
date: transfer.$.Date,
lockTransferDate: transfer.$.LockTransferDate || null,
transferOrderDetailId: parseInt(transfer.$.TransferOrderDetailID || '0', 10),
isLocked: transfer.$.IsLocked?.toLowerCase() === 'true',
authInfo: transfer.$.AuthInfo || null
}));
// Convert paging information
const pagingInfo = paging ? {
totalItems: parseInt(paging.TotalItems[0], 10),
currentPage: parseInt(paging.CurrentPage[0], 10),
pageSize: parseInt(paging.PageSize[0], 10)
} : {
totalItems: transfers.length,
currentPage: 1,
pageSize: transfers.length
};
return {
transfers,
paging: pagingInfo
};
}
/**
* Get the status of a domain transfer
* @param transferId Transfer ID
* @returns Transfer status information
*/
async getStatus(transferId: number): Promise<ITransferStatusInfo> {
// Make the API request
const response = await this.client.request<ITransferGetStatusResponse>(
'namecheap.domains.transfer.getStatus',
{ TransferID: transferId }
);
// Extract status information from the response
const commandResponse = response.ApiResponse.CommandResponse[0];
const statusInfo = commandResponse.TransferGetStatusResult[0].$;
return {
id: parseInt(statusInfo.TransferID, 10),
status: statusInfo.Status,
statusDescription: statusInfo.StatusDescription,
domainName: statusInfo.DomainName,
date: statusInfo.Date,
lockTransferDate: statusInfo.LockTransferDate || null,
transferOrderDetailId: parseInt(statusInfo.TransferOrderDetailID || '0', 10),
isLocked: statusInfo.IsLocked?.toLowerCase() === 'true',
authInfo: statusInfo.AuthInfo || null
};
}
/**
* Create a new domain transfer
* @param domainName Domain name to transfer
* @param years Number of years to renew for
* @param authCode Authorization code from current registrar
* @param options Additional options
* @returns Transfer creation result
*/
async create(
domainName: string,
years: number,
authCode: string,
options?: {
promotionCode?: string;
addFreeWhoisguard?: boolean;
whoisguardEnable?: boolean;
idnCode?: string;
premiumPrice?: number;
eapFee?: number;
}
): Promise<{
transferId: number;
transferOrderId: number;
isSuccess: boolean;
transferStatus: string;
statusDescription: string;
chargedAmount: number;
}> {
// Prepare the request parameters
const params: Record<string, string | number | boolean> = {
DomainName: domainName,
Years: years,
EPPCode: authCode
};
// Add optional parameters
if (options) {
if (options.promotionCode) {
params.PromotionCode = options.promotionCode;
}
if (options.addFreeWhoisguard !== undefined) {
params.AddFreeWhoisguard = options.addFreeWhoisguard;
}
if (options.whoisguardEnable !== undefined) {
params.WGEnable = options.whoisguardEnable;
}
if (options.idnCode) {
params.IdnCode = options.idnCode;
}
if (options.premiumPrice !== undefined) {
params.PremiumPrice = options.premiumPrice;
}
if (options.eapFee !== undefined) {
params.EapFee = options.eapFee;
}
}
// Make the API request
const response = await this.client.request<ITransferCreateResponse>(
'namecheap.domains.transfer.create',
params
);
// Extract creation information from the response
const commandResponse = response.ApiResponse.CommandResponse[0];
const result = commandResponse.DomainTransferCreateResult[0].$;
return {
transferId: parseInt(result.TransferID, 10),
transferOrderId: parseInt(result.OrderID, 10),
isSuccess: result.IsSuccess?.toLowerCase() === 'true',
transferStatus: result.Status,
statusDescription: result.StatusDescription,
chargedAmount: parseFloat(result.ChargedAmount)
};
}
/**
* Update the status of a domain transfer
* @param transferId Transfer ID
* @param resubmit Whether to resubmit the transfer
* @returns Success status
*/
async updateStatus(transferId: number, resubmit: boolean): Promise<boolean> {
// Make the API request
const response = await this.client.request<ITransferUpdateStatusResponse>(
'namecheap.domains.transfer.updateStatus',
{
TransferID: transferId,
Resubmit: resubmit
}
);
// Check if the operation was successful
return response.ApiResponse.$.Status === 'OK';
}
/**
* Get detailed information about a domain transfer
* @param transferId Transfer ID
* @returns Detailed transfer information
*/
async getInfo(transferId: number): Promise<{
transferId: number;
domainName: string;
status: string;
statusDescription: string;
authInfo: string;
date: string;
whoisguardStatus: string;
orderDate: string;
orderId: number;
transferOrderDetailId: number;
}> {
// Make the API request
const response = await this.client.request<ITransferGetInfoResponse>(
'namecheap.domains.transfer.getInfo',
{ TransferID: transferId }
);
// Extract detailed information from the response
const commandResponse = response.ApiResponse.CommandResponse[0];
const info = commandResponse.TransferGetInfoResult[0].$;
return {
transferId: parseInt(info.TransferID, 10),
domainName: info.DomainName,
status: info.Status,
statusDescription: info.StatusDescription,
authInfo: info.AuthInfo || '',
date: info.Date,
whoisguardStatus: info.WhoisguardStatus || '',
orderDate: info.OrderDate,
orderId: parseInt(info.OrderID, 10),
transferOrderDetailId: parseInt(info.TransferOrderDetailID, 10)
};
}
}