reddb-sdk 1.0.3 → 1.0.4
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/lib/RedDBClient.js +57 -11
- package/package.json +1 -1
package/lib/RedDBClient.js
CHANGED
|
@@ -1,34 +1,62 @@
|
|
|
1
1
|
const fetch = require('node-fetch');
|
|
2
2
|
|
|
3
3
|
class RedDBClient {
|
|
4
|
-
constructor(baseURL, reddbPassword, adminPassword = null) {
|
|
4
|
+
constructor(baseURL, reddbPassword, adminPassword = null, options = {}) {
|
|
5
5
|
this.baseURL = baseURL.replace(/\/$/, '');
|
|
6
6
|
this.reddbPassword = reddbPassword;
|
|
7
7
|
this.adminPassword = adminPassword;
|
|
8
|
+
|
|
9
|
+
this.timeout = options.timeout || 5000;
|
|
10
|
+
this.retries = options.retries || 2;
|
|
11
|
+
this.debug = options.debug || false;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
_log(...args) {
|
|
15
|
+
if (this.debug) console.log('[RedDB]', ...args);
|
|
8
16
|
}
|
|
9
17
|
|
|
10
18
|
async _safeJson(res) {
|
|
11
19
|
const text = await res.text();
|
|
20
|
+
|
|
12
21
|
try {
|
|
13
22
|
return JSON.parse(text);
|
|
14
23
|
} catch {
|
|
15
|
-
throw new Error(`Resposta inválida
|
|
24
|
+
throw new Error(`Resposta inválida do servidor:\n${text}`);
|
|
16
25
|
}
|
|
17
26
|
}
|
|
18
27
|
|
|
19
|
-
async
|
|
20
|
-
const
|
|
28
|
+
async _fetchWithTimeout(url, options) {
|
|
29
|
+
const controller = new AbortController();
|
|
30
|
+
const id = setTimeout(() => controller.abort(), this.timeout);
|
|
31
|
+
|
|
32
|
+
try {
|
|
33
|
+
const res = await fetch(url, { ...options, signal: controller.signal });
|
|
34
|
+
clearTimeout(id);
|
|
35
|
+
return res;
|
|
36
|
+
} catch (err) {
|
|
37
|
+
clearTimeout(id);
|
|
38
|
+
|
|
39
|
+
if (err.name === 'AbortError') {
|
|
40
|
+
throw new Error('Timeout: cluster não respondeu a tempo');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
throw new Error('Cluster offline ou inacessível');
|
|
44
|
+
}
|
|
45
|
+
}
|
|
21
46
|
|
|
47
|
+
async _request(path, options = {}, isAdmin = false) {
|
|
22
48
|
let url = `${this.baseURL}${path}`;
|
|
23
49
|
let body = options.body ? JSON.parse(options.body) : {};
|
|
24
50
|
|
|
25
51
|
body.reddbPassword = this.reddbPassword;
|
|
26
52
|
|
|
27
53
|
if (isAdmin) {
|
|
28
|
-
if (!this.adminPassword) throw new Error('Admin password
|
|
54
|
+
if (!this.adminPassword) throw new Error('Admin password não fornecida');
|
|
29
55
|
body.adminPassword = this.adminPassword;
|
|
30
56
|
}
|
|
31
57
|
|
|
58
|
+
const headers = { 'Content-Type': 'application/json' };
|
|
59
|
+
|
|
32
60
|
if (options.method === 'GET') {
|
|
33
61
|
const query = new URLSearchParams(body).toString();
|
|
34
62
|
url += `?${query}`;
|
|
@@ -37,12 +65,30 @@ class RedDBClient {
|
|
|
37
65
|
options.body = JSON.stringify(body);
|
|
38
66
|
}
|
|
39
67
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
68
|
+
for (let attempt = 0; attempt <= this.retries; attempt++) {
|
|
69
|
+
try {
|
|
70
|
+
this._log('Request:', options.method, url);
|
|
71
|
+
|
|
72
|
+
const res = await this._fetchWithTimeout(url, {
|
|
73
|
+
...options,
|
|
74
|
+
headers
|
|
75
|
+
});
|
|
44
76
|
|
|
45
|
-
|
|
77
|
+
const data = await this._safeJson(res);
|
|
78
|
+
|
|
79
|
+
if (!res.ok) {
|
|
80
|
+
throw new Error(data.error || `Erro HTTP ${res.status}`);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return data;
|
|
84
|
+
} catch (err) {
|
|
85
|
+
this._log(`Erro (tentativa ${attempt + 1}):`, err.message);
|
|
86
|
+
|
|
87
|
+
if (attempt === this.retries) {
|
|
88
|
+
throw new Error(`Falha após ${this.retries + 1} tentativas: ${err.message}`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
46
92
|
}
|
|
47
93
|
|
|
48
94
|
async set(key, value, document = 'users.json') {
|
|
@@ -81,7 +127,7 @@ class RedDBClient {
|
|
|
81
127
|
|
|
82
128
|
async listAdminKeys() {
|
|
83
129
|
return this._request('/admin/keys', {
|
|
84
|
-
method: '
|
|
130
|
+
method: 'POST'
|
|
85
131
|
}, true);
|
|
86
132
|
}
|
|
87
133
|
}
|