reddb-sdk 1.0.0 → 1.0.2

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 +20 -24
  2. package/package.json +9 -4
@@ -1,20 +1,21 @@
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
- // =========================
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
+ }
18
+
18
19
  async set(key, value, document = 'users.json') {
19
20
  if (!this.adminPassword) throw new Error('Admin password required for set');
20
21
 
@@ -29,7 +30,8 @@ class RedDBClient {
29
30
  adminPassword: this.adminPassword
30
31
  })
31
32
  });
32
- return res.json();
33
+
34
+ return this._safeJson(res);
33
35
  }
34
36
 
35
37
  async delete(key, document = 'users.json') {
@@ -45,27 +47,22 @@ class RedDBClient {
45
47
  adminPassword: this.adminPassword
46
48
  })
47
49
  });
48
- return res.json();
50
+
51
+ return this._safeJson(res);
49
52
  }
50
53
 
51
- // =========================
52
- // Operações de leitura (apenas REDDB_PASSWORD)
53
- // =========================
54
54
  async get(key, document = 'users.json') {
55
55
  const url = `${this.baseURL}/get/${encodeURIComponent(key)}?document=${encodeURIComponent(document)}&reddbPassword=${encodeURIComponent(this.reddbPassword)}`;
56
56
  const res = await fetch(url);
57
- return res.json();
57
+ return this._safeJson(res);
58
58
  }
59
59
 
60
60
  async listKeys(document = 'users.json') {
61
61
  const url = `${this.baseURL}/listKeys?document=${encodeURIComponent(document)}&reddbPassword=${encodeURIComponent(this.reddbPassword)}`;
62
62
  const res = await fetch(url);
63
- return res.json();
63
+ return this._safeJson(res);
64
64
  }
65
65
 
66
- // =========================
67
- // Endpoints admin
68
- // =========================
69
66
  async reset() {
70
67
  if (!this.adminPassword) throw new Error('Admin password required for reset');
71
68
 
@@ -77,17 +74,16 @@ class RedDBClient {
77
74
  adminPassword: this.adminPassword
78
75
  })
79
76
  });
80
- return res.json();
77
+
78
+ return this._safeJson(res);
81
79
  }
82
80
 
83
81
  async listAdminKeys() {
84
82
  if (!this.adminPassword) throw new Error('Admin password required for listAdminKeys');
85
83
 
86
- const res = await fetch(`${this.baseURL}/admin/keys`, {
87
- method: 'GET',
88
- headers: { 'Content-Type': 'application/json' }
89
- });
90
- return res.json();
84
+ const url = `${this.baseURL}/admin/keys?reddbPassword=${encodeURIComponent(this.reddbPassword)}&adminPassword=${encodeURIComponent(this.adminPassword)}`;
85
+ const res = await fetch(url);
86
+ return this._safeJson(res);
91
87
  }
92
88
  }
93
89
 
package/package.json CHANGED
@@ -1,13 +1,18 @@
1
1
  {
2
2
  "name": "reddb-sdk",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "SDK Node.js para RedDB Cluster",
5
5
  "main": "lib/RedDBClient.js",
6
- "keywords": ["reddb", "cluster", "database", "sdk"],
6
+ "keywords": [
7
+ "reddb",
8
+ "cluster",
9
+ "database",
10
+ "sdk"
11
+ ],
7
12
  "author": "MagicDB",
8
13
  "license": "MIT",
9
14
  "dependencies": {
10
- "node-fetch": "^3.3.2"
15
+ "node-fetch": "^2.6.7"
11
16
  },
12
17
  "type": "commonjs"
13
- }
18
+ }