UNPKG

netcup-node

Version:

A node wrapper for the Netcup CCP API (https://www.netcup-wiki.de/wiki/CCP_API)

168 lines (167 loc) 5.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NetcupApi = exports.NetcupRestApi = void 0; const tslib_1 = require("tslib"); const Formats_1 = require("./@types/Formats"); const api_1 = tslib_1.__importDefault(require("./api")); exports.NetcupRestApi = api_1.default; const constants_1 = require("./constants"); const utils_1 = require("./utils"); class NetcupApi { authData = { apiKey: '', apiPassword: '', customerNumber: '', apiSessionId: '', }; restApi = new api_1.default(); async checkAndRefreshAuth() { if ((0, utils_1.missingAuth)(this.authData)) { throw new Error(constants_1.NOT_INITIALIZED_ERROR); } else { await this.init({ format: this.restApi.format, apikey: this.authData.apiKey, apipassword: this.authData.apiPassword, customernumber: this.authData.customerNumber, }); } } /** * Initializes authentication parameters */ async init(params) { if (params.format && !Object.values(Formats_1.Formats).includes(params.format)) { throw new Error(constants_1.INVALID_FORMAT_ERROR); } this.restApi.format = params.format || Formats_1.Formats.JSON; const res = await this.restApi.login(params); this.authData.apiKey = params.apikey; this.authData.apiPassword = params.apipassword; this.authData.customerNumber = params.customernumber; this.authData.apiSessionId = res.responsedata.apisessionid; return this; } /** * Returns information about the DNS zone of a domain * @example await api.infoDnsZone({ domainname: 'example.com' }) */ async infoDnsZone(params) { await this.checkAndRefreshAuth(); return this.restApi.infoDnsZone({ apisessionid: this.authData.apiSessionId, customernumber: this.authData.customerNumber, apikey: this.authData.apiKey, ...params, }); } /** * Returns information about the DNS records of a domain * @example await api.infoDnsRecords({ domainname: 'example.com' }) */ async infoDnsRecords(params) { await this.checkAndRefreshAuth(); return this.restApi.infoDnsRecords({ apisessionid: this.authData.apiSessionId, customernumber: this.authData.customerNumber, apikey: this.authData.apiKey, ...params, }); } /** * Updates DNS records of a domain and only those that are specified. * @example * await api.updateDnsRecords({ domainname: 'example.com', dnsrecordset: { dnsrecords: [{ name: 'www', type: 'A', destination: 'some-ip' }], }, }) * @example * // can also be used to delete records * await api.updateDnsRecords({ domainname: 'example.com', dnsrecordset: { dnsrecords: [ { name: 'www', type: 'A', destination: 'some-ip', deleterecord: true }, ], }, }) */ async updateDnsRecords(params) { await this.checkAndRefreshAuth(); return this.restApi.updateDnsRecords({ apisessionid: this.authData.apiSessionId, customernumber: this.authData.customerNumber, apikey: this.authData.apiKey, ...params, }); } /** * Updates DNS records of a domain with the current public ip. * @example * // update ipv4 only * await api.updateDnsRecordsWithCurrentIp({ domainname: 'example.com', hostname: 'www', }) * @example * // update ipv4 and ipv6 * await api.updateDnsRecordsWithCurrentIp({ domainname: 'example.com', hostname: 'www', useIpv4AndIpv6: true, }) * @example * // update ipv6 only * await api.updateDnsRecordsWithCurrentIp({ domainname: 'example.com', hostname: 'www', useIpv6Only: true, }) */ async updateDnsRecordWithCurrentIp(params) { const publicIp = (await Promise.resolve().then(() => tslib_1.__importStar(require('public-ip')))).default; if (params.useIpv6Only) { const ipv6 = await publicIp.v6({ onlyHttps: true }); const ipv6Record = { type: 'AAAA', hostname: params.hostname, destination: ipv6, }; return this.updateDnsRecords({ domainname: params.domainname, dnsrecordset: { dnsrecords: [ipv6Record] }, }); } const ipv4 = await publicIp.v4({ onlyHttps: true }); const ipv4Record = { type: 'A', hostname: params.hostname, destination: ipv4, }; if (params.useIpv4AndIpv6) { const ipv6 = await publicIp.v6({ onlyHttps: true }); const ipv6Record = { type: 'AAAA', hostname: params.hostname, destination: ipv6, }; return this.updateDnsRecords({ domainname: params.domainname, dnsrecordset: { dnsrecords: [ipv4Record, ipv6Record] }, }); } return this.updateDnsRecords({ dnsrecordset: { dnsrecords: [ipv4Record] }, domainname: params.domainname, }); } getAuthData() { return this.authData; } } exports.NetcupApi = NetcupApi; tslib_1.__exportStar(require("./@types"), exports); exports.default = NetcupApi;