24fire-api 1.0.8 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -201
- package/README.md +146 -110
- package/package.json +33 -24
- package/src/index.d.ts +135 -0
- package/src/index.js +290 -207
package/src/index.js
CHANGED
|
@@ -1,207 +1,290 @@
|
|
|
1
|
-
//███████╗██╗██████╗ ███████╗ █████╗ ██████╗ ██╗
|
|
2
|
-
//██╔════╝██║██╔══██╗██╔════╝██╔══██╗██╔══██╗██║
|
|
3
|
-
//█████╗ ██║██████╔╝█████╗ ███████║██████╔╝██║
|
|
4
|
-
//██╔══╝ ██║██╔══██╗██╔══╝ ██╔══██║██╔═══╝ ██║
|
|
5
|
-
//██║ ██║██║ ██║███████╗██║ ██║██║ ██║
|
|
6
|
-
//╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝
|
|
7
|
-
//24fire API für
|
|
8
|
-
//
|
|
9
|
-
//Made by
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const
|
|
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
|
-
|
|
198
|
-
|
|
199
|
-
return
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
1
|
+
//███████╗██╗██████╗ ███████╗ █████╗ ██████╗ ██╗
|
|
2
|
+
//██╔════╝██║██╔══██╗██╔════╝██╔══██╗██╔══██╗██║
|
|
3
|
+
//█████╗ ██║██████╔╝█████╗ ███████║██████╔╝██║
|
|
4
|
+
//██╔══╝ ██║██╔══██╗██╔══╝ ██╔══██║██╔═══╝ ██║
|
|
5
|
+
//██║ ██║██║ ██║███████╗██║ ██║██║ ██║
|
|
6
|
+
//╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝
|
|
7
|
+
// 24fire REST-API v2 für Node.js
|
|
8
|
+
// Doku: https://apidocs.24fire.de/v2
|
|
9
|
+
// Made by FlosTechnikwelt <florian.linde@flostechnikwelt.de>
|
|
10
|
+
|
|
11
|
+
'use strict';
|
|
12
|
+
|
|
13
|
+
const BASE_URL = 'https://manage.24fire.de/api';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Fehler der bei einer fehlgeschlagenen API-Anfrage geworfen wird
|
|
17
|
+
* Enthält die HTTP-Statuscode und die rohe API-Antwort
|
|
18
|
+
*/
|
|
19
|
+
class FireAPIError extends Error {
|
|
20
|
+
constructor(message, { status, response } = {}) {
|
|
21
|
+
super(`[24fire] ${message}`);
|
|
22
|
+
this.name = 'FireAPIError';
|
|
23
|
+
this.status = status;
|
|
24
|
+
this.response = response;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Entfernt undefined/null-Werte aus einem Objekt, damit optionale
|
|
30
|
+
* Parameter einfach weggelassen werden können
|
|
31
|
+
*/
|
|
32
|
+
function clean(obj) {
|
|
33
|
+
const out = {};
|
|
34
|
+
for (const [key, value] of Object.entries(obj || {})) {
|
|
35
|
+
if (value !== undefined && value !== null) out[key] = value;
|
|
36
|
+
}
|
|
37
|
+
return out;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Ein Client wird einmalig mit dem API-Key erstellt:
|
|
42
|
+
*
|
|
43
|
+
* const FireAPI = require('24fire-api');
|
|
44
|
+
* const fire = new FireAPI('DEIN_API_KEY');
|
|
45
|
+
*/
|
|
46
|
+
class FireAPI {
|
|
47
|
+
/**
|
|
48
|
+
* @param {string} apiKey
|
|
49
|
+
* @param {object} [options]
|
|
50
|
+
* @param {string} [options.baseUrl] Abweichende Base-URL (Standard: https://manage.24fire.de/api)
|
|
51
|
+
*/
|
|
52
|
+
constructor(apiKey, options = {}) {
|
|
53
|
+
if (!apiKey || typeof apiKey !== 'string') {
|
|
54
|
+
throw new FireAPIError('Es wurde kein gültiger API-Key übergeben');
|
|
55
|
+
}
|
|
56
|
+
if (typeof fetch !== 'function') {
|
|
57
|
+
throw new FireAPIError('Globales fetch nicht verfügbar – bitte Node.js 18 oder neuer verwenden');
|
|
58
|
+
}
|
|
59
|
+
this.apiKey = apiKey;
|
|
60
|
+
this.baseUrl = (options.baseUrl || BASE_URL).replace(/\/$/, '');
|
|
61
|
+
|
|
62
|
+
// Account-Endpunkte
|
|
63
|
+
this.account = {
|
|
64
|
+
/** Account-Informationen */
|
|
65
|
+
info: () => this.request('GET', '/account'),
|
|
66
|
+
/** Übersicht aller aktiven Dienste */
|
|
67
|
+
services: () => this.request('GET', '/account/services'),
|
|
68
|
+
/** Daten zur Spendenseite */
|
|
69
|
+
donations: () => this.request('GET', '/account/donations'),
|
|
70
|
+
/** Daten zum Affiliate-System */
|
|
71
|
+
affiliate: () => this.request('GET', '/account/affiliate'),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Interne Methode für alle HTTP-Anfragen.
|
|
77
|
+
* @param {string} method GET | POST | PUT | DELETE
|
|
78
|
+
* @param {string} path Pfad ab der Base-URL, z.B. "/account"
|
|
79
|
+
* @param {object} [body] Body-Parameter (werden als x-www-form-urlencoded gesendet)
|
|
80
|
+
* @returns {Promise<object>} geparste JSON-Antwort der API
|
|
81
|
+
*/
|
|
82
|
+
async request(method, path, body) {
|
|
83
|
+
const headers = { 'X-Fire-Apikey': this.apiKey };
|
|
84
|
+
const options = { method, headers };
|
|
85
|
+
|
|
86
|
+
const data = clean(body);
|
|
87
|
+
if (Object.keys(data).length > 0) {
|
|
88
|
+
headers['Content-Type'] = 'application/x-www-form-urlencoded';
|
|
89
|
+
options.body = new URLSearchParams(data).toString();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
let res;
|
|
93
|
+
try {
|
|
94
|
+
res = await fetch(this.baseUrl + path, options);
|
|
95
|
+
} catch (err) {
|
|
96
|
+
throw new FireAPIError(`Netzwerkfehler: ${err.message}`);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const text = await res.text();
|
|
100
|
+
let json;
|
|
101
|
+
try {
|
|
102
|
+
json = text ? JSON.parse(text) : null;
|
|
103
|
+
} catch {
|
|
104
|
+
json = text;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (!res.ok || (json && json.status === 'error')) {
|
|
108
|
+
const message = (json && json.message) || res.statusText || 'Unbekannter Fehler';
|
|
109
|
+
throw new FireAPIError(message, { status: res.status, response: json });
|
|
110
|
+
}
|
|
111
|
+
return json;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Liefert einen Client für einen bestimmten KVM-Server
|
|
116
|
+
* @param {string|number} internalId internal_id des Servers (siehe account.services())
|
|
117
|
+
*/
|
|
118
|
+
kvm(internalId) {
|
|
119
|
+
return new KVM(this, internalId);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Liefert einen Client für eine bestimmte Domain
|
|
124
|
+
* @param {string|number} internalId internal_id der Domain (siehe account.services())
|
|
125
|
+
*/
|
|
126
|
+
domain(internalId) {
|
|
127
|
+
return new Domain(this, internalId);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Liefert einen Client für einen bestimmten Webspace
|
|
132
|
+
* @param {string|number} internalId internal_id des Webspace (siehe account.services())
|
|
133
|
+
*/
|
|
134
|
+
webspace(internalId) {
|
|
135
|
+
return new Webspace(this, internalId);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Alle Endpunkte eines einzelnen KVM-Servers */
|
|
140
|
+
class KVM {
|
|
141
|
+
constructor(client, internalId) {
|
|
142
|
+
if (!internalId && internalId !== 0) {
|
|
143
|
+
throw new FireAPIError('kvm() benötigt eine internal_id');
|
|
144
|
+
}
|
|
145
|
+
this._client = client;
|
|
146
|
+
this._base = `/kvm/${encodeURIComponent(internalId)}`;
|
|
147
|
+
|
|
148
|
+
this.backup = {
|
|
149
|
+
/** Alle Backups auflisten */
|
|
150
|
+
list: () => this._req('GET', '/backup/list'),
|
|
151
|
+
/** Neues Backup erstellen (24fire+). @param {string} [description] max. 24 Zeichen */
|
|
152
|
+
create: (description) => this._req('POST', '/backup/create', { description }),
|
|
153
|
+
/** Status der Backup-Erstellung abfragen */
|
|
154
|
+
createStatus: (backupId) => this._req('POST', '/backup/create/status', { backup_id: backupId }),
|
|
155
|
+
/** Backup wiederherstellen (24fire+) */
|
|
156
|
+
restore: (backupId) => this._req('POST', '/backup/restore', { backup_id: backupId }),
|
|
157
|
+
/** Status der Wiederherstellung abfragen */
|
|
158
|
+
restoreStatus: (backupId) => this._req('POST', '/backup/restore/status', { backup_id: backupId }),
|
|
159
|
+
/** Backup löschen */
|
|
160
|
+
delete: (backupId) => this._req('DELETE', '/backup/delete', { backup_id: backupId }),
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
this.traffic = {
|
|
164
|
+
/** Aktueller Traffic-Verbrauch des Monats */
|
|
165
|
+
current: () => this._req('GET', '/traffic/current'),
|
|
166
|
+
/** Traffic-Log (Messung alle 10 Minuten) */
|
|
167
|
+
log: () => this._req('GET', '/traffic/log'),
|
|
168
|
+
/**
|
|
169
|
+
* Traffic-Diagramm generieren (24fire+)
|
|
170
|
+
* @param {object} opts z.B. { type, summary, output, datapoints, size, ... }
|
|
171
|
+
*/
|
|
172
|
+
chart: (opts = {}) => this._req('POST', '/traffic/chart', opts),
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
this.monitoring = {
|
|
176
|
+
/** Monitoring-Messungen (Timings) abrufen */
|
|
177
|
+
timings: () => this._req('GET', '/monitoring/timings'),
|
|
178
|
+
/** Ausfälle / Incidences abrufen */
|
|
179
|
+
incidences: () => this._req('GET', '/monitoring/incidences'),
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
this.ddos = {
|
|
183
|
+
/** Aktuelle DDoS-Einstellungen abrufen */
|
|
184
|
+
get: () => this._req('GET', '/ddos'),
|
|
185
|
+
/**
|
|
186
|
+
* DDoS-Einstellungen ändern (24fire+)
|
|
187
|
+
* @param {object} opts { layer4, layer7, ip_address? }
|
|
188
|
+
*/
|
|
189
|
+
change: (opts = {}) => this._req('POST', '/ddos/change', opts),
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
_req(method, path, body) {
|
|
194
|
+
return this._client.request(method, this._base + path, body);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** Server-Konfiguration abrufen */
|
|
198
|
+
config() {
|
|
199
|
+
return this._req('GET', '/config');
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** Aktuellen Status und Auslastung abrufen */
|
|
203
|
+
status() {
|
|
204
|
+
return this._req('GET', '/status');
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Server starten / stoppen / neu starten.
|
|
209
|
+
* @param {string} mode z.B. "start", "stop", "restart"
|
|
210
|
+
*/
|
|
211
|
+
power(mode) {
|
|
212
|
+
return this._req('POST', '/power', { mode });
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** Kurzform für power('start') */
|
|
216
|
+
start() {
|
|
217
|
+
return this.power('start');
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** Kurzform für power('stop') */
|
|
221
|
+
stop() {
|
|
222
|
+
return this.power('stop');
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** Kurzform für power('restart') */
|
|
226
|
+
restart() {
|
|
227
|
+
return this.power('restart');
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** Alle Endpunkte einer einzelnen Domain */
|
|
232
|
+
class Domain {
|
|
233
|
+
constructor(client, internalId) {
|
|
234
|
+
if (!internalId && internalId !== 0) {
|
|
235
|
+
throw new FireAPIError('domain() benötigt eine internal_id');
|
|
236
|
+
}
|
|
237
|
+
this._client = client;
|
|
238
|
+
this._base = `/domain/${encodeURIComponent(internalId)}`;
|
|
239
|
+
|
|
240
|
+
this.dns = {
|
|
241
|
+
/** Alle DNS-Einträge auflisten */
|
|
242
|
+
list: () => this._req('GET', '/dns'),
|
|
243
|
+
/**
|
|
244
|
+
* DNS-Eintrag hinzufügen (24fire+)
|
|
245
|
+
* @param {object} record { type, name, data }
|
|
246
|
+
*/
|
|
247
|
+
add: (record = {}) => this._req('PUT', '/dns/add', record),
|
|
248
|
+
/**
|
|
249
|
+
* DNS-Eintrag bearbeiten (24fire+)
|
|
250
|
+
* @param {object} record { record_id, type?, name?, data? }
|
|
251
|
+
*/
|
|
252
|
+
edit: (record = {}) => this._req('POST', '/dns/edit', record),
|
|
253
|
+
/**
|
|
254
|
+
* DNS-Eintrag entfernen
|
|
255
|
+
* @param {string} recordId ID des Eintrags (siehe dns.list())
|
|
256
|
+
*/
|
|
257
|
+
remove: (recordId) => this._req('DELETE', '/dns/remove', { record_id: recordId }),
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
_req(method, path, body) {
|
|
262
|
+
return this._client.request(method, this._base + path, body);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** Domain-Informationen abrufen */
|
|
266
|
+
info() {
|
|
267
|
+
return this._req('GET', '');
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/** Endpunkte eines einzelnen Webspace */
|
|
272
|
+
class Webspace {
|
|
273
|
+
constructor(client, internalId) {
|
|
274
|
+
if (!internalId && internalId !== 0) {
|
|
275
|
+
throw new FireAPIError('webspace() benötigt eine internal_id');
|
|
276
|
+
}
|
|
277
|
+
this._client = client;
|
|
278
|
+
this._base = `/webspace/${encodeURIComponent(internalId)}`;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** Webspace-Daten abrufen */
|
|
282
|
+
info() {
|
|
283
|
+
return this._client.request('GET', this._base);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
module.exports = FireAPI;
|
|
288
|
+
module.exports.FireAPI = FireAPI;
|
|
289
|
+
module.exports.FireAPIError = FireAPIError;
|
|
290
|
+
module.exports.default = FireAPI;
|