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