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.
- package/lib/RedDBClient.js +47 -48
- package/package.json +1 -1
package/lib/RedDBClient.js
CHANGED
|
@@ -16,74 +16,73 @@ class RedDBClient {
|
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
async
|
|
20
|
-
|
|
19
|
+
async _request(path, options = {}, isAdmin = false) {
|
|
20
|
+
const headers = { 'Content-Type': 'application/json', ...(options.headers || {}) };
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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
|
|
38
|
-
|
|
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
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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
|
-
|
|
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
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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
|
-
|
|
62
|
-
|
|
63
|
-
|
|
70
|
+
return this._request('/listKeys', {
|
|
71
|
+
method: 'GET',
|
|
72
|
+
body: JSON.stringify({ document })
|
|
73
|
+
});
|
|
64
74
|
}
|
|
65
75
|
|
|
66
76
|
async reset() {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
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
|
-
|
|
83
|
-
|
|
84
|
-
|
|
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
|
|