reddb-sdk 1.0.2 → 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 +47 -48
  2. package/package.json +1 -1
@@ -16,74 +16,73 @@ class RedDBClient {
16
16
  }
17
17
  }
18
18
 
19
- async set(key, value, document = 'users.json') {
20
- if (!this.adminPassword) throw new Error('Admin password required for set');
19
+ async _request(path, options = {}, isAdmin = false) {
20
+ const headers = { 'Content-Type': 'application/json', ...(options.headers || {}) };
21
21
 
22
- const res = await fetch(`${this.baseURL}/set`, {
23
- method: 'POST',
24
- headers: { 'Content-Type': 'application/json' },
25
- body: JSON.stringify({
26
- key,
27
- value,
28
- document,
29
- reddbPassword: this.reddbPassword,
30
- adminPassword: this.adminPassword
31
- })
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
32
43
  });
33
44
 
34
45
  return this._safeJson(res);
35
46
  }
36
47
 
37
- async delete(key, document = 'users.json') {
38
- if (!this.adminPassword) throw new Error('Admin password required for delete');
39
-
40
- const res = await fetch(`${this.baseURL}/delete`, {
48
+ async set(key, value, document = 'users.json') {
49
+ return this._request('/set', {
41
50
  method: 'POST',
42
- headers: { 'Content-Type': 'application/json' },
43
- body: JSON.stringify({
44
- key,
45
- document,
46
- reddbPassword: this.reddbPassword,
47
- adminPassword: this.adminPassword
48
- })
49
- });
51
+ body: JSON.stringify({ key, value, document })
52
+ }, true);
53
+ }
50
54
 
51
- return this._safeJson(res);
55
+ async delete(key, document = 'users.json') {
56
+ return this._request('/delete', {
57
+ method: 'POST',
58
+ body: JSON.stringify({ key, document })
59
+ }, true);
52
60
  }
53
61
 
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 this._safeJson(res);
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 this._safeJson(res);
70
+ return this._request('/listKeys', {
71
+ method: 'GET',
72
+ body: JSON.stringify({ document })
73
+ });
64
74
  }
65
75
 
66
76
  async reset() {
67
- if (!this.adminPassword) throw new Error('Admin password required for reset');
68
-
69
- const res = await fetch(`${this.baseURL}/admin/reset`, {
70
- method: 'POST',
71
- headers: { 'Content-Type': 'application/json' },
72
- body: JSON.stringify({
73
- reddbPassword: this.reddbPassword,
74
- adminPassword: this.adminPassword
75
- })
76
- });
77
-
78
- return this._safeJson(res);
77
+ return this._request('/admin/reset', {
78
+ method: 'POST'
79
+ }, true);
79
80
  }
80
81
 
81
82
  async listAdminKeys() {
82
- if (!this.adminPassword) throw new Error('Admin password required for listAdminKeys');
83
-
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);
83
+ return this._request('/admin/keys', {
84
+ method: 'GET'
85
+ }, true);
87
86
  }
88
87
  }
89
88
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "reddb-sdk",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "SDK Node.js para RedDB Cluster",
5
5
  "main": "lib/RedDBClient.js",
6
6
  "keywords": [