namecheap-ts
Version:
A simple wrapper for namecheap API
325 lines (317 loc) • 12.4 kB
JavaScript
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __objRest = (source, exclude) => {
var target = {};
for (var prop in source)
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
target[prop] = source[prop];
if (source != null && __getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(source)) {
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
target[prop] = source[prop];
}
return target;
};
var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
};
var rejected = (value) => {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
};
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
step((generator = generator.apply(__this, __arguments)).next());
});
};
// src/commands.ts
var Commands = /* @__PURE__ */ ((Commands2) => {
Commands2["DOMAINS_GETLIST"] = "namecheap.domains.getList";
Commands2["DOMAINS_GETCONTACTS"] = "namecheap.domains.getContacts";
Commands2["DOMAINS_CREATE"] = "namecheap.domains.create";
Commands2["DOMAINS_GETTLDLIST"] = "namecheap.domains.getTldList";
Commands2["DOMAINS_SETCONTACTS"] = "namecheap.domains.setContacts";
Commands2["DOMAINS_CHECK"] = "namecheap.domains.check";
Commands2["DOMAINS_REACTIVATE"] = "namecheap.domains.reactivate";
Commands2["DOMAINS_RENEW"] = "namecheap.domains.renew";
Commands2["DOMAINS_GETREGISTRARLOCK"] = "namecheap.domains.getRegistrarLock";
Commands2["DOMAINS_SETREGISTRARLOCK"] = "namecheap.domains.setRegistrarLock";
Commands2["DOMAINS_GETINFO"] = "namecheap.domains.getInfo";
Commands2["DOMAINS_DNS_SETDEFAULT"] = "namecheap.domains.dns.setDefault";
Commands2["DOMAINS_DNS_SETCUSTOM"] = "namecheap.domains.dns.setCustom";
Commands2["DOMAINS_DNS_GETLIST"] = "namecheap.domains.dns.getList";
Commands2["DOMAINS_DNS_GETHOSTS"] = "namecheap.domains.dns.getHosts";
Commands2["DOMAINS_DNS_GETEMAILFORWARDING"] = "namecheap.domains.dns.getEmailForwarding";
Commands2["DOMAINS_DNS_SETEMAILFORWARDING"] = "namecheap.domains.dns.setEmailForwarding";
Commands2["DOMAINS_DNS_SETHOSTS"] = "namecheap.domains.dns.setHosts";
Commands2["DOMAINS_NS_CREATE"] = "namecheap.domains.ns.create";
Commands2["DOMAINS_NS_DELETE"] = "namecheap.domains.ns.delete";
Commands2["DOMAINS_NS_GETINFO"] = "namecheap.domains.ns.getInfo";
Commands2["DOMAINS_NS_UPDATE"] = "namecheap.domains.ns.update";
Commands2["DOMAINS_TRANSFER_CREATE"] = "namecheap.domains.transfer.create";
Commands2["DOMAINS_TRANSFER_GETSTATUS"] = "namecheap.domains.transfer.getStatus";
Commands2["DOMAINS_TRANSFER_UPDATESTATUS"] = "namecheap.domains.transfer.updateStatus";
Commands2["DOMAINS_TRANSFER_GETLIST"] = "namecheap.domains.transfer.getList";
Commands2["SSL_CREATE"] = "namecheap.ssl.create";
Commands2["SSL_GETLIST"] = "namecheap.ssl.getList";
Commands2["SSL_PARSECSR"] = "namecheap.ssl.parseCSR";
Commands2["SSL_GETAPPROVEREMAILLIST"] = "namecheap.ssl.getApproverEmailList";
Commands2["SSL_ACTIVATE"] = "namecheap.ssl.activate";
Commands2["SSL_RESENDAPPROVEREMAIL"] = "namecheap.ssl.resendApproverEmail";
Commands2["SSL_GETINFO"] = "namecheap.ssl.getInfo";
Commands2["SSL_RENEW"] = "namecheap.ssl.renew";
Commands2["SSL_REISSUE"] = "namecheap.ssl.reissue";
Commands2["SSL_RESENDFULFILLMENTEMAIL"] = "namecheap.ssl.resendfulfillmentemail";
Commands2["SSL_PURCHASEMORESANS"] = "namecheap.ssl.purchasemoresans";
Commands2["SSL_REVOKECERTIFICATE"] = "namecheap.ssl.revokecertificate";
Commands2["SSL_EDITDCVMETHOD"] = "namecheap.ssl.editDCVMethod";
Commands2["USERS_GETPRICING"] = "namecheap.users.getPricing";
Commands2["USERS_GETBALANCES"] = "namecheap.users.getBalances";
Commands2["USERS_CHANGEPASSWORD"] = "namecheap.users.changePassword";
Commands2["USERS_UPDATE"] = "namecheap.users.update";
Commands2["USERS_CREATEADDFUNDSREQUEST"] = "namecheap.users.createaddfundsrequest";
Commands2["USERS_GETADDFUNDSSTATUS"] = "namecheap.users.getAddFundsStatus";
Commands2["USERS_CREATE"] = "namecheap.users.create";
Commands2["USERS_LOGIN"] = "namecheap.users.login";
Commands2["USERS_RESETPASSWORD"] = "namecheap.users.resetPassword";
Commands2["USERS_ADDRESS_CREATE"] = "namecheap.users.address.create";
Commands2["USERS_ADDRESS_DELETE"] = "namecheap.users.address.delete";
Commands2["USERS_ADDRESS_GETINFO"] = "namecheap.users.address.getInfo";
Commands2["USERS_ADDRESS_GETLIST"] = "namecheap.users.address.getList";
Commands2["USERS_ADDRESS_SETDEFAULT"] = "namecheap.users.address.setDefault";
Commands2["USERS_ADDRESS_UPDATE"] = "namecheap.users.address.update";
Commands2["WHOISGUARD_CHANGEEMAILADDRESS"] = "Namecheap.Whoisguard.changeemailaddress";
Commands2["WHOISGUARD_ENABLE"] = "Namecheap.Whoisguard.enable";
Commands2["WHOISGUARD_DISABLE"] = "Namecheap.Whoisguard.disable";
Commands2["WHOISGUARD_GETLIST"] = "Namecheap.Whoisguard.getList";
Commands2["WHOISGUARD_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);
}
get(url) {
return __async(this, null, function* () {
return yield this._client.get(url);
});
}
post(url) {
return __async(this, null, function* () {
return yield this._client.post(url);
});
}
onResponse(response) {
return __async(this, null, function* () {
try {
const result = yield 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);
}
call(command, payload) {
return __async(this, null, function* () {
const _a = this.config, { username } = _a, config = __objRest(_a, ["username"]);
const params = __spreadProps(__spreadValues(__spreadValues({
username
}, payload), config), {
command
});
const url = "?" + new URLSearchParams(params).toString();
const { data, status } = yield this.apiClient.post(url);
return { data, status };
});
}
checkDomain(domainName) {
return __async(this, null, function* () {
const { data, status } = yield this.call("namecheap.domains.check" /* DOMAINS_CHECK */, {
DomainList: domainName
});
const response = {
data: {
availabe: data.DomainCheckResult.$.Available,
premium: data.DomainCheckResult.$.IsPremiumName
},
status
};
return response;
});
}
getDomainPrice(domainName, action) {
return __async(this, null, function* () {
var _a, _b, _c, _d;
const [_, tld] = domainName.split(".");
if (!tld) {
throw new InvalidDomainNameException(domainName);
}
const { data, status } = yield this.call("namecheap.users.getPricing" /* USERS_GETPRICING */, {
ProductType: "DOMAIN",
ProductCategory: "DOMAINS",
ActionName: action,
ProductName: tld
});
const pricing = (((_d = (_c = (_b = (_a = data.UserGetPricingResult) == null ? void 0 : _a.ProductType) == null ? void 0 : _b.ProductCategory) == null ? void 0 : _c.Product) == null ? void 0 : _d.Price) || []).map((price) => __spreadValues({}, price.$));
return { data: pricing, status };
});
}
registerDomain(payload) {
return __async(this, null, function* () {
var _a;
const { data, status } = yield this.call("namecheap.domains.create" /* DOMAINS_CREATE */, payload);
const result = (_a = data == null ? void 0 : data.DomainCreateResult) == null ? void 0 : _a.$;
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;
});
}
addFundsRequest(payload) {
return __async(this, null, function* () {
const { data, status } = yield this.call(
"namecheap.users.createaddfundsrequest" /* USERS_CREATEADDFUNDSREQUEST */,
payload
);
const result = data.Createaddfundsrequestresult.$;
const response = {
data: {
tokenId: result.TokenID,
returnURL: result.ReturnURL,
redirectURL: result.RedirectURL
},
status
};
return response;
});
}
getFundsStatus(tokenId) {
return __async(this, null, function* () {
const { data, status } = yield this.call("namecheap.users.getAddFundsStatus" /* USERS_GETADDFUNDSSTATUS */, {
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