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.
Files changed (2) hide show
  1. package/lib/RedDBClient.js +58 -63
  2. package/package.json +1 -1
@@ -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
- // Operações de escrita (set/delete) - exigem adminPassword
17
- // =========================
18
- async set(key, value, document = 'users.json') {
19
- if (!this.adminPassword) throw new Error('Admin password required for set');
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
- const res = await fetch(`${this.baseURL}/set`, {
22
- method: 'POST',
23
- headers: { 'Content-Type': 'application/json' },
24
- body: JSON.stringify({
25
- key,
26
- value,
27
- document,
28
- reddbPassword: this.reddbPassword,
29
- adminPassword: this.adminPassword
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
- return res.json();
44
+
45
+ return this._safeJson(res);
33
46
  }
34
47
 
35
- async delete(key, document = 'users.json') {
36
- if (!this.adminPassword) throw new Error('Admin password required for delete');
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
- const res = await fetch(`${this.baseURL}/delete`, {
55
+ async delete(key, document = 'users.json') {
56
+ return this._request('/delete', {
39
57
  method: 'POST',
40
- headers: { 'Content-Type': 'application/json' },
41
- body: JSON.stringify({
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
- const url = `${this.baseURL}/get/${encodeURIComponent(key)}?document=${encodeURIComponent(document)}&reddbPassword=${encodeURIComponent(this.reddbPassword)}`;
56
- const res = await fetch(url);
57
- return res.json();
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
- const url = `${this.baseURL}/listKeys?document=${encodeURIComponent(document)}&reddbPassword=${encodeURIComponent(this.reddbPassword)}`;
62
- const res = await fetch(url);
63
- return res.json();
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
- if (!this.adminPassword) throw new Error('Admin password required for reset');
71
-
72
- const res = await fetch(`${this.baseURL}/admin/reset`, {
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
- if (!this.adminPassword) throw new Error('Admin password required for listAdminKeys');
85
-
86
- const res = await fetch(`${this.baseURL}/admin/keys`, {
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
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "reddb-sdk",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "SDK Node.js para RedDB Cluster",
5
5
  "main": "lib/RedDBClient.js",
6
6
  "keywords": [