skapi-js 1.2.14-beta.3 → 1.2.14-beta.6
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/dist/bundle-report-commonjs.html +2 -2
- package/dist/bundle-report-esm.html +2 -2
- package/dist/bundle-report-node-esm.html +2 -2
- package/dist/bundle-report.html +2 -2
- package/dist/skapi.cjs +1 -1
- package/dist/skapi.cjs.map +1 -1
- package/dist/skapi.js +1 -1
- package/dist/skapi.js.map +1 -1
- package/dist/skapi.mjs +1 -1
- package/dist/skapi.mjs.map +1 -1
- package/dist/skapi.node.mjs +1 -1
- package/dist/skapi.node.mjs.map +1 -1
- package/js/package.json +6 -5
- package/js/src/Types.d.ts +40 -2
- package/js/src/main/skapi.d.ts +41 -29
- package/js/src/main/skapi.js +48 -60
- package/js/src/main/skapi.js.map +1 -1
- package/js/src/methods/admin.d.ts +2 -4
- package/js/src/methods/database.d.ts +8 -0
- package/js/src/methods/database.js +285 -8
- package/js/src/methods/database.js.map +1 -1
- package/js/src/methods/param_restrictions.d.ts +8 -4
- package/js/src/methods/param_restrictions.js +72 -6
- package/js/src/methods/param_restrictions.js.map +1 -1
- package/js/src/methods/request.js +1 -1
- package/js/src/methods/request.js.map +1 -1
- package/js/src/methods/subscription.d.ts +0 -6
- package/js/src/methods/subscription.js +0 -28
- package/js/src/methods/subscription.js.map +1 -1
- package/js/src/methods/vivian.d.ts +18 -3
- package/js/src/methods/vivian.js +20 -10
- package/js/src/methods/vivian.js.map +1 -1
- package/js/src/utils/network.js +16 -11
- package/js/src/utils/network.js.map +1 -1
- package/js/src/utils/utils.d.ts +3 -1
- package/js/src/utils/utils.js +15 -3
- package/js/src/utils/utils.js.map +1 -1
- package/package.json +6 -5
- package/js/Main.d.ts +0 -6
- package/js/Main.js +0 -6
- package/js/Main.js.map +0 -1
- package/js/Types.d.ts +0 -305
- package/js/Types.js +0 -2
- package/js/Types.js.map +0 -1
- package/js/main/error.d.ts +0 -9
- package/js/main/error.js +0 -46
- package/js/main/error.js.map +0 -1
- package/js/main/skapi.d.ts +0 -443
- package/js/main/skapi.js +0 -853
- package/js/main/skapi.js.map +0 -1
- package/js/methods/admin.d.ts +0 -61
- package/js/methods/admin.js +0 -325
- package/js/methods/admin.js.map +0 -1
- package/js/methods/database.d.ts +0 -65
- package/js/methods/database.js +0 -947
- package/js/methods/database.js.map +0 -1
- package/js/methods/notification.d.ts +0 -21
- package/js/methods/notification.js +0 -81
- package/js/methods/notification.js.map +0 -1
- package/js/methods/param_restrictions.d.ts +0 -21
- package/js/methods/param_restrictions.js +0 -144
- package/js/methods/param_restrictions.js.map +0 -1
- package/js/methods/realtime.d.ts +0 -35
- package/js/methods/realtime.js +0 -408
- package/js/methods/realtime.js.map +0 -1
- package/js/methods/request.d.ts +0 -43
- package/js/methods/request.js +0 -132
- package/js/methods/request.js.map +0 -1
- package/js/methods/subscription.d.ts +0 -53
- package/js/methods/subscription.js +0 -337
- package/js/methods/subscription.js.map +0 -1
- package/js/methods/user.d.ts +0 -176
- package/js/methods/user.js +0 -1120
- package/js/methods/user.js.map +0 -1
- package/js/methods/vivian.d.ts +0 -3
- package/js/methods/vivian.js +0 -36
- package/js/methods/vivian.js.map +0 -1
- package/js/methods/webrtc.d.ts +0 -26
- package/js/methods/webrtc.js +0 -608
- package/js/methods/webrtc.js.map +0 -1
- package/js/polyfills/global.d.ts +0 -3
- package/js/polyfills/global.js +0 -235
- package/js/polyfills/global.js.map +0 -1
- package/js/utils/network.d.ts +0 -24
- package/js/utils/network.js +0 -707
- package/js/utils/network.js.map +0 -1
- package/js/utils/utils.d.ts +0 -40
- package/js/utils/utils.js +0 -651
- package/js/utils/utils.js.map +0 -1
- package/js/utils/validator.d.ts +0 -24
- package/js/utils/validator.js +0 -293
- package/js/utils/validator.js.map +0 -1
package/js/methods/database.js
DELETED
|
@@ -1,947 +0,0 @@
|
|
|
1
|
-
import SkapiError from '../main/error';
|
|
2
|
-
import { extractFormData, fromBase62 } from '../utils/utils';
|
|
3
|
-
import validator from '../utils/validator';
|
|
4
|
-
import { request, uploadFiles } from '../utils/network';
|
|
5
|
-
import { checkAdmin } from './user';
|
|
6
|
-
import { authentication } from './user';
|
|
7
|
-
import { accessGroup, cannotBeEmptyString, getStruct, indexValue, recordIdOrUniqueId } from './param_restrictions';
|
|
8
|
-
export async function normalizeRecord(record, _called_from) {
|
|
9
|
-
if (record?.rec) {
|
|
10
|
-
if (_called_from !== 'called from postRecord') {
|
|
11
|
-
let recPost = window.sessionStorage.getItem(`${this.service}:post:${record.rec}`);
|
|
12
|
-
if (recPost) {
|
|
13
|
-
try {
|
|
14
|
-
record = JSON.parse(recPost);
|
|
15
|
-
}
|
|
16
|
-
catch (err) { }
|
|
17
|
-
window.sessionStorage.removeItem(`${this.service}:post:${record.rec}`);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
const output = {
|
|
22
|
-
user_id: '',
|
|
23
|
-
record_id: '',
|
|
24
|
-
updated: 0,
|
|
25
|
-
uploaded: 0,
|
|
26
|
-
readonly: false,
|
|
27
|
-
table: {
|
|
28
|
-
name: '',
|
|
29
|
-
access_group: 0,
|
|
30
|
-
subscription: {
|
|
31
|
-
is_subscription_record: false,
|
|
32
|
-
upload_to_feed: false,
|
|
33
|
-
notify_subscribers: false,
|
|
34
|
-
feed_referencing_records: false,
|
|
35
|
-
notify_referencing_records: false
|
|
36
|
-
}
|
|
37
|
-
},
|
|
38
|
-
referenced_count: 0,
|
|
39
|
-
source: {
|
|
40
|
-
referencing_limit: null,
|
|
41
|
-
prevent_multiple_referencing: false,
|
|
42
|
-
can_remove_referencing_records: false,
|
|
43
|
-
only_granted_can_reference: false,
|
|
44
|
-
},
|
|
45
|
-
ip: '',
|
|
46
|
-
bin: {}
|
|
47
|
-
};
|
|
48
|
-
let is_anonymous = false;
|
|
49
|
-
function access_group_set(v) {
|
|
50
|
-
let access_group = v == '**' ? 'private' : parseInt(v);
|
|
51
|
-
access_group = access_group == 0 ? 'public' : access_group == 1 ? 'authorized' : access_group == 99 ? 'admin' : access_group;
|
|
52
|
-
return access_group;
|
|
53
|
-
}
|
|
54
|
-
const keys = {
|
|
55
|
-
'ip': (r) => {
|
|
56
|
-
let split_ip = r.split('#');
|
|
57
|
-
let ip = split_ip[0];
|
|
58
|
-
if (split_ip.length > 1) {
|
|
59
|
-
output.unique_id = split_ip[1];
|
|
60
|
-
}
|
|
61
|
-
if (ip.slice(-1) === 'R') {
|
|
62
|
-
output.readonly = true;
|
|
63
|
-
ip = ip.slice(0, -1);
|
|
64
|
-
}
|
|
65
|
-
else {
|
|
66
|
-
output.readonly = false;
|
|
67
|
-
}
|
|
68
|
-
if (/^\d{1,3}-\d{1,3}-\d{1,3}-\d{1,3}$/.test(ip)) {
|
|
69
|
-
ip = ip.split('-').join('.');
|
|
70
|
-
is_anonymous = true;
|
|
71
|
-
}
|
|
72
|
-
output.ip = ip;
|
|
73
|
-
},
|
|
74
|
-
'rec': (r) => {
|
|
75
|
-
if (!r)
|
|
76
|
-
return;
|
|
77
|
-
output.record_id = r;
|
|
78
|
-
let base62timestamp = r.substring(0, r.length - 9);
|
|
79
|
-
let uploaded = fromBase62(base62timestamp);
|
|
80
|
-
output.uploaded = uploaded;
|
|
81
|
-
},
|
|
82
|
-
'usr': (r) => {
|
|
83
|
-
output.user_id = r;
|
|
84
|
-
},
|
|
85
|
-
'tbl': (r) => {
|
|
86
|
-
if (!r)
|
|
87
|
-
return;
|
|
88
|
-
if (!output.table.name) {
|
|
89
|
-
let rSplit = r.split('/');
|
|
90
|
-
output.table.name = rSplit[0];
|
|
91
|
-
output.table.access_group = access_group_set(rSplit[2]);
|
|
92
|
-
if (rSplit?.[3]) {
|
|
93
|
-
output.table.subscription.is_subscription_record = true;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
},
|
|
97
|
-
'usr_tbl': (r) => {
|
|
98
|
-
let rSplit = r.split('/');
|
|
99
|
-
if (!output.user_id) {
|
|
100
|
-
output.user_id = rSplit[0];
|
|
101
|
-
}
|
|
102
|
-
if (!output.table.name) {
|
|
103
|
-
output.table.name = rSplit[1];
|
|
104
|
-
output.table.access_group = access_group_set(rSplit[3]);
|
|
105
|
-
if (rSplit?.[4]) {
|
|
106
|
-
output.table.subscription.is_subscription_record = true;
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
},
|
|
110
|
-
'idx': (r) => {
|
|
111
|
-
if (!r)
|
|
112
|
-
return;
|
|
113
|
-
let rSplit = r.split('!');
|
|
114
|
-
let name = rSplit.splice(0, 1)[0];
|
|
115
|
-
let value = normalizeTypedString('!' + rSplit.join('!'));
|
|
116
|
-
output.index = {
|
|
117
|
-
name,
|
|
118
|
-
value
|
|
119
|
-
};
|
|
120
|
-
},
|
|
121
|
-
'ref': (r) => {
|
|
122
|
-
if (!r)
|
|
123
|
-
return;
|
|
124
|
-
output.reference = r.split('/')[0];
|
|
125
|
-
},
|
|
126
|
-
'tags': (r) => {
|
|
127
|
-
output.tags = r;
|
|
128
|
-
},
|
|
129
|
-
'upd': (r) => {
|
|
130
|
-
output.updated = r;
|
|
131
|
-
},
|
|
132
|
-
'acpt_mrf': (r) => {
|
|
133
|
-
output.source.prevent_multiple_referencing = !r;
|
|
134
|
-
},
|
|
135
|
-
'ref_limt': (r) => {
|
|
136
|
-
output.source.referencing_limit = r;
|
|
137
|
-
},
|
|
138
|
-
'rfd': (r) => {
|
|
139
|
-
output.referenced_count = r;
|
|
140
|
-
},
|
|
141
|
-
'bin': async (r) => {
|
|
142
|
-
let binObj = {};
|
|
143
|
-
let _ref = output?.reference || null;
|
|
144
|
-
if (Array.isArray(r)) {
|
|
145
|
-
for (let url of r) {
|
|
146
|
-
let path = url.split('/').slice(3).join('/');
|
|
147
|
-
let splitPath = path.split('/');
|
|
148
|
-
let filename = decodeURIComponent(splitPath.slice(-1)[0]);
|
|
149
|
-
let pathKey = decodeURIComponent(splitPath[10]);
|
|
150
|
-
let size = splitPath[9];
|
|
151
|
-
let uploaded = splitPath[8];
|
|
152
|
-
let access_group = access_group_set(splitPath[6]);
|
|
153
|
-
let url_endpoint = url;
|
|
154
|
-
if (access_group !== 'public') {
|
|
155
|
-
url_endpoint = await getFile.bind(this)(url, { dataType: 'endpoint', _ref });
|
|
156
|
-
}
|
|
157
|
-
let obj = {
|
|
158
|
-
access_group,
|
|
159
|
-
filename,
|
|
160
|
-
url: url_endpoint,
|
|
161
|
-
path,
|
|
162
|
-
size: fromBase62(size),
|
|
163
|
-
uploaded: fromBase62(uploaded),
|
|
164
|
-
getFile: (dataType, progress) => {
|
|
165
|
-
let config = {
|
|
166
|
-
dataType: dataType || 'download',
|
|
167
|
-
progress,
|
|
168
|
-
_ref,
|
|
169
|
-
_update: obj
|
|
170
|
-
};
|
|
171
|
-
return getFile.bind(this)(url_endpoint, config);
|
|
172
|
-
}
|
|
173
|
-
};
|
|
174
|
-
if (binObj[pathKey]) {
|
|
175
|
-
binObj[pathKey].push(obj);
|
|
176
|
-
continue;
|
|
177
|
-
}
|
|
178
|
-
binObj[pathKey] = [obj];
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
output.bin = binObj;
|
|
182
|
-
},
|
|
183
|
-
'prv_acs': (r) => {
|
|
184
|
-
for (let k in r) {
|
|
185
|
-
let subscription_config = ['notify_subscribers', 'upload_to_feed', 'feed_referencing_records', 'notify_referencing_records'];
|
|
186
|
-
if (subscription_config.includes(k)) {
|
|
187
|
-
output.table.subscription[k] = r[k];
|
|
188
|
-
}
|
|
189
|
-
else {
|
|
190
|
-
output.source[k] = r[k];
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
},
|
|
194
|
-
'data': (r) => {
|
|
195
|
-
let data = r;
|
|
196
|
-
if (r === '!D%{}') {
|
|
197
|
-
data = {};
|
|
198
|
-
}
|
|
199
|
-
else if (r === '!L%[]') {
|
|
200
|
-
data = [];
|
|
201
|
-
}
|
|
202
|
-
output.data = data;
|
|
203
|
-
}
|
|
204
|
-
};
|
|
205
|
-
if (record.record_id) {
|
|
206
|
-
return record;
|
|
207
|
-
}
|
|
208
|
-
for (let k in keys) {
|
|
209
|
-
if (record.hasOwnProperty(k)) {
|
|
210
|
-
let exec = keys[k](record[k]);
|
|
211
|
-
if (exec instanceof Promise) {
|
|
212
|
-
await exec;
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
if (is_anonymous) {
|
|
217
|
-
output.user_id = 'anonymous:' + output.user_id;
|
|
218
|
-
}
|
|
219
|
-
return output;
|
|
220
|
-
}
|
|
221
|
-
function normalizeTypedString(v) {
|
|
222
|
-
let value = v.substring(3);
|
|
223
|
-
let type = v.substring(0, 3);
|
|
224
|
-
switch (type) {
|
|
225
|
-
case "!S%":
|
|
226
|
-
return value;
|
|
227
|
-
case "!N%":
|
|
228
|
-
let splitDec = value.split('.');
|
|
229
|
-
let calcNumb = Number(splitDec[0]) - 4503599627370496;
|
|
230
|
-
if (splitDec.length === 1) {
|
|
231
|
-
return calcNumb;
|
|
232
|
-
}
|
|
233
|
-
return parseFloat(calcNumb.toString() + '.' + splitDec[1]);
|
|
234
|
-
case "!B%":
|
|
235
|
-
return value === '1';
|
|
236
|
-
case "!L%":
|
|
237
|
-
case "!D%":
|
|
238
|
-
try {
|
|
239
|
-
return JSON.parse(value);
|
|
240
|
-
}
|
|
241
|
-
catch (err) {
|
|
242
|
-
throw new SkapiError('Value parse error.', { code: 'PARSE_ERROR' });
|
|
243
|
-
}
|
|
244
|
-
default:
|
|
245
|
-
return v;
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
export async function deleteFiles(params) {
|
|
249
|
-
let { endpoints } = params;
|
|
250
|
-
if (typeof endpoints === 'string') {
|
|
251
|
-
endpoints = [endpoints];
|
|
252
|
-
}
|
|
253
|
-
if (!Array.isArray(endpoints)) {
|
|
254
|
-
throw new SkapiError('"endpoints" should be type: array | string.', { code: 'INVALID_PARAMETER' });
|
|
255
|
-
}
|
|
256
|
-
let updatedRec = await request.bind(this)('del-files', {
|
|
257
|
-
endpoints,
|
|
258
|
-
storage: 'records'
|
|
259
|
-
}, { auth: true, method: 'post' });
|
|
260
|
-
let to_process = [];
|
|
261
|
-
for (let i in updatedRec) {
|
|
262
|
-
to_process.push(normalizeRecord.bind(this)(updatedRec[i]));
|
|
263
|
-
}
|
|
264
|
-
return Promise.all(to_process);
|
|
265
|
-
}
|
|
266
|
-
export async function getFile(url, config) {
|
|
267
|
-
if (typeof url !== 'string') {
|
|
268
|
-
throw new SkapiError('"url" should be type: string.', { code: 'INVALID_PARAMETER' });
|
|
269
|
-
}
|
|
270
|
-
url = validator.Url(url.split('?')[0]);
|
|
271
|
-
let isValidEndpoint = false;
|
|
272
|
-
let splitUrl = url.split('/');
|
|
273
|
-
let host = splitUrl[2];
|
|
274
|
-
let splitHost = host.split('.');
|
|
275
|
-
let subdomain = null;
|
|
276
|
-
if (splitHost.length === 3 && splitHost[1] === 'skapi') {
|
|
277
|
-
subdomain = splitHost[0];
|
|
278
|
-
isValidEndpoint = true;
|
|
279
|
-
}
|
|
280
|
-
let target_key = splitUrl.slice(3);
|
|
281
|
-
let needAuth = false;
|
|
282
|
-
if (!isValidEndpoint) {
|
|
283
|
-
if (target_key[0] === 'auth' || target_key[0] === 'publ') {
|
|
284
|
-
try {
|
|
285
|
-
validator.UserId(target_key[2]);
|
|
286
|
-
validator.UserId(target_key[3]);
|
|
287
|
-
needAuth = target_key[0] == 'auth';
|
|
288
|
-
isValidEndpoint = true;
|
|
289
|
-
}
|
|
290
|
-
catch {
|
|
291
|
-
throw new SkapiError('Invalid file url.', { code: 'INVALID_PARAMETER' });
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
let service = subdomain ? null : target_key[1];
|
|
296
|
-
config = validator.Params(config, {
|
|
297
|
-
expires: ['number', () => 0],
|
|
298
|
-
dataType: ['base64', 'blob', 'endpoint', 'text', 'info', () => 'download'],
|
|
299
|
-
progress: 'function',
|
|
300
|
-
_ref: 'string',
|
|
301
|
-
_update: v => v
|
|
302
|
-
});
|
|
303
|
-
if (config?.dataType === 'info') {
|
|
304
|
-
return {
|
|
305
|
-
url,
|
|
306
|
-
filename: target_key[target_key.length - 1],
|
|
307
|
-
fileKey: target_key[target_key.length - 2],
|
|
308
|
-
access_group: target_key[6] === '**' ? 'private' : target_key[6] === '01' ? 'authorized' : target_key[6] === '00' ? 'public' : parseInt(target_key[6]),
|
|
309
|
-
uploader: target_key[3],
|
|
310
|
-
record_id: target_key[4] === 'records' ? target_key[5] : 'N/A',
|
|
311
|
-
filesize: fromBase62(target_key[9]),
|
|
312
|
-
uploaded: fromBase62(target_key[8]),
|
|
313
|
-
};
|
|
314
|
-
}
|
|
315
|
-
let filename = url.split('/').slice(-1)[0];
|
|
316
|
-
let expires = config.expires;
|
|
317
|
-
if (expires) {
|
|
318
|
-
if (!isValidEndpoint) {
|
|
319
|
-
throw new SkapiError('Expires option can only be used on skapi cdn endpoints.', { code: 'INVALID_PARAMETER' });
|
|
320
|
-
}
|
|
321
|
-
if (expires < 0) {
|
|
322
|
-
throw new SkapiError('"config.expires" should be > 0. (seconds)', { code: 'INVALID_PARAMETER' });
|
|
323
|
-
}
|
|
324
|
-
let params = {
|
|
325
|
-
request: subdomain ? 'get-host' : 'get',
|
|
326
|
-
id: subdomain || target_key[5],
|
|
327
|
-
key: url,
|
|
328
|
-
expires
|
|
329
|
-
};
|
|
330
|
-
if (service) {
|
|
331
|
-
params.service = service;
|
|
332
|
-
}
|
|
333
|
-
url = (await request.bind(this)('get-signed-url', params, { auth: true })).url;
|
|
334
|
-
}
|
|
335
|
-
else if (needAuth) {
|
|
336
|
-
let currTime = Math.floor(Date.now() / 1000);
|
|
337
|
-
if (!this.bearerToken && this.session?.idToken?.payload?.exp < currTime) {
|
|
338
|
-
this.log('getFile:requesting new token', null);
|
|
339
|
-
try {
|
|
340
|
-
await authentication.bind(this)().getSession({ refreshToken: true });
|
|
341
|
-
this.log('getFile:received new tokens', {
|
|
342
|
-
exp: this.session?.idToken?.payload?.exp,
|
|
343
|
-
currTime,
|
|
344
|
-
expiresIn: this.session?.idToken?.payload?.exp - currTime,
|
|
345
|
-
token: this.session?.accessToken?.jwtToken,
|
|
346
|
-
refreshToken: this.session?.refreshToken?.token
|
|
347
|
-
});
|
|
348
|
-
}
|
|
349
|
-
catch (err) {
|
|
350
|
-
this.log('getFile:new token error', err);
|
|
351
|
-
throw new SkapiError('User login is required.', { code: 'INVALID_REQUEST' });
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
let token = this.bearerToken || this.session?.idToken?.jwtToken;
|
|
355
|
-
url += `?t=${token}`;
|
|
356
|
-
let access_group = target_key[6] === '**' ? '**' : parseInt(target_key[6]);
|
|
357
|
-
if (this.user.user_id !== target_key[3] && (access_group === '**' || this.user?.access_group < access_group)) {
|
|
358
|
-
let record_id = target_key[5];
|
|
359
|
-
if (this.__private_access_key[record_id] && typeof this.__private_access_key[record_id] === 'string') {
|
|
360
|
-
url += '&p=' + this.__private_access_key[record_id];
|
|
361
|
-
}
|
|
362
|
-
else if (this.owner !== this.host) {
|
|
363
|
-
try {
|
|
364
|
-
let p = await this.requestPrivateRecordAccessKey({ record_id, reference_id: config?._ref });
|
|
365
|
-
url += '&p=' + p;
|
|
366
|
-
}
|
|
367
|
-
catch (err) { }
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
if (config?.dataType === 'endpoint') {
|
|
372
|
-
if (config._update) {
|
|
373
|
-
config._update.url = url;
|
|
374
|
-
}
|
|
375
|
-
return url;
|
|
376
|
-
}
|
|
377
|
-
if (config?.dataType === 'download') {
|
|
378
|
-
let a = document.createElement('a');
|
|
379
|
-
a.href = url;
|
|
380
|
-
document.body.appendChild(a);
|
|
381
|
-
a.setAttribute('download', filename);
|
|
382
|
-
a.target = '_blank';
|
|
383
|
-
a.click();
|
|
384
|
-
document.body.removeChild(a);
|
|
385
|
-
return null;
|
|
386
|
-
}
|
|
387
|
-
let blob = new Promise(async (res, rej) => {
|
|
388
|
-
try {
|
|
389
|
-
let b = await request.bind(this)(url, null, { method: 'get', contentType: null, responseType: config?.dataType === 'text' ? 'text' : 'blob', fetchOptions: { progress: config?.progress } }, { ignoreService: true });
|
|
390
|
-
if (config?.dataType === 'base64') {
|
|
391
|
-
const reader = new FileReader();
|
|
392
|
-
reader.onloadend = () => res(reader.result);
|
|
393
|
-
reader.readAsDataURL(b);
|
|
394
|
-
}
|
|
395
|
-
else {
|
|
396
|
-
res(b);
|
|
397
|
-
}
|
|
398
|
-
}
|
|
399
|
-
catch (err) {
|
|
400
|
-
rej(err);
|
|
401
|
-
}
|
|
402
|
-
});
|
|
403
|
-
return blob;
|
|
404
|
-
}
|
|
405
|
-
async function prepGetParams(query, isDel = false) {
|
|
406
|
-
query = extractFormData(query, { ignoreEmpty: true }).data;
|
|
407
|
-
if (typeof query?.table === 'string') {
|
|
408
|
-
query.table = {
|
|
409
|
-
name: query.table,
|
|
410
|
-
access_group: 0
|
|
411
|
-
};
|
|
412
|
-
}
|
|
413
|
-
let is_reference_fetch = '';
|
|
414
|
-
let rec_or_uniq = recordIdOrUniqueId(query);
|
|
415
|
-
if (rec_or_uniq) {
|
|
416
|
-
query = rec_or_uniq;
|
|
417
|
-
is_reference_fetch = query.record_id || query.unique_id;
|
|
418
|
-
if (typeof is_reference_fetch === 'string') {
|
|
419
|
-
if (typeof this.__private_access_key?.[is_reference_fetch] === 'string') {
|
|
420
|
-
query.private_key = this.__private_access_key?.[is_reference_fetch] || undefined;
|
|
421
|
-
}
|
|
422
|
-
if (this.__my_unique_ids[is_reference_fetch]) {
|
|
423
|
-
if (isDel) {
|
|
424
|
-
delete this.__my_unique_ids[is_reference_fetch];
|
|
425
|
-
}
|
|
426
|
-
else {
|
|
427
|
-
query.record_id = this.__my_unique_ids[is_reference_fetch];
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
else {
|
|
433
|
-
let isAdmin = await checkAdmin.bind(this)();
|
|
434
|
-
let ref = query.reference;
|
|
435
|
-
let ref_user = '';
|
|
436
|
-
if (ref?.record_id || ref?.unique_id) {
|
|
437
|
-
is_reference_fetch = ref.record_id || ref.unique_id;
|
|
438
|
-
if (is_reference_fetch && typeof this.__private_access_key?.[is_reference_fetch] === 'string') {
|
|
439
|
-
query.private_key = this.__private_access_key?.[is_reference_fetch] || undefined;
|
|
440
|
-
}
|
|
441
|
-
query.reference = is_reference_fetch;
|
|
442
|
-
}
|
|
443
|
-
else if (ref?.user_id) {
|
|
444
|
-
ref_user = ref.user_id;
|
|
445
|
-
query.reference = ref_user;
|
|
446
|
-
}
|
|
447
|
-
query = validator.Params(query || {}, getStruct.bind(this)(query), ref_user || isAdmin ? [] : ['table'], { ignoreEmpty: true });
|
|
448
|
-
}
|
|
449
|
-
return {
|
|
450
|
-
query,
|
|
451
|
-
is_reference_fetch
|
|
452
|
-
};
|
|
453
|
-
}
|
|
454
|
-
export async function getRecords(query, fetchOptions) {
|
|
455
|
-
await this.__connection;
|
|
456
|
-
let q = await prepGetParams.bind(this)(query);
|
|
457
|
-
let is_reference_fetch = q.is_reference_fetch;
|
|
458
|
-
let result = await request.bind(this)('get-records', q.query, {
|
|
459
|
-
fetchOptions,
|
|
460
|
-
auth: !!this.__user,
|
|
461
|
-
method: !!this.__user ? 'post' : 'get'
|
|
462
|
-
});
|
|
463
|
-
if (is_reference_fetch && result?.reference_private_key && typeof result.reference_private_key === 'string') {
|
|
464
|
-
this.__private_access_key[is_reference_fetch] = result.reference_private_key;
|
|
465
|
-
}
|
|
466
|
-
let to_process = [];
|
|
467
|
-
for (let i in result.list) {
|
|
468
|
-
to_process.push(normalizeRecord.bind(this)(result.list[i]));
|
|
469
|
-
}
|
|
470
|
-
result.list = await Promise.all(to_process);
|
|
471
|
-
return result;
|
|
472
|
-
}
|
|
473
|
-
export async function postRecord(form, config, files) {
|
|
474
|
-
await this.__connection;
|
|
475
|
-
let is_reference_post = "";
|
|
476
|
-
if (!config) {
|
|
477
|
-
throw new SkapiError('"config" argument is required.', { code: 'INVALID_PARAMETER' });
|
|
478
|
-
}
|
|
479
|
-
let is_public = !this.__user;
|
|
480
|
-
if (typeof config.table === 'string') {
|
|
481
|
-
config.table = {
|
|
482
|
-
name: config.table
|
|
483
|
-
};
|
|
484
|
-
}
|
|
485
|
-
if (!config.record_id && !config.table.hasOwnProperty('access_group')) {
|
|
486
|
-
config.table.access_group = 0;
|
|
487
|
-
}
|
|
488
|
-
let reference_limit_check = (v) => {
|
|
489
|
-
if (v === null) {
|
|
490
|
-
return null;
|
|
491
|
-
}
|
|
492
|
-
else if (typeof v === 'number') {
|
|
493
|
-
if (0 > v) {
|
|
494
|
-
throw new SkapiError(`"reference_limit" should be >= 0`, { code: 'INVALID_PARAMETER' });
|
|
495
|
-
}
|
|
496
|
-
if (v > 4503599627370546) {
|
|
497
|
-
throw new SkapiError(`"reference_limit" should be <= 4503599627370546`, { code: 'INVALID_PARAMETER' });
|
|
498
|
-
}
|
|
499
|
-
return v;
|
|
500
|
-
}
|
|
501
|
-
throw new SkapiError(`"reference_limit" should be type: <number | null>`, { code: 'INVALID_PARAMETER' });
|
|
502
|
-
};
|
|
503
|
-
if (config.table?.subscription) {
|
|
504
|
-
if (config.table?.subscription?.is_subscription_record) {
|
|
505
|
-
Object.assign(config.table.subscription, { group: 1 });
|
|
506
|
-
}
|
|
507
|
-
else if (config.table?.subscription?.is_subscription_record === false || !config.record_id && !config.table.subscription?.is_subscription_record) {
|
|
508
|
-
Object.assign(config.table.subscription, { group: null });
|
|
509
|
-
}
|
|
510
|
-
delete config.table.subscription?.is_subscription_record;
|
|
511
|
-
}
|
|
512
|
-
let _config = validator.Params(config || {}, {
|
|
513
|
-
record_id: ['string', () => {
|
|
514
|
-
if (!config.table || !config.table.name) {
|
|
515
|
-
throw new SkapiError('"table.name" is required.', { code: 'INVALID_PARAMETER' });
|
|
516
|
-
}
|
|
517
|
-
}],
|
|
518
|
-
unique_id: 'string',
|
|
519
|
-
readonly: 'boolean',
|
|
520
|
-
table: {
|
|
521
|
-
name: v => cannotBeEmptyString(v, 'table name', true, true),
|
|
522
|
-
subscription: {
|
|
523
|
-
group: v => {
|
|
524
|
-
if (v === 1) {
|
|
525
|
-
return 1;
|
|
526
|
-
}
|
|
527
|
-
return null;
|
|
528
|
-
},
|
|
529
|
-
upload_to_feed: 'boolean',
|
|
530
|
-
notify_subscribers: 'boolean',
|
|
531
|
-
feed_referencing_records: 'boolean',
|
|
532
|
-
notify_referencing_records: 'boolean',
|
|
533
|
-
},
|
|
534
|
-
access_group: accessGroup.bind(this),
|
|
535
|
-
},
|
|
536
|
-
source: {
|
|
537
|
-
referencing_limit: reference_limit_check,
|
|
538
|
-
prevent_multiple_referencing: 'boolean',
|
|
539
|
-
can_remove_referencing_records: 'boolean',
|
|
540
|
-
only_granted_can_reference: 'boolean',
|
|
541
|
-
allow_granted_to_grant_others: 'boolean',
|
|
542
|
-
referencing_index_restrictions: v => {
|
|
543
|
-
if (v === undefined) {
|
|
544
|
-
return undefined;
|
|
545
|
-
}
|
|
546
|
-
if (!v) {
|
|
547
|
-
return null;
|
|
548
|
-
}
|
|
549
|
-
if (Array.isArray(v) && !v.length) {
|
|
550
|
-
return null;
|
|
551
|
-
}
|
|
552
|
-
let p = {
|
|
553
|
-
name: [v => cannotBeEmptyString(v, '"name" in "index_restrictions"', true, false)],
|
|
554
|
-
value: v => indexValue(v),
|
|
555
|
-
condition: ['gt', 'gte', 'lt', 'lte', '>', '>=', '<', '<=', '=', 'eq', '!=', 'ne', () => null],
|
|
556
|
-
range: val => {
|
|
557
|
-
if (val !== null && typeof v.value !== typeof val) {
|
|
558
|
-
throw new SkapiError('Index restriction "range" type should match the type of "value".', { code: 'INVALID_PARAMETER' });
|
|
559
|
-
}
|
|
560
|
-
if (!v.hasOwnProperty('value')) {
|
|
561
|
-
throw new SkapiError('Index restriction "value" is required.', { code: 'INVALID_PARAMETER' });
|
|
562
|
-
}
|
|
563
|
-
if (v.condition && (v.condition !== 'eq' || v.condition !== '=')) {
|
|
564
|
-
throw new SkapiError('Index restriction "condition" cannot be used with "range".', { code: 'INVALID_PARAMETER' });
|
|
565
|
-
}
|
|
566
|
-
return val;
|
|
567
|
-
}
|
|
568
|
-
};
|
|
569
|
-
if (!Array.isArray(v)) {
|
|
570
|
-
v = [v];
|
|
571
|
-
}
|
|
572
|
-
return v.map(vv => validator.Params(vv, p));
|
|
573
|
-
},
|
|
574
|
-
},
|
|
575
|
-
reference: v => {
|
|
576
|
-
if (v === null) {
|
|
577
|
-
return { record_id: null };
|
|
578
|
-
}
|
|
579
|
-
if (!v) {
|
|
580
|
-
return undefined;
|
|
581
|
-
}
|
|
582
|
-
if (typeof v === 'string') {
|
|
583
|
-
is_reference_post = v;
|
|
584
|
-
if (this.__my_unique_ids[v]) {
|
|
585
|
-
return this.__my_unique_ids[v];
|
|
586
|
-
}
|
|
587
|
-
return v;
|
|
588
|
-
}
|
|
589
|
-
if (typeof v !== 'object') {
|
|
590
|
-
throw new SkapiError('"reference" should be type: <string | object>.', { code: 'INVALID_PARAMETER' });
|
|
591
|
-
}
|
|
592
|
-
return validator.Params(v, {
|
|
593
|
-
unique_id: 'string',
|
|
594
|
-
record_id: v => {
|
|
595
|
-
if (v === null || v === undefined) {
|
|
596
|
-
return v;
|
|
597
|
-
}
|
|
598
|
-
is_reference_post = v;
|
|
599
|
-
if (typeof this.__private_access_key?.[v] === 'string') {
|
|
600
|
-
config.reference_private_key = this.__private_access_key[v] || undefined;
|
|
601
|
-
}
|
|
602
|
-
return validator.specialChars(v, '"reference.record_id"', false, false);
|
|
603
|
-
}
|
|
604
|
-
});
|
|
605
|
-
},
|
|
606
|
-
index: {
|
|
607
|
-
name: v => cannotBeEmptyString(v, 'index.name', true, false),
|
|
608
|
-
value: v => indexValue(v)
|
|
609
|
-
},
|
|
610
|
-
tags: (v) => {
|
|
611
|
-
if (v === null || v === undefined) {
|
|
612
|
-
return v;
|
|
613
|
-
}
|
|
614
|
-
if (typeof v === 'string') {
|
|
615
|
-
v = v.split(',').map(t => t.trim());
|
|
616
|
-
}
|
|
617
|
-
return validator.specialChars(v, 'tag', false, true);
|
|
618
|
-
},
|
|
619
|
-
remove_bin: (v) => {
|
|
620
|
-
if (!v) {
|
|
621
|
-
return null;
|
|
622
|
-
}
|
|
623
|
-
let arr = [];
|
|
624
|
-
if (Array.isArray(v)) {
|
|
625
|
-
for (let i of v) {
|
|
626
|
-
if (typeof i === 'string') {
|
|
627
|
-
arr.push(decodeURIComponent(i.split('?')[0]));
|
|
628
|
-
}
|
|
629
|
-
else if (i.url && i.size && i.filename) {
|
|
630
|
-
let hostUrl = i.url.split('/').slice(0, 3).join('/');
|
|
631
|
-
let url = hostUrl + '/' + i.path;
|
|
632
|
-
arr.push(url);
|
|
633
|
-
}
|
|
634
|
-
else {
|
|
635
|
-
throw new SkapiError(`"remove_bin" should be type: <string[] | BinaryFile[] | null>`, { code: 'INVALID_PARAMETER' });
|
|
636
|
-
}
|
|
637
|
-
}
|
|
638
|
-
}
|
|
639
|
-
else {
|
|
640
|
-
throw new SkapiError(`"remove_bin" should be type: <string[] | BinaryFile[] | null>`, { code: 'INVALID_PARAMETER' });
|
|
641
|
-
}
|
|
642
|
-
return arr;
|
|
643
|
-
},
|
|
644
|
-
progress: 'function',
|
|
645
|
-
});
|
|
646
|
-
let progress = config.progress || null;
|
|
647
|
-
delete _config.progress;
|
|
648
|
-
let options = { auth: !!this.__user, method: 'post' };
|
|
649
|
-
let postData = null;
|
|
650
|
-
let to_bin = null;
|
|
651
|
-
let extractedForm = extractFormData(form);
|
|
652
|
-
if (files) {
|
|
653
|
-
to_bin = files;
|
|
654
|
-
}
|
|
655
|
-
else if (extractedForm.files.length) {
|
|
656
|
-
to_bin = extractedForm.files;
|
|
657
|
-
}
|
|
658
|
-
if (is_public) {
|
|
659
|
-
if (_config.record_id) {
|
|
660
|
-
throw new SkapiError('Public users cannot update existing records.', { code: 'INVALID_REQUEST' });
|
|
661
|
-
}
|
|
662
|
-
if (_config.table.access_group !== 'public' && _config.table.access_group !== 0) {
|
|
663
|
-
throw new SkapiError('Public users can only post records to public tables.', { code: 'INVALID_REQUEST' });
|
|
664
|
-
}
|
|
665
|
-
if (_config.table.subscription) {
|
|
666
|
-
throw new SkapiError('Public users cannot post subscription records.', { code: 'INVALID_REQUEST' });
|
|
667
|
-
}
|
|
668
|
-
if (_config.remove_bin) {
|
|
669
|
-
throw new SkapiError('Public users cannot remove files from records.', { code: 'INVALID_REQUEST' });
|
|
670
|
-
}
|
|
671
|
-
if (_config.unique_id) {
|
|
672
|
-
throw new SkapiError('Public users cannot set unique_id for records.', { code: 'INVALID_REQUEST' });
|
|
673
|
-
}
|
|
674
|
-
}
|
|
675
|
-
postData = Object.assign({ data: extractedForm.data }, _config);
|
|
676
|
-
let fetchOptions = {};
|
|
677
|
-
if (typeof progress === 'function') {
|
|
678
|
-
fetchOptions.progress = progress;
|
|
679
|
-
}
|
|
680
|
-
if (Object.keys(fetchOptions).length) {
|
|
681
|
-
Object.assign(options, { fetchOptions });
|
|
682
|
-
}
|
|
683
|
-
let rec = await request.bind(this)('post-record', postData, options);
|
|
684
|
-
if (to_bin) {
|
|
685
|
-
let bin_formData = new FormData();
|
|
686
|
-
for (let f of to_bin) {
|
|
687
|
-
bin_formData.append(f.name, f.file, f.file.name);
|
|
688
|
-
}
|
|
689
|
-
let uploadFileParams = {
|
|
690
|
-
record_id: rec.rec,
|
|
691
|
-
progress
|
|
692
|
-
};
|
|
693
|
-
if (_config.hasOwnProperty('service')) {
|
|
694
|
-
uploadFileParams['service'] = _config.service;
|
|
695
|
-
}
|
|
696
|
-
let { bin_endpoints } = await uploadFiles.bind(this)(bin_formData, uploadFileParams);
|
|
697
|
-
if (!rec.bin) {
|
|
698
|
-
rec.bin = bin_endpoints;
|
|
699
|
-
}
|
|
700
|
-
else {
|
|
701
|
-
rec.bin.push(...bin_endpoints);
|
|
702
|
-
}
|
|
703
|
-
}
|
|
704
|
-
if (is_reference_post && typeof rec?.reference_private_key === 'string') {
|
|
705
|
-
this.__private_access_key[is_reference_post] = rec.reference_private_key;
|
|
706
|
-
}
|
|
707
|
-
window.sessionStorage.setItem(`${this.service}:post:${rec.rec}`, JSON.stringify(rec));
|
|
708
|
-
let record = await normalizeRecord.bind(this)(rec, 'called from postRecord');
|
|
709
|
-
if (record.unique_id) {
|
|
710
|
-
this.__my_unique_ids[record.unique_id] = record.record_id;
|
|
711
|
-
window.sessionStorage.setItem(`${this.service}:uniqueids`, JSON.stringify(this.__my_unique_ids));
|
|
712
|
-
}
|
|
713
|
-
return record;
|
|
714
|
-
}
|
|
715
|
-
export async function getTables(query, fetchOptions) {
|
|
716
|
-
let res = await request.bind(this)('get-table', validator.Params(query || {}, {
|
|
717
|
-
table: 'string',
|
|
718
|
-
condition: ['gt', 'gte', 'lt', 'lte', '>', '>=', '<', '<=', '=', 'eq', '!=', 'ne']
|
|
719
|
-
}), Object.assign({ auth: !!this.__user }, { fetchOptions }));
|
|
720
|
-
let convert = {
|
|
721
|
-
'cnt_rec': 'number_of_records',
|
|
722
|
-
'tbl': 'table',
|
|
723
|
-
'srvc': 'service'
|
|
724
|
-
};
|
|
725
|
-
if (Array.isArray(res?.list)) {
|
|
726
|
-
for (let t of res.list) {
|
|
727
|
-
for (let k in convert) {
|
|
728
|
-
if (t.hasOwnProperty(k)) {
|
|
729
|
-
t[convert[k]] = t[k];
|
|
730
|
-
delete t[k];
|
|
731
|
-
}
|
|
732
|
-
}
|
|
733
|
-
}
|
|
734
|
-
}
|
|
735
|
-
return res;
|
|
736
|
-
}
|
|
737
|
-
export async function getIndexes(query, fetchOptions) {
|
|
738
|
-
if (!query?.table) {
|
|
739
|
-
throw new SkapiError('"table" is required.', { code: 'INVALID_PARAMETER' });
|
|
740
|
-
}
|
|
741
|
-
let p = validator.Params(query || {}, {
|
|
742
|
-
table: 'string',
|
|
743
|
-
index: (v) => validator.specialChars(v, 'index name', true, false),
|
|
744
|
-
order: {
|
|
745
|
-
by: [
|
|
746
|
-
'average_number',
|
|
747
|
-
'total_number',
|
|
748
|
-
'number_count',
|
|
749
|
-
'average_bool',
|
|
750
|
-
'total_bool',
|
|
751
|
-
'bool_count',
|
|
752
|
-
'string_count',
|
|
753
|
-
'index_name',
|
|
754
|
-
'number_of_records'
|
|
755
|
-
],
|
|
756
|
-
value: ['string', 'number', 'boolean'],
|
|
757
|
-
condition: ['gt', 'gte', 'lt', 'lte', '>', '>=', '<', '<=', '=', 'eq', '!=', 'ne']
|
|
758
|
-
}
|
|
759
|
-
}, ['table']);
|
|
760
|
-
if (p.hasOwnProperty('order')) {
|
|
761
|
-
if (!p.order?.by) {
|
|
762
|
-
throw new SkapiError('"order.by" is required.', { code: 'INVALID_PARAMETER' });
|
|
763
|
-
}
|
|
764
|
-
if (p.order.hasOwnProperty('condition') && !p.order.hasOwnProperty('value')) {
|
|
765
|
-
throw new SkapiError('"value" is required for "condition".', { code: 'INVALID_PARAMETER' });
|
|
766
|
-
}
|
|
767
|
-
if (p.hasOwnProperty('index')) {
|
|
768
|
-
if (p.index.substring(p.index.length - 1) !== '.') {
|
|
769
|
-
throw new SkapiError('"index" should be a parent index name of the compound index when using "order.by"', { code: 'INVALID_PARAMETER' });
|
|
770
|
-
}
|
|
771
|
-
}
|
|
772
|
-
}
|
|
773
|
-
let res = await request.bind(this)('get-index', p, Object.assign({ auth: !!this.__user }, { fetchOptions }));
|
|
774
|
-
let convert = {
|
|
775
|
-
'cnt_bool': 'boolean_count',
|
|
776
|
-
'cnt_numb': 'number_count',
|
|
777
|
-
'totl_numb': 'total_number',
|
|
778
|
-
'totl_bool': 'total_bool',
|
|
779
|
-
'avrg_numb': 'average_number',
|
|
780
|
-
'avrg_bool': 'average_bool',
|
|
781
|
-
'cnt_str': 'string_count'
|
|
782
|
-
};
|
|
783
|
-
if (Array.isArray(res?.list)) {
|
|
784
|
-
res.list = res.list.map((i) => {
|
|
785
|
-
let iSplit = i.idx.split('/');
|
|
786
|
-
let resolved = {
|
|
787
|
-
table: iSplit[1],
|
|
788
|
-
index: iSplit[2],
|
|
789
|
-
number_of_records: i.cnt_rec
|
|
790
|
-
};
|
|
791
|
-
for (let k in convert) {
|
|
792
|
-
if (i?.[k]) {
|
|
793
|
-
resolved[convert[k]] = i[k];
|
|
794
|
-
}
|
|
795
|
-
}
|
|
796
|
-
return resolved;
|
|
797
|
-
});
|
|
798
|
-
}
|
|
799
|
-
return res;
|
|
800
|
-
}
|
|
801
|
-
export async function getTags(query, fetchOptions) {
|
|
802
|
-
let res = await request.bind(this)('get-tag', validator.Params(query || {}, {
|
|
803
|
-
table: 'string',
|
|
804
|
-
tag: 'string',
|
|
805
|
-
condition: ['gt', 'gte', 'lt', 'lte', '>', '>=', '<', '<=', '=', 'eq', '!=', 'ne']
|
|
806
|
-
}), Object.assign({ auth: !!this.__user }, { fetchOptions }));
|
|
807
|
-
if (Array.isArray(res?.list)) {
|
|
808
|
-
res.list = res.list.map(item => {
|
|
809
|
-
let tSplit = item.tag.split('/');
|
|
810
|
-
return {
|
|
811
|
-
table: tSplit[1],
|
|
812
|
-
tag: tSplit[0],
|
|
813
|
-
number_of_records: item.cnt_rec
|
|
814
|
-
};
|
|
815
|
-
});
|
|
816
|
-
}
|
|
817
|
-
return res;
|
|
818
|
-
}
|
|
819
|
-
export async function getUniqueId(query, fetchOptions) {
|
|
820
|
-
let res = await request.bind(this)('get-uniqueid', validator.Params(query || {}, {
|
|
821
|
-
unique_id: 'string',
|
|
822
|
-
condition: ['gt', 'gte', 'lt', 'lte', '>', '>=', '<', '<=', '=', 'eq', '!=', 'ne']
|
|
823
|
-
}), Object.assign({ auth: !!this.__user }, { fetchOptions }));
|
|
824
|
-
if (Array.isArray(res?.list)) {
|
|
825
|
-
for (let i in res.list) {
|
|
826
|
-
let item = res.list[i];
|
|
827
|
-
res.list[i] = {
|
|
828
|
-
unique_id: item.unq,
|
|
829
|
-
record_id: item.rec
|
|
830
|
-
};
|
|
831
|
-
}
|
|
832
|
-
}
|
|
833
|
-
return res;
|
|
834
|
-
}
|
|
835
|
-
export async function deleteRecords(query, fetchOptions) {
|
|
836
|
-
await this.__connection;
|
|
837
|
-
let q = await prepGetParams.bind(this)(query, true);
|
|
838
|
-
let is_reference_fetch = q.is_reference_fetch;
|
|
839
|
-
let result = await request.bind(this)('del-records', q.query, { auth: true, fetchOptions });
|
|
840
|
-
if (is_reference_fetch && typeof result?.reference_private_key === 'string') {
|
|
841
|
-
this.__private_access_key[is_reference_fetch] = result.reference_private_key;
|
|
842
|
-
}
|
|
843
|
-
return result?.message || result;
|
|
844
|
-
}
|
|
845
|
-
export function grantPrivateRecordAccess(params) {
|
|
846
|
-
return recordAccess.bind(this)({
|
|
847
|
-
record_id: params.record_id,
|
|
848
|
-
user_id: params.user_id,
|
|
849
|
-
execute: 'add'
|
|
850
|
-
});
|
|
851
|
-
}
|
|
852
|
-
export function removePrivateRecordAccess(params) {
|
|
853
|
-
return recordAccess.bind(this)({
|
|
854
|
-
record_id: params.record_id,
|
|
855
|
-
user_id: params.user_id || null,
|
|
856
|
-
execute: 'remove'
|
|
857
|
-
});
|
|
858
|
-
}
|
|
859
|
-
export async function listPrivateRecordAccess(p, fetchOptions) {
|
|
860
|
-
let params = {
|
|
861
|
-
record_id: p.record_id || undefined,
|
|
862
|
-
user_id: p.user_id || undefined,
|
|
863
|
-
execute: 'list'
|
|
864
|
-
};
|
|
865
|
-
if (!params.record_id && !params.user_id) {
|
|
866
|
-
throw new SkapiError(`Either record_id or user_id must be provided.`, { code: 'INVALID_PARAMETER' });
|
|
867
|
-
}
|
|
868
|
-
if (params.user_id) {
|
|
869
|
-
if (typeof params.user_id === 'string') {
|
|
870
|
-
validator.UserId(params.user_id);
|
|
871
|
-
params.user_id = [params.user_id];
|
|
872
|
-
}
|
|
873
|
-
else if (Array.isArray(params.user_id)) {
|
|
874
|
-
for (let u of params.user_id) {
|
|
875
|
-
validator.UserId(u);
|
|
876
|
-
}
|
|
877
|
-
}
|
|
878
|
-
else {
|
|
879
|
-
throw new SkapiError(`user_id should be type: <string | string[]>`, { code: 'INVALID_PARAMETER' });
|
|
880
|
-
}
|
|
881
|
-
}
|
|
882
|
-
let mapper = (i) => {
|
|
883
|
-
if (i.rec_usr) {
|
|
884
|
-
i.record_id = i.rec_usr.split('/')[0];
|
|
885
|
-
i.user_id = i.rec_usr.split('/')[1];
|
|
886
|
-
}
|
|
887
|
-
else if (i.usr_rec) {
|
|
888
|
-
i.user_id = i.usr_rec.split('/')[0];
|
|
889
|
-
i.record_id = i.usr_rec.split('/')[1];
|
|
890
|
-
}
|
|
891
|
-
return i;
|
|
892
|
-
};
|
|
893
|
-
let list = await request.bind(this)('grant-private-access', params, { auth: true, fetchOptions });
|
|
894
|
-
list.list = list.list.map(mapper);
|
|
895
|
-
return list;
|
|
896
|
-
}
|
|
897
|
-
export function requestPrivateRecordAccessKey(params) {
|
|
898
|
-
let record_id = params.record_id;
|
|
899
|
-
let reference_id = params.reference_id || undefined;
|
|
900
|
-
if (!record_id) {
|
|
901
|
-
throw new SkapiError(`Record ID is required.`, { code: 'INVALID_PARAMETER' });
|
|
902
|
-
}
|
|
903
|
-
if (typeof record_id !== 'string') {
|
|
904
|
-
throw new SkapiError(`Record ID should be type: <string | string[]>`, { code: 'INVALID_PARAMETER' });
|
|
905
|
-
}
|
|
906
|
-
if (reference_id && typeof reference_id !== 'string') {
|
|
907
|
-
throw new SkapiError(`Reference ID should be type: <string>`, { code: 'INVALID_PARAMETER' });
|
|
908
|
-
}
|
|
909
|
-
if (this.__private_access_key[record_id]) {
|
|
910
|
-
return this.__private_access_key[record_id];
|
|
911
|
-
}
|
|
912
|
-
let res = request.bind(this)('request-private-access-key', { record_id, reference_id }, { auth: true });
|
|
913
|
-
this.__private_access_key[record_id] = res;
|
|
914
|
-
return res;
|
|
915
|
-
}
|
|
916
|
-
function recordAccess(params) {
|
|
917
|
-
let execute = params.execute;
|
|
918
|
-
let req = validator.Params(params, {
|
|
919
|
-
record_id: 'string',
|
|
920
|
-
user_id: (v) => {
|
|
921
|
-
if (!v) {
|
|
922
|
-
throw new SkapiError(`User ID is required.`, { code: 'INVALID_PARAMETER' });
|
|
923
|
-
}
|
|
924
|
-
let id = v;
|
|
925
|
-
if (typeof id === 'string') {
|
|
926
|
-
id = [id];
|
|
927
|
-
}
|
|
928
|
-
if (id.length > 100) {
|
|
929
|
-
throw new SkapiError(`Cannot process more than 100 users at once.`, { code: 'INVALID_REQUEST' });
|
|
930
|
-
}
|
|
931
|
-
for (let i of id) {
|
|
932
|
-
validator.UserId(i, 'User ID in "user_id"');
|
|
933
|
-
}
|
|
934
|
-
return id;
|
|
935
|
-
},
|
|
936
|
-
execute: ['add', 'remove']
|
|
937
|
-
}, [
|
|
938
|
-
'execute',
|
|
939
|
-
'record_id',
|
|
940
|
-
'user_id'
|
|
941
|
-
]);
|
|
942
|
-
if (!req.user_id) {
|
|
943
|
-
req.user_id = null;
|
|
944
|
-
}
|
|
945
|
-
return request.bind(this)('grant-private-access', req, { auth: true });
|
|
946
|
-
}
|
|
947
|
-
//# sourceMappingURL=database.js.map
|