/
index.ts
197 lines (184 loc) · 5.49 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { Formats } from './@types/Formats';
import { InitParams } from './@types/InitParams';
import { NetcupAuth } from './@types/NetcupAuth';
import {
InfoDNSRecordsParam,
InfoDNSZoneParam,
UpdateDNSRecordsParam,
UpdateDnsRecordWithCurrentIpParams,
} from './@types/Requests';
import {
DnsRecord,
InfoDNSRecordsResponse,
InfoDNSZoneResponse,
LoginResponse,
UpdateDNSRecordsResponse,
} from './@types/Responses';
import NetcupRestApi from './api';
import { INVALID_FORMAT_ERROR, NOT_INITIALIZED_ERROR } from './constants';
import { missingAuth } from './utils';
class NetcupApi {
private authData: NetcupAuth = {
apiKey: '',
apiPassword: '',
customerNumber: '',
apiSessionId: '',
};
public readonly restApi: NetcupRestApi = new NetcupRestApi();
private 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
*/
public async init(params: InitParams): Promise<NetcupApi> {
if (params.format && !Object.values(Formats).includes(params.format)) {
throw new Error(INVALID_FORMAT_ERROR);
}
this.restApi.format = params.format || Formats.JSON;
const res: LoginResponse = 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' })
*/
public async infoDnsZone(
params: InfoDNSZoneParam,
): Promise<InfoDNSZoneResponse> {
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' })
*/
public async infoDnsRecords(
params: InfoDNSRecordsParam,
): Promise<InfoDNSRecordsResponse> {
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 },
],
},
})
*/
public async updateDnsRecords(
params: UpdateDNSRecordsParam,
): Promise<UpdateDNSRecordsResponse> {
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,
})
*/
public async updateDnsRecordWithCurrentIp(
params: UpdateDnsRecordWithCurrentIpParams,
): Promise<UpdateDNSRecordsResponse> {
const publicIp = (await import('public-ip')).default;
if (params.useIpv6Only) {
const ipv6 = await publicIp.v6({ onlyHttps: true });
const ipv6Record: DnsRecord = {
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: DnsRecord = {
type: 'A',
hostname: params.hostname,
destination: ipv4,
};
if (params.useIpv4AndIpv6) {
const ipv6 = await publicIp.v6({ onlyHttps: true });
const ipv6Record: DnsRecord = {
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,
});
}
public getAuthData(): NetcupAuth {
return this.authData;
}
}
export * from './@types';
export { NetcupRestApi, NetcupApi };
export default NetcupApi;