+52
-52
@@ -22,3 +22,2 @@ const fetch = require('node-fetch'); | ||
| const text = await res.text(); | ||
| try { | ||
@@ -41,7 +40,3 @@ return JSON.parse(text); | ||
| clearTimeout(id); | ||
| if (err.name === 'AbortError') { | ||
| throw new Error('Timeout: cluster did not respond in time'); | ||
| } | ||
| if (err.name === 'AbortError') throw new Error('Timeout: cluster did not respond in time'); | ||
| throw new Error('Cluster offline or unreachable'); | ||
@@ -55,11 +50,7 @@ } | ||
| // accept body as object or string | ||
| if (options.body) { | ||
| body = typeof options.body === 'string' | ||
| ? JSON.parse(options.body) | ||
| : options.body; | ||
| body = typeof options.body === 'string' ? JSON.parse(options.body) : options.body; | ||
| } | ||
| body.reddbPassword = this.reddbPassword; | ||
| if (isAdmin) { | ||
@@ -84,12 +75,6 @@ if (!this.adminPassword) throw new Error('Admin password not provided'); | ||
| const res = await this._fetchWithTimeout(url, { | ||
| ...options, | ||
| headers | ||
| }); | ||
| const res = await this._fetchWithTimeout(url, { ...options, headers }); | ||
| const data = await this._safeJson(res); | ||
| if (!res.ok || data.error) { | ||
| throw new Error(data.error || `HTTP error ${res.status}`); | ||
| } | ||
| if (!res.ok || data.error) throw new Error(data.error || `HTTP error ${res.status}`); | ||
@@ -99,6 +84,3 @@ return data; | ||
| this._log(`Error (attempt ${attempt + 1}):`, err.message); | ||
| if (attempt === this.retries) { | ||
| throw new Error(`Failed after ${this.retries + 1} attempts: ${err.message}`); | ||
| } | ||
| if (attempt === this.retries) throw new Error(`Failed after ${this.retries + 1} attempts: ${err.message}`); | ||
| } | ||
@@ -112,60 +94,78 @@ } | ||
| async set(key, value, document = 'users.json') { | ||
| return this._request('/set', { | ||
| method: 'POST', | ||
| body: { key, value, document } | ||
| }, true); | ||
| return this._request('/set', { method: 'POST', body: { key, value, document } }, true); | ||
| } | ||
| async delete(key, document = 'users.json') { | ||
| return this._request('/delete', { | ||
| method: 'POST', | ||
| body: { key, document } | ||
| }, true); | ||
| return this._request('/delete', { method: 'POST', body: { key, document } }, true); | ||
| } | ||
| async get(key, document = 'users.json') { | ||
| return this._request(`/get/${encodeURIComponent(key)}`, { | ||
| method: 'GET', | ||
| body: { document } | ||
| }); | ||
| return this._request(`/get/${encodeURIComponent(key)}`, { method: 'GET', body: { document } }); | ||
| } | ||
| async listKeys(document = 'users.json') { | ||
| return this._request('/listKeys', { | ||
| method: 'GET', | ||
| body: { document } | ||
| }); | ||
| return this._request('/listKeys', { method: 'GET', body: { document } }); | ||
| } | ||
| async exists(key, document = 'users.json') { | ||
| return this._request(`/exists/${encodeURIComponent(key)}`, { | ||
| method: 'GET', | ||
| body: { document } | ||
| }); | ||
| return this._request(`/exists/${encodeURIComponent(key)}`, { method: 'GET', body: { document } }); | ||
| } | ||
| async update(key, value, document = 'users.json') { | ||
| return this._request('/update', { | ||
| return this._request('/update', { method: 'POST', body: { key, value, document } }, true); | ||
| } | ||
| // ========================= | ||
| // COLLECTIONS | ||
| // ========================= | ||
| async addCollection(collectionName, document = 'users.json') { | ||
| return this._request('/addCollection', { method: 'POST', body: { collectionName, document } }, true); | ||
| } | ||
| async removeCollection(collectionName, document = 'users.json') { | ||
| return this._request('/removeCollection', { method: 'POST', body: { collectionName, document } }, true); | ||
| } | ||
| async listCollectionKeys(collection, document = 'users.json') { | ||
| return this._request('/listCollectionKeys', { method: 'GET', body: { collection, document } }); | ||
| } | ||
| async updateCollectionItem(document, collection, key, value) { | ||
| return this._request('/updateCollectionItem', { | ||
| method: 'POST', | ||
| body: { key, value, document } | ||
| body: { document, collection, key, value } | ||
| }, true); | ||
| } | ||
| async addCollection(collectionName, document = 'users.json') { | ||
| return this._request('/addCollection', { | ||
| async deleteCollectionItem(document, collection, key) { | ||
| return this._request('/deleteCollectionItem', { | ||
| method: 'POST', | ||
| body: { collectionName, document } | ||
| body: { document, collection, key } | ||
| }, true); | ||
| } | ||
| async listCollectionKeys(collection, document = 'users.json') { | ||
| return this._request('/listCollectionKeys', { | ||
| async getCollectionItem(document, collection, key) { | ||
| return this._request('/getCollectionItem', { | ||
| method: 'GET', | ||
| body: { collection, document } | ||
| body: { document, collection, key } | ||
| }); | ||
| } | ||
| async listCollectionItems(document, collection) { | ||
| return this._request('/listCollectionItems', { | ||
| method: 'GET', | ||
| body: { document, collection } | ||
| }); | ||
| } | ||
| // ========================= | ||
| // Admin | ||
| // DOCUMENT | ||
| // ========================= | ||
| async getDocument(document) { | ||
| return this._request('/getDocument', { method: 'GET', body: { document } }); | ||
| } | ||
| // ========================= | ||
| // ADMIN | ||
| // ========================= | ||
| async reset() { | ||
@@ -172,0 +172,0 @@ return this._request('/admin/reset', { method: 'POST' }, true); |
+1
-1
| { | ||
| "name": "reddb-sdk", | ||
| "version": "2.0.0", | ||
| "version": "3.0.0", | ||
| "description": "SDK Node.js para RedDB Cluster", | ||
@@ -5,0 +5,0 @@ "main": "lib/RedDBClient.js", |
7053
15.19%