namecheap-ts
Version:
A simple wrapper for namecheap API
261 lines (254 loc) • 9.93 kB
JavaScript
// src/commands.ts
var Commands = /* @__PURE__ */ ((Commands2) => {
Commands2["DOMAINS_GET_LIST"] = "namecheap.domains.getList";
Commands2["DOMAINS_GET_CONTACTS"] = "namecheap.domains.getContacts";
Commands2["DOMAINS_CREATE"] = "namecheap.domains.create";
Commands2["DOMAINS_GET_TLD_LIST"] = "namecheap.domains.getTldList";
Commands2["DOMAINS_SET_CONTACTS"] = "namecheap.domains.setContacts";
Commands2["DOMAINS_CHECK"] = "namecheap.domains.check";
Commands2["DOMAINS_REACTIVATE"] = "namecheap.domains.reactivate";
Commands2["DOMAINS_RENEW"] = "namecheap.domains.renew";
Commands2["DOMAINS_GET_REGISTRAR_LOCK"] = "namecheap.domains.getRegistrarLock";
Commands2["DOMAINS_SET_REGISTRAR_LOCK"] = "namecheap.domains.setRegistrarLock";
Commands2["DOMAINS_GET_INFO"] = "namecheap.domains.getInfo";
Commands2["DOMAINS_DNS_SET_DEFAULT"] = "namecheap.domains.dns.setDefault";
Commands2["DOMAINS_DNS_SET_CUSTOM"] = "namecheap.domains.dns.setCustom";
Commands2["DOMAINS_DNS_GET_LIST"] = "namecheap.domains.dns.getList";
Commands2["DOMAINS_DNS_GET_HOSTS"] = "namecheap.domains.dns.getHosts";
Commands2["DOMAINS_DNS_GET_EMAIL_FORWARDING"] = "namecheap.domains.dns.getEmailForwarding";
Commands2["DOMAINS_DNS_SET_EMAIL_FORWARDING"] = "namecheap.domains.dns.setEmailForwarding";
Commands2["DOMAINS_DNS_SET_HOSTS"] = "namecheap.domains.dns.setHosts";
Commands2["DOMAINS_NS_CREATE"] = "namecheap.domains.ns.create";
Commands2["DOMAINS_NS_DELETE"] = "namecheap.domains.ns.delete";
Commands2["DOMAINS_NS_GET_INFO"] = "namecheap.domains.ns.getInfo";
Commands2["DOMAINS_NS_UPDATE"] = "namecheap.domains.ns.update";
Commands2["DOMAINS_TRANSFER_CREATE"] = "namecheap.domains.transfer.create";
Commands2["DOMAINS_TRANSFER_GET_STATUS"] = "namecheap.domains.transfer.getStatus";
Commands2["DOMAINS_TRANSFER_UPDATE_STATUS"] = "namecheap.domains.transfer.updateStatus";
Commands2["DOMAINS_TRANSFER_GET_LIST"] = "namecheap.domains.transfer.getList";
Commands2["SSL_CREATE"] = "namecheap.ssl.create";
Commands2["SSL_GET_LIST"] = "namecheap.ssl.getList";
Commands2["SSL_PARSE_CSR"] = "namecheap.ssl.parseCSR";
Commands2["SSL_GET_APPROVER_EMAIL_LIST"] = "namecheap.ssl.getApproverEmailList";
Commands2["SSL_ACTIVATE"] = "namecheap.ssl.activate";
Commands2["SSL_RESEND_APPROVER_EMAIL"] = "namecheap.ssl.resendApproverEmail";
Commands2["SSL_GET_INFO"] = "namecheap.ssl.getInfo";
Commands2["SSL_RENEW"] = "namecheap.ssl.renew";
Commands2["SSL_REISSUE"] = "namecheap.ssl.reissue";
Commands2["SSL_RESEND_FULFILLMENT_EMAIL"] = "namecheap.ssl.resendfulfillmentemail";
Commands2["SSL_PURCHASE_MORE_SANS"] = "namecheap.ssl.purchasemoresans";
Commands2["SSL_REVOKE_CERTIFICATE"] = "namecheap.ssl.revokecertificate";
Commands2["SSL_EDIT_DCV_METHOD"] = "namecheap.ssl.editDCVMethod";
Commands2["USERS_GET_PRICING"] = "namecheap.users.getPricing";
Commands2["USERS_GET_BALANCES"] = "namecheap.users.getBalances";
Commands2["USERS_CHANGE_PASSWORD"] = "namecheap.users.changePassword";
Commands2["USERS_UPDATE"] = "namecheap.users.update";
Commands2["USERS_CREATE_ADD_FUNDS_REQUEST"] = "namecheap.users.createaddfundsrequest";
Commands2["USERS_GET_ADD_FUNDS_STATUS"] = "namecheap.users.getAddFundsStatus";
Commands2["USERS_CREATE"] = "namecheap.users.create";
Commands2["USERS_LOGIN"] = "namecheap.users.login";
Commands2["USERS_RESET_PASSWORD"] = "namecheap.users.resetPassword";
Commands2["USERS_ADDRESS_CREATE"] = "namecheap.users.address.create";
Commands2["USERS_ADDRESS_DELETE"] = "namecheap.users.address.delete";
Commands2["USERS_ADDRESS_GET_INFO"] = "namecheap.users.address.getInfo";
Commands2["USERS_ADDRESS_GET_LIST"] = "namecheap.users.address.getList";
Commands2["USERS_ADDRESS_SET_DEFAULT"] = "namecheap.users.address.setDefault";
Commands2["USERS_ADDRESS_UPDATE"] = "namecheap.users.address.update";
Commands2["WHO_IS_GUARD_CHANGE_EMAIL_ADDRESS"] = "Namecheap.Whoisguard.changeemailaddress";
Commands2["WHO_IS_GUARD_ENABLE"] = "Namecheap.Whoisguard.enable";
Commands2["WHO_IS_GUARD_DISABLE"] = "Namecheap.Whoisguard.disable";
Commands2["WHO_IS_GUARD_GET_LIST"] = "Namecheap.Whoisguard.getList";
Commands2["WHO_IS_GUARD_RENEW"] = "Namecheap.Whoisguard.renew";
return Commands2;
})(Commands || {});
// src/types.ts
var DomainPriceActions = /* @__PURE__ */ ((DomainPriceActions2) => {
DomainPriceActions2["REGISTER"] = "REGISTER";
DomainPriceActions2["RENEW"] = "RENEW";
DomainPriceActions2["REACTIVATE"] = "REACTIVATE";
DomainPriceActions2["TRANSFER"] = "TRANSFER";
return DomainPriceActions2;
})(DomainPriceActions || {});
// src/api/api-client.ts
import axios from "axios";
// src/api/errors.ts
var XMLParsingError = class extends Error {
constructor() {
super("Invalid XML response");
this.name = "XMLParsingError";
}
};
var ResponseError = class extends Error {
constructor(message, code) {
super((code ? `${code}: ` : "") + message);
this.code = code;
this.name = "ResponseError";
}
};
// src/api/utils.ts
import { parseStringPromise } from "xml2js";
function parseXMLString(xml) {
return parseStringPromise(xml, {
explicitArray: false,
attrValueProcessors: [transformValues]
});
}
function transformValues(value, name) {
if (value === "false" || value === "true") {
return value === "true";
}
if (!isNaN(+value)) {
return +value;
}
return value;
}
// src/api/api-client.ts
var APIClient = class {
constructor(baseURL) {
this._client = axios.create({ baseURL });
this._client.interceptors.response.use(this.onResponse);
}
async get(url) {
return await this._client.get(url);
}
async post(url) {
return await this._client.post(url);
}
async onResponse(response) {
try {
const result = await parseXMLString(response.data);
if (result.ApiResponse.$.Status === "ERROR") {
const responseErrors = result.ApiResponse.Errors;
const code = responseErrors.Error.$.Number;
const message = responseErrors.Error._;
throw new ResponseError(message, code);
}
response.data = result.ApiResponse.CommandResponse;
} catch (err) {
response.status = 400;
if (err instanceof ResponseError) {
throw err;
}
throw new XMLParsingError();
}
return response;
}
};
var api_client_default = APIClient;
// src/exceptions/InvalidDomainNameException.ts
var InvalidDomainNameException = class extends Error {
constructor(domainName) {
super(`The provided domain name [${domainName}] is not valid`);
this.name = "InvalidDomainNameException";
}
};
// src/namecheap.ts
var Namecheap = class {
constructor(config, sandbox) {
this.config = config;
const baseURL = `https://api${sandbox ? ".sandbox" : ""}.namecheap.com/xml.response`;
this.apiClient = new api_client_default(baseURL);
}
async call(command, payload) {
const { username, ...config } = this.config;
const params = {
username,
...payload,
...config,
command
};
const url = "?" + new URLSearchParams(params).toString();
const { data, status } = await this.apiClient.post(url);
return { data, status };
}
async checkDomain(domainName) {
const { data, status } = await this.call("namecheap.domains.check" /* DOMAINS_CHECK */, {
DomainList: domainName
});
const response = {
data: {
available: data.DomainCheckResult.$.Available,
premium: data.DomainCheckResult.$.IsPremiumName
},
status
};
return response;
}
async getDomainPrice(domainName, action) {
const [_, tld] = domainName.split(".");
if (!tld) {
throw new InvalidDomainNameException(domainName);
}
const { data, status } = await this.call("namecheap.users.getPricing" /* USERS_GET_PRICING */, {
ProductType: "DOMAIN",
ProductCategory: "DOMAINS",
ActionName: action,
ProductName: tld
});
const price = data.UserGetPricingResult?.ProductType?.ProductCategory?.Product?.Price;
if (!price) {
throw new InvalidDomainNameException(domainName);
}
const pricing = Array.isArray(price) ? price.map((price2) => ({ ...price2.$ })) : [{ ...price.$ }];
return { data: pricing, status };
}
async registerDomain(payload) {
const { data, status } = await this.call("namecheap.domains.create" /* DOMAINS_CREATE */, payload);
const result = data?.DomainCreateResult?.$;
const response = {
data: {
domain: result.Domain,
registered: result.Registered,
chargedAmount: result.ChargedAmount,
domainID: result.DomainID,
orderID: result.OrderID,
transactionID: result.TransactionID,
whoIsGuardEnable: result.WhoisguardEnable,
freePositiveSSL: result.FreePositiveSSL,
nonRealTimeDomain: result.NonRealTimeDomain
},
status
};
return response;
}
async addFundsRequest(payload) {
const { data, status } = await this.call(
"namecheap.users.createaddfundsrequest" /* USERS_CREATE_ADD_FUNDS_REQUEST */,
payload
);
const result = data.Createaddfundsrequestresult.$;
const response = {
data: {
tokenId: result.TokenID,
returnURL: result.ReturnURL,
redirectURL: result.RedirectURL
},
status
};
return response;
}
async getFundsStatus(tokenId) {
const { data, status } = await this.call(
"namecheap.users.getAddFundsStatus" /* USERS_GET_ADD_FUNDS_STATUS */,
{
tokenId
}
);
const result = data.GetAddFundsStatusResult.$;
const response = {
data: {
amount: result.Amount,
status: result.Status
},
status
};
return response;
}
};
var namecheap_default = Namecheap;
export {
Commands,
DomainPriceActions,
namecheap_default as default
};
//# sourceMappingURL=index.mjs.map