skapi-js 0.1.124 → 0.2.0-alpha.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 +15 -0
- package/dist/skapi.js.map +1 -1
- package/esm/Main.d.ts +3 -0
- package/esm/Main.js +3 -0
- package/esm/Types.d.ts +194 -0
- package/esm/Types.js +1 -0
- package/esm/main/error.d.ts +8 -0
- package/esm/main/error.js +38 -0
- package/esm/main/skapi.d.ts +79 -0
- package/esm/main/skapi.js +296 -0
- package/esm/methods/database.d.ts +83 -0
- package/esm/methods/database.js +952 -0
- package/esm/methods/request.d.ts +36 -0
- package/esm/methods/request.js +687 -0
- package/esm/methods/subscription.d.ts +46 -0
- package/esm/methods/subscription.js +273 -0
- package/esm/methods/user.d.ts +67 -0
- package/esm/methods/user.js +790 -0
- package/esm/utils/utils.d.ts +20 -0
- package/esm/utils/utils.js +286 -0
- package/esm/utils/validator.d.ts +19 -0
- package/esm/utils/validator.js +295 -0
- package/package.json +9 -6
- package/dist/skapi.module.js +0 -3
- package/dist/skapi.module.js.LICENSE.txt +0 -21
- package/dist/skapi.module.js.map +0 -1
|
@@ -0,0 +1,687 @@
|
|
|
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 { MD5 } from '../utils/utils';
|
|
12
|
+
import validator from '../utils/validator';
|
|
13
|
+
const __pendingRequest = {};
|
|
14
|
+
export function getConnection() {
|
|
15
|
+
return this.__connection;
|
|
16
|
+
}
|
|
17
|
+
export function listHostDirectory(params, fetchOptions) {
|
|
18
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
19
|
+
let is_admin = yield this.checkAdmin();
|
|
20
|
+
if (is_admin) {
|
|
21
|
+
if (!(params === null || params === void 0 ? void 0 : params.dir)) {
|
|
22
|
+
params.dir = '/';
|
|
23
|
+
}
|
|
24
|
+
return request.bind(this)('list-host-directory', params, {
|
|
25
|
+
fetchOptions,
|
|
26
|
+
auth: true,
|
|
27
|
+
method: 'post'
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
return [];
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
export function request(url, data = null, options) {
|
|
34
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
|
|
35
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
36
|
+
options = options || {};
|
|
37
|
+
let { auth = false, method = 'post', meta = null, bypassAwaitConnection = false, } = options;
|
|
38
|
+
method = method.toLowerCase();
|
|
39
|
+
let __connection = bypassAwaitConnection ? null : (yield this.__connection);
|
|
40
|
+
if (!__connection && !bypassAwaitConnection) {
|
|
41
|
+
throw new SkapiError('Invalid connection. The service could have been disabled, or has a restricted CORS.', { code: 'INVALID_REQUEST' });
|
|
42
|
+
}
|
|
43
|
+
let token = auth ? (_b = (_a = this.session) === null || _a === void 0 ? void 0 : _a.idToken) === null || _b === void 0 ? void 0 : _b.jwtToken : null;
|
|
44
|
+
if (auth) {
|
|
45
|
+
if (!token) {
|
|
46
|
+
this.logout();
|
|
47
|
+
throw new SkapiError('User login is required.', { code: 'INVALID_REQUEST' });
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
let currTime = Date.now() / 1000;
|
|
51
|
+
if (this.session.idToken.payload.exp < currTime) {
|
|
52
|
+
try {
|
|
53
|
+
yield this.authentication().getSession({ refreshToken: true });
|
|
54
|
+
token = (_d = (_c = this.session) === null || _c === void 0 ? void 0 : _c.idToken) === null || _d === void 0 ? void 0 : _d.jwtToken;
|
|
55
|
+
}
|
|
56
|
+
catch (err) {
|
|
57
|
+
this.logout();
|
|
58
|
+
throw new SkapiError('User login is required.', { code: 'INVALID_REQUEST' });
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
let isExternalUrl = '';
|
|
64
|
+
try {
|
|
65
|
+
isExternalUrl = validator.Url(url);
|
|
66
|
+
}
|
|
67
|
+
catch (err) {
|
|
68
|
+
}
|
|
69
|
+
const getEndpoint = (dest, auth) => __awaiter(this, void 0, void 0, function* () {
|
|
70
|
+
var _o;
|
|
71
|
+
const endpoints = yield Promise.all([
|
|
72
|
+
this.admin_endpoint,
|
|
73
|
+
this.record_endpoint
|
|
74
|
+
]);
|
|
75
|
+
const admin = endpoints[0];
|
|
76
|
+
const record = endpoints[1];
|
|
77
|
+
const get_ep = () => {
|
|
78
|
+
switch (dest) {
|
|
79
|
+
case 'get-newsletters':
|
|
80
|
+
case 'get-public-newsletters':
|
|
81
|
+
case 'delete-newsletter':
|
|
82
|
+
case 'block-account':
|
|
83
|
+
case 'register-service':
|
|
84
|
+
case 'get-users':
|
|
85
|
+
case 'post-userdata':
|
|
86
|
+
case 'remove-account':
|
|
87
|
+
case 'post-secure':
|
|
88
|
+
case 'subscribe-newsletter':
|
|
89
|
+
case 'subscribe-public-newsletter':
|
|
90
|
+
case 'signup':
|
|
91
|
+
case 'confirm-signup':
|
|
92
|
+
case 'recover-account':
|
|
93
|
+
case 'mock':
|
|
94
|
+
case 'get-services':
|
|
95
|
+
case 'service':
|
|
96
|
+
case 'grant-access':
|
|
97
|
+
case 'last-verified-email':
|
|
98
|
+
case 'get-newsletter-subscription':
|
|
99
|
+
case 'request-username-change':
|
|
100
|
+
case 'register-subdomain':
|
|
101
|
+
case 'list-host-directory':
|
|
102
|
+
case 'refresh-cdn':
|
|
103
|
+
case 'request-newsletter-sender':
|
|
104
|
+
case 'set-404':
|
|
105
|
+
return {
|
|
106
|
+
public: admin.admin_public,
|
|
107
|
+
private: admin.admin_private
|
|
108
|
+
};
|
|
109
|
+
case 'post-record':
|
|
110
|
+
case 'get-records':
|
|
111
|
+
case 'subscription':
|
|
112
|
+
case 'get-subscription':
|
|
113
|
+
case 'del-records':
|
|
114
|
+
case 'get-table':
|
|
115
|
+
case 'get-tag':
|
|
116
|
+
case 'get-index':
|
|
117
|
+
case 'storage-info':
|
|
118
|
+
case 'get-signed-url':
|
|
119
|
+
case 'grant-private-access':
|
|
120
|
+
case 'request-private-access-key':
|
|
121
|
+
case 'del-files':
|
|
122
|
+
return {
|
|
123
|
+
private: record.record_private,
|
|
124
|
+
public: record.record_public
|
|
125
|
+
};
|
|
126
|
+
default:
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
return (((_o = get_ep()) === null || _o === void 0 ? void 0 : _o[auth ? 'private' : 'public']) || '') + dest;
|
|
131
|
+
});
|
|
132
|
+
let endpoint = isExternalUrl || (yield getEndpoint(url, !!auth));
|
|
133
|
+
let service = ((_f = (_e = this.session) === null || _e === void 0 ? void 0 : _e.attributes) === null || _f === void 0 ? void 0 : _f['custom:service']) || (__connection === null || __connection === void 0 ? void 0 : __connection.service) || this.service;
|
|
134
|
+
let owner = ((_h = (_g = this.session) === null || _g === void 0 ? void 0 : _g.attributes) === null || _h === void 0 ? void 0 : _h['custom:owner']) || (__connection === null || __connection === void 0 ? void 0 : __connection.owner) || this.owner;
|
|
135
|
+
if (meta) {
|
|
136
|
+
if (typeof meta === 'object' && !Array.isArray(meta)) {
|
|
137
|
+
meta = JSON.parse(JSON.stringify(meta));
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
throw new SkapiError('Invalid meta data.', { code: 'INVALID_REQUEST' });
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (Array.isArray(data) || data && typeof data !== 'object') {
|
|
144
|
+
throw new SkapiError('Request data should be a JSON Object | FormData | HTMLFormElement.', { code: 'INVALID_REQUEST' });
|
|
145
|
+
}
|
|
146
|
+
let required = (options === null || options === void 0 ? void 0 : options.responseType) !== 'blob' ? { service, owner } : {};
|
|
147
|
+
let fetchOptions = {};
|
|
148
|
+
let { fetchMore = false, progress } = (options === null || options === void 0 ? void 0 : options.fetchOptions) || {};
|
|
149
|
+
if ((options === null || options === void 0 ? void 0 : options.fetchOptions) && Object.keys(options.fetchOptions).length) {
|
|
150
|
+
let fetOpt = validator.Params({
|
|
151
|
+
limit: ((_j = options.fetchOptions) === null || _j === void 0 ? void 0 : _j.limit) || 50,
|
|
152
|
+
startKey: ((_k = options.fetchOptions) === null || _k === void 0 ? void 0 : _k.startKey) || null,
|
|
153
|
+
ascending: typeof ((_l = options.fetchOptions) === null || _l === void 0 ? void 0 : _l.ascending) === 'boolean' ? options.fetchOptions.ascending : true
|
|
154
|
+
}, {
|
|
155
|
+
limit: ['number', () => 50],
|
|
156
|
+
startKey: null,
|
|
157
|
+
ascending: ['boolean', () => true]
|
|
158
|
+
});
|
|
159
|
+
if (fetOpt.hasOwnProperty('limit') && typeof fetOpt.limit === 'number') {
|
|
160
|
+
if (fetOpt.limit > 1000) {
|
|
161
|
+
throw new SkapiError('Fetch limit should be below 1000.', { code: 'INVALID_REQUEST' });
|
|
162
|
+
}
|
|
163
|
+
Object.assign(fetchOptions, { limit: fetOpt.limit });
|
|
164
|
+
}
|
|
165
|
+
if (fetOpt.hasOwnProperty('startKey') && typeof fetOpt.startKey === 'object' && fetOpt.startKey && Object.keys(fetOpt.startKey)) {
|
|
166
|
+
Object.assign(fetchOptions, { startKey: fetOpt.startKey });
|
|
167
|
+
}
|
|
168
|
+
if (fetOpt.hasOwnProperty('ascending') && typeof fetOpt.ascending === 'boolean') {
|
|
169
|
+
Object.assign(fetchOptions, { ascending: fetOpt.ascending });
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
Object.assign(required, fetchOptions);
|
|
173
|
+
let isForm = false;
|
|
174
|
+
if (data instanceof SubmitEvent) {
|
|
175
|
+
data = data === null || data === void 0 ? void 0 : data.target;
|
|
176
|
+
}
|
|
177
|
+
if (data instanceof HTMLFormElement) {
|
|
178
|
+
data = new FormData(data);
|
|
179
|
+
}
|
|
180
|
+
if (data instanceof FormData) {
|
|
181
|
+
isForm = true;
|
|
182
|
+
}
|
|
183
|
+
if (meta) {
|
|
184
|
+
meta = Object.assign(required, meta);
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
if (!data) {
|
|
188
|
+
data = required;
|
|
189
|
+
}
|
|
190
|
+
else if (isForm) {
|
|
191
|
+
for (let k in required) {
|
|
192
|
+
if (required[k] !== undefined) {
|
|
193
|
+
data.set(k, new Blob([JSON.stringify(required[k])], {
|
|
194
|
+
type: 'application/json'
|
|
195
|
+
}));
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
data = Object.assign(required, data);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
if (isForm && typeof ((_m = options === null || options === void 0 ? void 0 : options.fetchOptions) === null || _m === void 0 ? void 0 : _m.formData) === 'function') {
|
|
204
|
+
let cb = options.fetchOptions.formData(data);
|
|
205
|
+
if (cb instanceof Promise) {
|
|
206
|
+
cb = yield cb;
|
|
207
|
+
}
|
|
208
|
+
if (cb instanceof FormData) {
|
|
209
|
+
data = cb;
|
|
210
|
+
}
|
|
211
|
+
if (data instanceof FormData) {
|
|
212
|
+
let totalFileSize = 0;
|
|
213
|
+
for (let pair of data.entries()) {
|
|
214
|
+
let v = pair[1];
|
|
215
|
+
if (v instanceof File) {
|
|
216
|
+
totalFileSize += v.size;
|
|
217
|
+
}
|
|
218
|
+
else if (v instanceof FileList) {
|
|
219
|
+
if (v && v.length > 0) {
|
|
220
|
+
for (let idx = 0; idx <= v.length - 1; idx++) {
|
|
221
|
+
totalFileSize += v.item(idx).size;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
if (totalFileSize > 4200000) {
|
|
227
|
+
throw new SkapiError('Files cannot exceed 4MB. Use skapi.uploadFiles(...) instead.', { code: 'INVALID_REQUEST' });
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
else {
|
|
231
|
+
throw new SkapiError('Callback for extractFormData() should return FormData', { code: 'INVALID_PARAMETER' });
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
let requestKey = load_startKey_keys.bind(this)({
|
|
235
|
+
params: data,
|
|
236
|
+
url: isExternalUrl || url,
|
|
237
|
+
fetchMore: isForm ? false : fetchMore
|
|
238
|
+
});
|
|
239
|
+
if (requestKey && typeof requestKey === 'object') {
|
|
240
|
+
return requestKey;
|
|
241
|
+
}
|
|
242
|
+
if (typeof requestKey === 'string') {
|
|
243
|
+
if (!(__pendingRequest[requestKey] instanceof Promise)) {
|
|
244
|
+
let headers = {
|
|
245
|
+
'Accept': '*/*'
|
|
246
|
+
};
|
|
247
|
+
if (token) {
|
|
248
|
+
headers.Authorization = token;
|
|
249
|
+
}
|
|
250
|
+
if (meta) {
|
|
251
|
+
headers["Content-Meta"] = window.btoa(encodeURIComponent(typeof meta === 'string' ? meta : JSON.stringify(meta)));
|
|
252
|
+
}
|
|
253
|
+
if (options.hasOwnProperty('contentType')) {
|
|
254
|
+
if (options === null || options === void 0 ? void 0 : options.contentType) {
|
|
255
|
+
headers["Content-Type"] = options.contentType;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
else if (!(data instanceof FormData)) {
|
|
259
|
+
headers["Content-Type"] = 'application/json';
|
|
260
|
+
}
|
|
261
|
+
let opt = { headers };
|
|
262
|
+
if (options === null || options === void 0 ? void 0 : options.responseType) {
|
|
263
|
+
opt.responseType = options.responseType;
|
|
264
|
+
}
|
|
265
|
+
if (method === 'post') {
|
|
266
|
+
__pendingRequest[requestKey] = _post.bind(this)(endpoint, data, opt, progress);
|
|
267
|
+
}
|
|
268
|
+
else if (method === 'get') {
|
|
269
|
+
__pendingRequest[requestKey] = _get.bind(this)(endpoint, data, opt, progress);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
try {
|
|
273
|
+
let response = yield __pendingRequest[requestKey];
|
|
274
|
+
if (isForm) {
|
|
275
|
+
return response;
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
return yield update_startKey_keys.bind(this)({
|
|
279
|
+
hashedParam: requestKey,
|
|
280
|
+
url,
|
|
281
|
+
response
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
catch (err) {
|
|
286
|
+
throw err;
|
|
287
|
+
}
|
|
288
|
+
finally {
|
|
289
|
+
if (requestKey && __pendingRequest.hasOwnProperty(requestKey)) {
|
|
290
|
+
delete __pendingRequest[requestKey];
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
;
|
|
297
|
+
function load_startKey_keys(option) {
|
|
298
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
|
|
299
|
+
let { params = {}, url, fetchMore = false } = option || {};
|
|
300
|
+
if (params.hasOwnProperty('startKey') && params.startKey) {
|
|
301
|
+
if (typeof params.startKey !== 'object' && !Object.keys(params.startKey).length &&
|
|
302
|
+
params.startKey !== 'start' && params.startKey !== 'end') {
|
|
303
|
+
throw new SkapiError(`"${params.startKey}" is invalid startKey key.`, { code: 'INVALID_PARAMETER' });
|
|
304
|
+
}
|
|
305
|
+
switch (params.startKey) {
|
|
306
|
+
case 'start':
|
|
307
|
+
fetchMore = false;
|
|
308
|
+
delete params.startKey;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
let hashedParams = (() => {
|
|
312
|
+
if (params && typeof params === 'object' && Object.keys(params).length) {
|
|
313
|
+
function orderObjectKeys(obj) {
|
|
314
|
+
function sortObject(obj) {
|
|
315
|
+
if (typeof obj === 'object' && obj) {
|
|
316
|
+
return Object.keys(obj).sort().reduce((res, key) => (res[key] = obj[key], res), {});
|
|
317
|
+
}
|
|
318
|
+
return obj;
|
|
319
|
+
}
|
|
320
|
+
;
|
|
321
|
+
let _obj = sortObject(obj);
|
|
322
|
+
if (_obj.hasOwnProperty('limit')) {
|
|
323
|
+
delete _obj.limit;
|
|
324
|
+
}
|
|
325
|
+
for (let k in _obj) {
|
|
326
|
+
if (_obj[k] && typeof _obj[k] === 'object') {
|
|
327
|
+
_obj[k] = sortObject(obj[k]);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
return _obj;
|
|
331
|
+
}
|
|
332
|
+
return MD5.hash(url + '/' + JSON.stringify(orderObjectKeys(params)));
|
|
333
|
+
}
|
|
334
|
+
return MD5.hash(url + '/' + this.service);
|
|
335
|
+
})();
|
|
336
|
+
if (!fetchMore && ((_b = (_a = this.__startKeyHistory) === null || _a === void 0 ? void 0 : _a[url]) === null || _b === void 0 ? void 0 : _b[hashedParams])) {
|
|
337
|
+
if (((_c = this.__cached_requests) === null || _c === void 0 ? void 0 : _c[url]) && ((_e = (_d = this.__cached_requests) === null || _d === void 0 ? void 0 : _d[url]) === null || _e === void 0 ? void 0 : _e[hashedParams])) {
|
|
338
|
+
delete this.__cached_requests[url][hashedParams];
|
|
339
|
+
}
|
|
340
|
+
if (Array.isArray(this.__startKeyHistory[url][hashedParams]) && this.__startKeyHistory[url][hashedParams].length) {
|
|
341
|
+
for (let p of this.__startKeyHistory[url][hashedParams]) {
|
|
342
|
+
let hashedParams_cached = hashedParams + '/' + MD5.hash(JSON.stringify(p));
|
|
343
|
+
if (((_f = this.__cached_requests) === null || _f === void 0 ? void 0 : _f[url]) && ((_h = (_g = this.__cached_requests) === null || _g === void 0 ? void 0 : _g[url]) === null || _h === void 0 ? void 0 : _h[hashedParams_cached])) {
|
|
344
|
+
delete this.__cached_requests[url][hashedParams_cached];
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
delete this.__startKeyHistory[url][hashedParams];
|
|
349
|
+
return hashedParams;
|
|
350
|
+
}
|
|
351
|
+
if (!Array.isArray((_k = (_j = this.__startKeyHistory) === null || _j === void 0 ? void 0 : _j[url]) === null || _k === void 0 ? void 0 : _k[hashedParams])) {
|
|
352
|
+
return hashedParams;
|
|
353
|
+
}
|
|
354
|
+
let list_of_startKeys = this.__startKeyHistory[url][hashedParams];
|
|
355
|
+
let last_startKey_key = list_of_startKeys[list_of_startKeys.length - 1];
|
|
356
|
+
let cache_hashedParams = hashedParams;
|
|
357
|
+
if (last_startKey_key) {
|
|
358
|
+
if (last_startKey_key === 'end') {
|
|
359
|
+
return {
|
|
360
|
+
list: [],
|
|
361
|
+
startKey: 'end',
|
|
362
|
+
endOfList: true,
|
|
363
|
+
startKeyHistory: this.__startKeyHistory[url][hashedParams]
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
else {
|
|
367
|
+
cache_hashedParams += MD5.hash(last_startKey_key);
|
|
368
|
+
params.startKey = JSON.parse(last_startKey_key);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
if ((_m = (_l = this.__cached_requests) === null || _l === void 0 ? void 0 : _l[url]) === null || _m === void 0 ? void 0 : _m[cache_hashedParams]) {
|
|
372
|
+
return this.__cached_requests[url][cache_hashedParams];
|
|
373
|
+
}
|
|
374
|
+
return hashedParams;
|
|
375
|
+
}
|
|
376
|
+
;
|
|
377
|
+
function update_startKey_keys(option) {
|
|
378
|
+
var _a;
|
|
379
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
380
|
+
let { hashedParam, url, response } = option;
|
|
381
|
+
let fetched = null;
|
|
382
|
+
if (response instanceof Promise) {
|
|
383
|
+
fetched = yield response;
|
|
384
|
+
}
|
|
385
|
+
else {
|
|
386
|
+
fetched = response;
|
|
387
|
+
}
|
|
388
|
+
if (!fetched ||
|
|
389
|
+
typeof fetched !== 'object' ||
|
|
390
|
+
!fetched.hasOwnProperty('startKey') ||
|
|
391
|
+
!hashedParam ||
|
|
392
|
+
!url) {
|
|
393
|
+
return fetched;
|
|
394
|
+
}
|
|
395
|
+
if (!this.__startKeyHistory.hasOwnProperty(url)) {
|
|
396
|
+
this.__startKeyHistory[url] = {};
|
|
397
|
+
}
|
|
398
|
+
if (!((_a = this.__cached_requests) === null || _a === void 0 ? void 0 : _a[url])) {
|
|
399
|
+
this.__cached_requests[url] = {};
|
|
400
|
+
}
|
|
401
|
+
this.__cached_requests[url][hashedParam] = fetched;
|
|
402
|
+
if (!this.__startKeyHistory[url].hasOwnProperty(hashedParam)) {
|
|
403
|
+
this.__startKeyHistory[url][hashedParam] = [];
|
|
404
|
+
}
|
|
405
|
+
let startKey_string = fetched.startKey === 'end' ? 'end' : JSON.stringify(fetched.startKey);
|
|
406
|
+
if (!this.__startKeyHistory[url][hashedParam].includes(startKey_string)) {
|
|
407
|
+
this.__startKeyHistory[url][hashedParam].push(startKey_string);
|
|
408
|
+
}
|
|
409
|
+
this.__cached_requests[url][hashedParam] = fetched;
|
|
410
|
+
return Object.assign({ startKeyHistory: this.__startKeyHistory[url][hashedParam] }, fetched);
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
;
|
|
414
|
+
function _fetch(url, opt, progress) {
|
|
415
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
416
|
+
let fetchProgress = (url, opts, progress) => {
|
|
417
|
+
return new Promise((res, rej) => {
|
|
418
|
+
let xhr = new XMLHttpRequest();
|
|
419
|
+
xhr.open((opt.method || 'GET').toUpperCase(), url);
|
|
420
|
+
for (var k in opts.headers || {}) {
|
|
421
|
+
xhr.setRequestHeader(k, opts.headers[k]);
|
|
422
|
+
}
|
|
423
|
+
if (opt.responseType) {
|
|
424
|
+
xhr.responseType = opt.responseType;
|
|
425
|
+
}
|
|
426
|
+
xhr.onload = () => {
|
|
427
|
+
if (xhr.status >= 200 && xhr.status < 300) {
|
|
428
|
+
if (opts.responseType == 'json' || opts.responseType == 'blob') {
|
|
429
|
+
res(xhr.response);
|
|
430
|
+
}
|
|
431
|
+
else {
|
|
432
|
+
let result = xhr.responseText;
|
|
433
|
+
try {
|
|
434
|
+
result = JSON.parse(result);
|
|
435
|
+
}
|
|
436
|
+
catch (err) { }
|
|
437
|
+
res(result);
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
else {
|
|
441
|
+
let status = xhr.status;
|
|
442
|
+
let errCode = [
|
|
443
|
+
'INVALID_CORS',
|
|
444
|
+
'INVALID_REQUEST',
|
|
445
|
+
'SERVICE_DISABLED',
|
|
446
|
+
'INVALID_PARAMETER',
|
|
447
|
+
'ERROR',
|
|
448
|
+
'EXISTS',
|
|
449
|
+
'NOT_EXISTS'
|
|
450
|
+
];
|
|
451
|
+
let result = xhr.responseText;
|
|
452
|
+
try {
|
|
453
|
+
result = JSON.parse(result);
|
|
454
|
+
}
|
|
455
|
+
catch (err) { }
|
|
456
|
+
if (typeof result === 'string') {
|
|
457
|
+
let errMsg = xhr.response.split(':');
|
|
458
|
+
let code = errMsg.splice(0, 1)[0].trim();
|
|
459
|
+
rej(new SkapiError(errMsg.join('').trim(), { code: (errCode.includes(code) ? code : 'ERROR') }));
|
|
460
|
+
}
|
|
461
|
+
else if (typeof result === 'object' && (result === null || result === void 0 ? void 0 : result.message)) {
|
|
462
|
+
let code = ((result === null || result === void 0 ? void 0 : result.code) || (status ? status.toString() : null) || 'ERROR');
|
|
463
|
+
rej(new SkapiError(result === null || result === void 0 ? void 0 : result.message, { code: code }));
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
};
|
|
467
|
+
xhr.onerror = () => rej('Network error');
|
|
468
|
+
xhr.onabort = () => rej('Aborted');
|
|
469
|
+
xhr.ontimeout = () => rej('Timeout');
|
|
470
|
+
if (typeof progress === 'function') {
|
|
471
|
+
xhr.onprogress = (p) => {
|
|
472
|
+
progress({
|
|
473
|
+
status: 'download',
|
|
474
|
+
progress: p.loaded / p.total * 100,
|
|
475
|
+
loaded: p.loaded,
|
|
476
|
+
total: p.total,
|
|
477
|
+
abort: () => xhr.abort()
|
|
478
|
+
});
|
|
479
|
+
};
|
|
480
|
+
if (xhr.upload) {
|
|
481
|
+
xhr.upload.onprogress = (p) => {
|
|
482
|
+
progress({
|
|
483
|
+
status: 'upload',
|
|
484
|
+
progress: p.loaded / p.total * 100,
|
|
485
|
+
loaded: p.loaded,
|
|
486
|
+
total: p.total,
|
|
487
|
+
abort: () => xhr.abort()
|
|
488
|
+
});
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
xhr.send(opts.body);
|
|
493
|
+
});
|
|
494
|
+
};
|
|
495
|
+
let received = yield fetchProgress(url, {
|
|
496
|
+
headers: opt === null || opt === void 0 ? void 0 : opt.headers,
|
|
497
|
+
body: opt.body,
|
|
498
|
+
responseType: opt === null || opt === void 0 ? void 0 : opt.responseType
|
|
499
|
+
}, progress);
|
|
500
|
+
if (typeof received === 'object' && opt.method === 'GET' && received.hasOwnProperty('body')) {
|
|
501
|
+
try {
|
|
502
|
+
received = JSON.parse(received.body);
|
|
503
|
+
}
|
|
504
|
+
catch (err) { }
|
|
505
|
+
}
|
|
506
|
+
return received;
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
;
|
|
510
|
+
function _post(url, params, option, progress) {
|
|
511
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
512
|
+
let opt = Object.assign({
|
|
513
|
+
method: 'POST'
|
|
514
|
+
}, option, {
|
|
515
|
+
body: params instanceof FormData ? params : JSON.stringify(params)
|
|
516
|
+
});
|
|
517
|
+
return _fetch.bind(this)(url, opt, progress);
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
;
|
|
521
|
+
function _get(url, params, option, progress) {
|
|
522
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
523
|
+
if (params && typeof params === 'object' && Object.keys(params).length) {
|
|
524
|
+
if (url.substring(url.length - 1) !== '?') {
|
|
525
|
+
url = url + '?';
|
|
526
|
+
}
|
|
527
|
+
let query = Object.keys(params)
|
|
528
|
+
.map(k => {
|
|
529
|
+
let value = params[k];
|
|
530
|
+
if (typeof value !== 'string') {
|
|
531
|
+
value = JSON.stringify(value);
|
|
532
|
+
}
|
|
533
|
+
return encodeURIComponent(k) + '=' + encodeURIComponent(value);
|
|
534
|
+
})
|
|
535
|
+
.join('&');
|
|
536
|
+
url += query;
|
|
537
|
+
}
|
|
538
|
+
let opt = Object.assign({
|
|
539
|
+
method: 'GET'
|
|
540
|
+
}, option);
|
|
541
|
+
return _fetch.bind(this)(url, opt, progress);
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
;
|
|
545
|
+
export function secureRequest(params) {
|
|
546
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
547
|
+
let paramsStruct = {
|
|
548
|
+
url: (v) => {
|
|
549
|
+
return validator.Url(v);
|
|
550
|
+
},
|
|
551
|
+
data: null,
|
|
552
|
+
sync: ['boolean', () => true]
|
|
553
|
+
};
|
|
554
|
+
if (Array.isArray(params)) {
|
|
555
|
+
for (let r of params) {
|
|
556
|
+
r = validator.Params(r, paramsStruct);
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
else {
|
|
560
|
+
params = validator.Params(params, paramsStruct);
|
|
561
|
+
}
|
|
562
|
+
return yield request.bind(this)('post-secure', params, { auth: true });
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
;
|
|
566
|
+
export function mock(data, formCallback, options) {
|
|
567
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
568
|
+
options = options || {};
|
|
569
|
+
if (formCallback && (formCallback === null || formCallback === void 0 ? void 0 : formCallback.formData) && typeof formCallback.formData === 'function') {
|
|
570
|
+
let fetchOptions = {
|
|
571
|
+
formData: formCallback.formData
|
|
572
|
+
};
|
|
573
|
+
Object.assign(options, fetchOptions);
|
|
574
|
+
}
|
|
575
|
+
return request.bind(this)('mock', data, options);
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
;
|
|
579
|
+
export function getFormResponse() {
|
|
580
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
581
|
+
yield this.__connection;
|
|
582
|
+
let responseKey = `${this.service}:${MD5.hash(window.location.href.split('?')[0])}`;
|
|
583
|
+
let stored = window.sessionStorage.getItem(responseKey);
|
|
584
|
+
if (stored !== null) {
|
|
585
|
+
try {
|
|
586
|
+
stored = JSON.parse(stored);
|
|
587
|
+
}
|
|
588
|
+
catch (err) { }
|
|
589
|
+
return stored;
|
|
590
|
+
}
|
|
591
|
+
throw new SkapiError("Form response doesn't exist.", { code: 'NOT_EXISTS' });
|
|
592
|
+
});
|
|
593
|
+
}
|
|
594
|
+
;
|
|
595
|
+
const pendPromise = {};
|
|
596
|
+
export function formHandler(options) {
|
|
597
|
+
let { preventMultipleCalls = false } = options || {};
|
|
598
|
+
return function (target, propertyKey, descriptor) {
|
|
599
|
+
const fn = descriptor.value;
|
|
600
|
+
descriptor.value = function (...arg) {
|
|
601
|
+
let form = arg[0];
|
|
602
|
+
let option = (arg === null || arg === void 0 ? void 0 : arg[1]) || {};
|
|
603
|
+
let routeWithDataKey = true;
|
|
604
|
+
let formEl = null;
|
|
605
|
+
let actionDestination = '';
|
|
606
|
+
if (form instanceof SubmitEvent) {
|
|
607
|
+
form.preventDefault();
|
|
608
|
+
let currentUrl = window.location.href;
|
|
609
|
+
formEl = form.target;
|
|
610
|
+
let href = new URL(formEl.action);
|
|
611
|
+
actionDestination = href.href;
|
|
612
|
+
if (!formEl.action || href.href === currentUrl) {
|
|
613
|
+
routeWithDataKey = false;
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
const handleResponse = (response) => {
|
|
617
|
+
if (option === null || option === void 0 ? void 0 : option.response) {
|
|
618
|
+
if (typeof option.response === 'function')
|
|
619
|
+
return option.response(response);
|
|
620
|
+
else
|
|
621
|
+
throw new SkapiError('Callback "response" should be type: function.', { code: 'INVALID_PARAMETER' });
|
|
622
|
+
}
|
|
623
|
+
if (formEl) {
|
|
624
|
+
if (routeWithDataKey) {
|
|
625
|
+
window.sessionStorage.setItem(`${this.service}:${MD5.hash(actionDestination)}`, JSON.stringify(response));
|
|
626
|
+
window.location.href = actionDestination;
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
return response;
|
|
630
|
+
};
|
|
631
|
+
let response;
|
|
632
|
+
let handleError = (err) => {
|
|
633
|
+
if (form instanceof SubmitEvent) {
|
|
634
|
+
form.preventDefault();
|
|
635
|
+
}
|
|
636
|
+
let is_err = err instanceof Error ? err : new SkapiError(err);
|
|
637
|
+
if (option === null || option === void 0 ? void 0 : option.onerror) {
|
|
638
|
+
if (typeof option.onerror === 'function')
|
|
639
|
+
return option.onerror(is_err);
|
|
640
|
+
else
|
|
641
|
+
throw new SkapiError('Callback "onerror" should be type: function.', { code: 'INVALID_PARAMETER' });
|
|
642
|
+
}
|
|
643
|
+
throw is_err;
|
|
644
|
+
};
|
|
645
|
+
const executeMethod = () => {
|
|
646
|
+
try {
|
|
647
|
+
response = fn.bind(this)(...arg);
|
|
648
|
+
}
|
|
649
|
+
catch (err) {
|
|
650
|
+
let is_err = handleError(err);
|
|
651
|
+
if (is_err instanceof Error) {
|
|
652
|
+
throw is_err;
|
|
653
|
+
}
|
|
654
|
+
return is_err;
|
|
655
|
+
}
|
|
656
|
+
if (response instanceof Promise) {
|
|
657
|
+
return (() => __awaiter(this, void 0, void 0, function* () {
|
|
658
|
+
try {
|
|
659
|
+
let resolved = yield response;
|
|
660
|
+
return handleResponse(resolved);
|
|
661
|
+
}
|
|
662
|
+
catch (err) {
|
|
663
|
+
return handleError(err);
|
|
664
|
+
}
|
|
665
|
+
}))();
|
|
666
|
+
}
|
|
667
|
+
};
|
|
668
|
+
if (preventMultipleCalls) {
|
|
669
|
+
return (() => __awaiter(this, void 0, void 0, function* () {
|
|
670
|
+
if ((pendPromise === null || pendPromise === void 0 ? void 0 : pendPromise[propertyKey]) instanceof Promise) {
|
|
671
|
+
let res = yield pendPromise[propertyKey];
|
|
672
|
+
pendPromise[propertyKey] = null;
|
|
673
|
+
return res;
|
|
674
|
+
}
|
|
675
|
+
else {
|
|
676
|
+
pendPromise[propertyKey] = executeMethod().finally(() => {
|
|
677
|
+
pendPromise[propertyKey] = null;
|
|
678
|
+
});
|
|
679
|
+
return pendPromise[propertyKey];
|
|
680
|
+
}
|
|
681
|
+
}))();
|
|
682
|
+
}
|
|
683
|
+
return executeMethod();
|
|
684
|
+
};
|
|
685
|
+
};
|
|
686
|
+
}
|
|
687
|
+
;
|