UNPKG

netcup-node

Version:

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

163 lines (162 loc) 5.38 kB
import { Formats } from './@types/Formats.js'; import NetcupRestApi from './api.js'; import { INVALID_FORMAT_ERROR, NOT_INITIALIZED_ERROR } from './constants.js'; import { missingAuth } from './utils.js'; class NetcupApi { authData = { apiKey: '', apiPassword: '', customerNumber: '', apiSessionId: '', }; restApi = new NetcupRestApi(); async checkAndRefreshAuth() { if (missingAuth(this.authData)) { throw new Error(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).includes(params.format)) { throw new Error(INVALID_FORMAT_ERROR); } this.restApi.format = params.format || 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 import('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; } } export * from './@types/index.js'; export { NetcupRestApi, NetcupApi }; export default NetcupApi;