reddb-sdk 1.0.1 → 1.0.3
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 +58 -63
- package/package.json +1 -1
package/lib/RedDBClient.js
CHANGED
|
@@ -1,93 +1,88 @@
|
|
|
1
1
|
const fetch = require('node-fetch');
|
|
2
2
|
|
|
3
3
|
class RedDBClient {
|
|
4
|
-
/**
|
|
5
|
-
* @param {string} baseURL - URL do cluster (ex: http://localhost:3000)
|
|
6
|
-
* @param {string} reddbPassword - senha do RedDB
|
|
7
|
-
* @param {string|null} adminPassword - senha admin (opcional, necessária para escrita)
|
|
8
|
-
*/
|
|
9
4
|
constructor(baseURL, reddbPassword, adminPassword = null) {
|
|
10
5
|
this.baseURL = baseURL.replace(/\/$/, '');
|
|
11
6
|
this.reddbPassword = reddbPassword;
|
|
12
7
|
this.adminPassword = adminPassword;
|
|
13
8
|
}
|
|
14
9
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
10
|
+
async _safeJson(res) {
|
|
11
|
+
const text = await res.text();
|
|
12
|
+
try {
|
|
13
|
+
return JSON.parse(text);
|
|
14
|
+
} catch {
|
|
15
|
+
throw new Error(`Resposta inválida (não é JSON):\n${text}`);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
20
18
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
19
|
+
async _request(path, options = {}, isAdmin = false) {
|
|
20
|
+
const headers = { 'Content-Type': 'application/json', ...(options.headers || {}) };
|
|
21
|
+
|
|
22
|
+
let url = `${this.baseURL}${path}`;
|
|
23
|
+
let body = options.body ? JSON.parse(options.body) : {};
|
|
24
|
+
|
|
25
|
+
body.reddbPassword = this.reddbPassword;
|
|
26
|
+
|
|
27
|
+
if (isAdmin) {
|
|
28
|
+
if (!this.adminPassword) throw new Error('Admin password required');
|
|
29
|
+
body.adminPassword = this.adminPassword;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (options.method === 'GET') {
|
|
33
|
+
const query = new URLSearchParams(body).toString();
|
|
34
|
+
url += `?${query}`;
|
|
35
|
+
delete options.body;
|
|
36
|
+
} else {
|
|
37
|
+
options.body = JSON.stringify(body);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const res = await fetch(url, {
|
|
41
|
+
...options,
|
|
42
|
+
headers
|
|
31
43
|
});
|
|
32
|
-
|
|
44
|
+
|
|
45
|
+
return this._safeJson(res);
|
|
33
46
|
}
|
|
34
47
|
|
|
35
|
-
async
|
|
36
|
-
|
|
48
|
+
async set(key, value, document = 'users.json') {
|
|
49
|
+
return this._request('/set', {
|
|
50
|
+
method: 'POST',
|
|
51
|
+
body: JSON.stringify({ key, value, document })
|
|
52
|
+
}, true);
|
|
53
|
+
}
|
|
37
54
|
|
|
38
|
-
|
|
55
|
+
async delete(key, document = 'users.json') {
|
|
56
|
+
return this._request('/delete', {
|
|
39
57
|
method: 'POST',
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
key,
|
|
43
|
-
document,
|
|
44
|
-
reddbPassword: this.reddbPassword,
|
|
45
|
-
adminPassword: this.adminPassword
|
|
46
|
-
})
|
|
47
|
-
});
|
|
48
|
-
return res.json();
|
|
58
|
+
body: JSON.stringify({ key, document })
|
|
59
|
+
}, true);
|
|
49
60
|
}
|
|
50
61
|
|
|
51
|
-
// =========================
|
|
52
|
-
// Operações de leitura (apenas REDDB_PASSWORD)
|
|
53
|
-
// =========================
|
|
54
62
|
async get(key, document = 'users.json') {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
63
|
+
return this._request(`/get/${encodeURIComponent(key)}`, {
|
|
64
|
+
method: 'GET',
|
|
65
|
+
body: JSON.stringify({ document })
|
|
66
|
+
});
|
|
58
67
|
}
|
|
59
68
|
|
|
60
69
|
async listKeys(document = 'users.json') {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
70
|
+
return this._request('/listKeys', {
|
|
71
|
+
method: 'GET',
|
|
72
|
+
body: JSON.stringify({ document })
|
|
73
|
+
});
|
|
64
74
|
}
|
|
65
75
|
|
|
66
|
-
// =========================
|
|
67
|
-
// Endpoints admin
|
|
68
|
-
// =========================
|
|
69
76
|
async reset() {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
method: 'POST',
|
|
74
|
-
headers: { 'Content-Type': 'application/json' },
|
|
75
|
-
body: JSON.stringify({
|
|
76
|
-
reddbPassword: this.reddbPassword,
|
|
77
|
-
adminPassword: this.adminPassword
|
|
78
|
-
})
|
|
79
|
-
});
|
|
80
|
-
return res.json();
|
|
77
|
+
return this._request('/admin/reset', {
|
|
78
|
+
method: 'POST'
|
|
79
|
+
}, true);
|
|
81
80
|
}
|
|
82
81
|
|
|
83
82
|
async listAdminKeys() {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
method: 'GET',
|
|
88
|
-
headers: { 'Content-Type': 'application/json' }
|
|
89
|
-
});
|
|
90
|
-
return res.json();
|
|
83
|
+
return this._request('/admin/keys', {
|
|
84
|
+
method: 'GET'
|
|
85
|
+
}, true);
|
|
91
86
|
}
|
|
92
87
|
}
|
|
93
88
|
|