reddb-sdk 1.0.4 → 1.0.5
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 +17 -7
- package/package.json +1 -1
package/lib/RedDBClient.js
CHANGED
|
@@ -2,12 +2,14 @@ const fetch = require('node-fetch');
|
|
|
2
2
|
|
|
3
3
|
class RedDBClient {
|
|
4
4
|
constructor(baseURL, reddbPassword, adminPassword = null, options = {}) {
|
|
5
|
+
if (!baseURL) throw new Error('baseURL é obrigatório');
|
|
6
|
+
|
|
5
7
|
this.baseURL = baseURL.replace(/\/$/, '');
|
|
6
8
|
this.reddbPassword = reddbPassword;
|
|
7
9
|
this.adminPassword = adminPassword;
|
|
8
10
|
|
|
9
11
|
this.timeout = options.timeout || 5000;
|
|
10
|
-
this.retries = options.retries
|
|
12
|
+
this.retries = options.retries ?? 2;
|
|
11
13
|
this.debug = options.debug || false;
|
|
12
14
|
}
|
|
13
15
|
|
|
@@ -46,7 +48,14 @@ class RedDBClient {
|
|
|
46
48
|
|
|
47
49
|
async _request(path, options = {}, isAdmin = false) {
|
|
48
50
|
let url = `${this.baseURL}${path}`;
|
|
49
|
-
let body =
|
|
51
|
+
let body = {};
|
|
52
|
+
|
|
53
|
+
// aceita body como objeto OU string
|
|
54
|
+
if (options.body) {
|
|
55
|
+
body = typeof options.body === 'string'
|
|
56
|
+
? JSON.parse(options.body)
|
|
57
|
+
: options.body;
|
|
58
|
+
}
|
|
50
59
|
|
|
51
60
|
body.reddbPassword = this.reddbPassword;
|
|
52
61
|
|
|
@@ -76,7 +85,8 @@ class RedDBClient {
|
|
|
76
85
|
|
|
77
86
|
const data = await this._safeJson(res);
|
|
78
87
|
|
|
79
|
-
|
|
88
|
+
// 🔥 tratamento de erro da API
|
|
89
|
+
if (!res.ok || data.error) {
|
|
80
90
|
throw new Error(data.error || `Erro HTTP ${res.status}`);
|
|
81
91
|
}
|
|
82
92
|
|
|
@@ -94,28 +104,28 @@ class RedDBClient {
|
|
|
94
104
|
async set(key, value, document = 'users.json') {
|
|
95
105
|
return this._request('/set', {
|
|
96
106
|
method: 'POST',
|
|
97
|
-
body:
|
|
107
|
+
body: { key, value, document }
|
|
98
108
|
}, true);
|
|
99
109
|
}
|
|
100
110
|
|
|
101
111
|
async delete(key, document = 'users.json') {
|
|
102
112
|
return this._request('/delete', {
|
|
103
113
|
method: 'POST',
|
|
104
|
-
body:
|
|
114
|
+
body: { key, document }
|
|
105
115
|
}, true);
|
|
106
116
|
}
|
|
107
117
|
|
|
108
118
|
async get(key, document = 'users.json') {
|
|
109
119
|
return this._request(`/get/${encodeURIComponent(key)}`, {
|
|
110
120
|
method: 'GET',
|
|
111
|
-
body:
|
|
121
|
+
body: { document }
|
|
112
122
|
});
|
|
113
123
|
}
|
|
114
124
|
|
|
115
125
|
async listKeys(document = 'users.json') {
|
|
116
126
|
return this._request('/listKeys', {
|
|
117
127
|
method: 'GET',
|
|
118
|
-
body:
|
|
128
|
+
body: { document }
|
|
119
129
|
});
|
|
120
130
|
}
|
|
121
131
|
|