skapi-js 0.2.0 → 0.2.2
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.map +1 -1
- package/dist/skapi.module.js +1 -1
- package/dist/skapi.module.js.map +1 -1
- package/js/Main.d.ts +3 -0
- package/js/Main.js +3 -0
- package/js/Types.d.ts +194 -0
- package/js/Types.js +1 -0
- package/js/main/error.d.ts +8 -0
- package/js/main/error.js +38 -0
- package/js/main/skapi.d.ts +79 -0
- package/js/main/skapi.js +285 -0
- package/js/methods/database.d.ts +83 -0
- package/js/methods/database.js +915 -0
- package/js/methods/request.d.ts +36 -0
- package/js/methods/request.js +656 -0
- package/js/methods/subscription.d.ts +46 -0
- package/js/methods/subscription.js +240 -0
- package/js/methods/user.d.ts +67 -0
- package/js/methods/user.js +747 -0
- package/js/utils/utils.d.ts +20 -0
- package/js/utils/utils.js +286 -0
- package/js/utils/validator.d.ts +19 -0
- package/js/utils/validator.js +294 -0
- package/package.json +5 -3
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Form } from "../Types";
|
|
2
|
+
declare class MD5 {
|
|
3
|
+
private static readonly alphabet;
|
|
4
|
+
static hash(str?: string): string;
|
|
5
|
+
private static rh;
|
|
6
|
+
private static ad;
|
|
7
|
+
private static rl;
|
|
8
|
+
private static cm;
|
|
9
|
+
private static ff;
|
|
10
|
+
private static gg;
|
|
11
|
+
private static hh;
|
|
12
|
+
private static ii;
|
|
13
|
+
private static sb;
|
|
14
|
+
}
|
|
15
|
+
declare function generateRandom(length?: number): string;
|
|
16
|
+
declare function extractFormMeta(form: Form<any>): {
|
|
17
|
+
meta: {};
|
|
18
|
+
files: any[];
|
|
19
|
+
};
|
|
20
|
+
export { extractFormMeta, MD5, generateRandom };
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
import SkapiError from "../main/error";
|
|
2
|
+
class MD5 {
|
|
3
|
+
static hash(str) {
|
|
4
|
+
if (typeof str !== 'string') {
|
|
5
|
+
console.warn('coercing non-string value to empty string');
|
|
6
|
+
str = '';
|
|
7
|
+
}
|
|
8
|
+
const x = MD5.sb(str);
|
|
9
|
+
let a = 1732584193;
|
|
10
|
+
let b = -271733879;
|
|
11
|
+
let c = -1732584194;
|
|
12
|
+
let d = 271733878;
|
|
13
|
+
let lastA;
|
|
14
|
+
let lastB;
|
|
15
|
+
let lastC;
|
|
16
|
+
let lastD;
|
|
17
|
+
for (let i = 0; i < x.length; i += 16) {
|
|
18
|
+
lastA = a;
|
|
19
|
+
lastB = b;
|
|
20
|
+
lastC = c;
|
|
21
|
+
lastD = d;
|
|
22
|
+
a = MD5.ff(a, b, c, d, x[i], 7, -680876936);
|
|
23
|
+
d = MD5.ff(d, a, b, c, x[i + 1], 12, -389564586);
|
|
24
|
+
c = MD5.ff(c, d, a, b, x[i + 2], 17, 606105819);
|
|
25
|
+
b = MD5.ff(b, c, d, a, x[i + 3], 22, -1044525330);
|
|
26
|
+
a = MD5.ff(a, b, c, d, x[i + 4], 7, -176418897);
|
|
27
|
+
d = MD5.ff(d, a, b, c, x[i + 5], 12, 1200080426);
|
|
28
|
+
c = MD5.ff(c, d, a, b, x[i + 6], 17, -1473231341);
|
|
29
|
+
b = MD5.ff(b, c, d, a, x[i + 7], 22, -45705983);
|
|
30
|
+
a = MD5.ff(a, b, c, d, x[i + 8], 7, 1770035416);
|
|
31
|
+
d = MD5.ff(d, a, b, c, x[i + 9], 12, -1958414417);
|
|
32
|
+
c = MD5.ff(c, d, a, b, x[i + 10], 17, -42063);
|
|
33
|
+
b = MD5.ff(b, c, d, a, x[i + 11], 22, -1990404162);
|
|
34
|
+
a = MD5.ff(a, b, c, d, x[i + 12], 7, 1804603682);
|
|
35
|
+
d = MD5.ff(d, a, b, c, x[i + 13], 12, -40341101);
|
|
36
|
+
c = MD5.ff(c, d, a, b, x[i + 14], 17, -1502002290);
|
|
37
|
+
b = MD5.ff(b, c, d, a, x[i + 15], 22, 1236535329);
|
|
38
|
+
a = MD5.gg(a, b, c, d, x[i + 1], 5, -165796510);
|
|
39
|
+
d = MD5.gg(d, a, b, c, x[i + 6], 9, -1069501632);
|
|
40
|
+
c = MD5.gg(c, d, a, b, x[i + 11], 14, 643717713);
|
|
41
|
+
b = MD5.gg(b, c, d, a, x[i], 20, -373897302);
|
|
42
|
+
a = MD5.gg(a, b, c, d, x[i + 5], 5, -701558691);
|
|
43
|
+
d = MD5.gg(d, a, b, c, x[i + 10], 9, 38016083);
|
|
44
|
+
c = MD5.gg(c, d, a, b, x[i + 15], 14, -660478335);
|
|
45
|
+
b = MD5.gg(b, c, d, a, x[i + 4], 20, -405537848);
|
|
46
|
+
a = MD5.gg(a, b, c, d, x[i + 9], 5, 568446438);
|
|
47
|
+
d = MD5.gg(d, a, b, c, x[i + 14], 9, -1019803690);
|
|
48
|
+
c = MD5.gg(c, d, a, b, x[i + 3], 14, -187363961);
|
|
49
|
+
b = MD5.gg(b, c, d, a, x[i + 8], 20, 1163531501);
|
|
50
|
+
a = MD5.gg(a, b, c, d, x[i + 13], 5, -1444681467);
|
|
51
|
+
d = MD5.gg(d, a, b, c, x[i + 2], 9, -51403784);
|
|
52
|
+
c = MD5.gg(c, d, a, b, x[i + 7], 14, 1735328473);
|
|
53
|
+
b = MD5.gg(b, c, d, a, x[i + 12], 20, -1926607734);
|
|
54
|
+
a = MD5.hh(a, b, c, d, x[i + 5], 4, -378558);
|
|
55
|
+
d = MD5.hh(d, a, b, c, x[i + 8], 11, -2022574463);
|
|
56
|
+
c = MD5.hh(c, d, a, b, x[i + 11], 16, 1839030562);
|
|
57
|
+
b = MD5.hh(b, c, d, a, x[i + 14], 23, -35309556);
|
|
58
|
+
a = MD5.hh(a, b, c, d, x[i + 1], 4, -1530992060);
|
|
59
|
+
d = MD5.hh(d, a, b, c, x[i + 4], 11, 1272893353);
|
|
60
|
+
c = MD5.hh(c, d, a, b, x[i + 7], 16, -155497632);
|
|
61
|
+
b = MD5.hh(b, c, d, a, x[i + 10], 23, -1094730640);
|
|
62
|
+
a = MD5.hh(a, b, c, d, x[i + 13], 4, 681279174);
|
|
63
|
+
d = MD5.hh(d, a, b, c, x[i], 11, -358537222);
|
|
64
|
+
c = MD5.hh(c, d, a, b, x[i + 3], 16, -722521979);
|
|
65
|
+
b = MD5.hh(b, c, d, a, x[i + 6], 23, 76029189);
|
|
66
|
+
a = MD5.hh(a, b, c, d, x[i + 9], 4, -640364487);
|
|
67
|
+
d = MD5.hh(d, a, b, c, x[i + 12], 11, -421815835);
|
|
68
|
+
c = MD5.hh(c, d, a, b, x[i + 15], 16, 530742520);
|
|
69
|
+
b = MD5.hh(b, c, d, a, x[i + 2], 23, -995338651);
|
|
70
|
+
a = MD5.ii(a, b, c, d, x[i], 6, -198630844);
|
|
71
|
+
d = MD5.ii(d, a, b, c, x[i + 7], 10, 1126891415);
|
|
72
|
+
c = MD5.ii(c, d, a, b, x[i + 14], 15, -1416354905);
|
|
73
|
+
b = MD5.ii(b, c, d, a, x[i + 5], 21, -57434055);
|
|
74
|
+
a = MD5.ii(a, b, c, d, x[i + 12], 6, 1700485571);
|
|
75
|
+
d = MD5.ii(d, a, b, c, x[i + 3], 10, -1894986606);
|
|
76
|
+
c = MD5.ii(c, d, a, b, x[i + 10], 15, -1051523);
|
|
77
|
+
b = MD5.ii(b, c, d, a, x[i + 1], 21, -2054922799);
|
|
78
|
+
a = MD5.ii(a, b, c, d, x[i + 8], 6, 1873313359);
|
|
79
|
+
d = MD5.ii(d, a, b, c, x[i + 15], 10, -30611744);
|
|
80
|
+
c = MD5.ii(c, d, a, b, x[i + 6], 15, -1560198380);
|
|
81
|
+
b = MD5.ii(b, c, d, a, x[i + 13], 21, 1309151649);
|
|
82
|
+
a = MD5.ii(a, b, c, d, x[i + 4], 6, -145523070);
|
|
83
|
+
d = MD5.ii(d, a, b, c, x[i + 11], 10, -1120210379);
|
|
84
|
+
c = MD5.ii(c, d, a, b, x[i + 2], 15, 718787259);
|
|
85
|
+
b = MD5.ii(b, c, d, a, x[i + 9], 21, -343485551);
|
|
86
|
+
a = MD5.ad(a, lastA);
|
|
87
|
+
b = MD5.ad(b, lastB);
|
|
88
|
+
c = MD5.ad(c, lastC);
|
|
89
|
+
d = MD5.ad(d, lastD);
|
|
90
|
+
}
|
|
91
|
+
return MD5.rh(a) + MD5.rh(b) + MD5.rh(c) + MD5.rh(d);
|
|
92
|
+
}
|
|
93
|
+
static rh(n) {
|
|
94
|
+
let s = '';
|
|
95
|
+
for (let j = 0; j <= 3; j++) {
|
|
96
|
+
s += MD5.alphabet.charAt((n >> (j * 8 + 4)) & 0x0F) + MD5.alphabet.charAt((n >> (j * 8)) & 0x0F);
|
|
97
|
+
}
|
|
98
|
+
return s;
|
|
99
|
+
}
|
|
100
|
+
static ad(x, y) {
|
|
101
|
+
const l = (x & 0xFFFF) + (y & 0xFFFF);
|
|
102
|
+
const m = (x >> 16) + (y >> 16) + (l >> 16);
|
|
103
|
+
return (m << 16) | (l & 0xFFFF);
|
|
104
|
+
}
|
|
105
|
+
static rl(n, c) {
|
|
106
|
+
return (n << c) | (n >>> (32 - c));
|
|
107
|
+
}
|
|
108
|
+
static cm(q, a, b, x, s, t) {
|
|
109
|
+
return MD5.ad(MD5.rl(MD5.ad(MD5.ad(a, q), MD5.ad(x, t)), s), b);
|
|
110
|
+
}
|
|
111
|
+
static ff(a, b, c, d, x, s, t) {
|
|
112
|
+
return MD5.cm(b & c | ~b & d, a, b, x, s, t);
|
|
113
|
+
}
|
|
114
|
+
static gg(a, b, c, d, x, s, t) {
|
|
115
|
+
return MD5.cm(b & d | c & ~d, a, b, x, s, t);
|
|
116
|
+
}
|
|
117
|
+
static hh(a, b, c, d, x, s, t) {
|
|
118
|
+
return MD5.cm(b ^ c ^ d, a, b, x, s, t);
|
|
119
|
+
}
|
|
120
|
+
static ii(a, b, c, d, x, s, t) {
|
|
121
|
+
return MD5.cm(c ^ (b | ~d), a, b, x, s, t);
|
|
122
|
+
}
|
|
123
|
+
static sb(x) {
|
|
124
|
+
let i;
|
|
125
|
+
const numBlocks = ((x.length + 8) >> 6) + 1;
|
|
126
|
+
const blocks = new Array(numBlocks * 16);
|
|
127
|
+
for (i = 0; i < numBlocks * 16; i++) {
|
|
128
|
+
blocks[i] = 0;
|
|
129
|
+
}
|
|
130
|
+
for (i = 0; i < x.length; i++) {
|
|
131
|
+
blocks[i >> 2] |= x.charCodeAt(i) << ((i % 4) * 8);
|
|
132
|
+
}
|
|
133
|
+
blocks[i >> 2] |= 0x80 << ((i % 4) * 8);
|
|
134
|
+
blocks[numBlocks * 16 - 2] = x.length * 8;
|
|
135
|
+
return blocks;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
MD5.alphabet = '0123456789abcdef';
|
|
139
|
+
function generateRandom(length = 6) {
|
|
140
|
+
let result = '';
|
|
141
|
+
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
142
|
+
const charactersLength = characters.length;
|
|
143
|
+
let counter = 0;
|
|
144
|
+
while (counter < length) {
|
|
145
|
+
result += characters.charAt(Math.floor(Math.random() * charactersLength));
|
|
146
|
+
counter += 1;
|
|
147
|
+
}
|
|
148
|
+
return result;
|
|
149
|
+
}
|
|
150
|
+
function extractFormMeta(form) {
|
|
151
|
+
function appendData(meta, key, val) {
|
|
152
|
+
let fchar = key.slice(0, 1);
|
|
153
|
+
let lchar = key.slice(-1);
|
|
154
|
+
if (fchar === '.') {
|
|
155
|
+
key = key.slice(1);
|
|
156
|
+
}
|
|
157
|
+
if (lchar === '.') {
|
|
158
|
+
key = key.slice(0, -1);
|
|
159
|
+
}
|
|
160
|
+
if (key.includes('.')) {
|
|
161
|
+
let nestKey = key.split('.');
|
|
162
|
+
key = nestKey.pop();
|
|
163
|
+
for (let k of nestKey) {
|
|
164
|
+
if (!k) {
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
if (!meta.hasOwnProperty(k)) {
|
|
168
|
+
meta[k] = {};
|
|
169
|
+
}
|
|
170
|
+
meta = meta[k];
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
if (meta.hasOwnProperty(key)) {
|
|
174
|
+
if (Array.isArray(meta[key])) {
|
|
175
|
+
meta[key].push(val);
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
meta[key] = [meta[key], val];
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
meta[key] = val;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
if (form instanceof FormData) {
|
|
186
|
+
let meta = {};
|
|
187
|
+
let totalFileSize = 0;
|
|
188
|
+
let files = [];
|
|
189
|
+
for (let pair of form.entries()) {
|
|
190
|
+
let name = pair[0];
|
|
191
|
+
let v = pair[1];
|
|
192
|
+
if (v instanceof File) {
|
|
193
|
+
if (!files.includes(name)) {
|
|
194
|
+
files.push(name);
|
|
195
|
+
}
|
|
196
|
+
totalFileSize += v.size;
|
|
197
|
+
}
|
|
198
|
+
else if (v instanceof FileList) {
|
|
199
|
+
if (!files.includes(name)) {
|
|
200
|
+
files.push(name);
|
|
201
|
+
}
|
|
202
|
+
if (v && v.length > 0) {
|
|
203
|
+
for (let idx = 0; idx <= v.length - 1; idx++) {
|
|
204
|
+
totalFileSize += v.item(idx).size;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
appendData(meta, name, v);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
if (totalFileSize > 4400000) {
|
|
213
|
+
throw new SkapiError('Total File size cannot exceed 4MB. Use skapi.uploadFiles() instead.', { code: 'INVALID_REQUEST' });
|
|
214
|
+
}
|
|
215
|
+
return { meta, files };
|
|
216
|
+
}
|
|
217
|
+
if (form instanceof SubmitEvent) {
|
|
218
|
+
form = form.target;
|
|
219
|
+
}
|
|
220
|
+
if (form instanceof HTMLFormElement) {
|
|
221
|
+
let meta = {};
|
|
222
|
+
let files = [];
|
|
223
|
+
let totalFileSize = 0;
|
|
224
|
+
let inputs = form.querySelectorAll('input');
|
|
225
|
+
let textarea = form.querySelectorAll('textarea');
|
|
226
|
+
for (let idx = 0; idx < textarea.length; idx++) {
|
|
227
|
+
let i = textarea[idx];
|
|
228
|
+
if (i.name) {
|
|
229
|
+
appendData(meta, i.name, i.value);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
for (let idx = 0; idx < inputs.length; idx++) {
|
|
233
|
+
let i = inputs[idx];
|
|
234
|
+
if (i.name) {
|
|
235
|
+
if (i.type === 'number') {
|
|
236
|
+
if (i.value) {
|
|
237
|
+
appendData(meta, i.name, Number(i.value));
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
else if (i.type === 'checkbox' || i.type === 'radio') {
|
|
241
|
+
if (i.checked) {
|
|
242
|
+
if (i.value === '' && i.type === 'checkbox' || i.value === 'on' || i.value === 'true') {
|
|
243
|
+
appendData(meta, i.name, true);
|
|
244
|
+
}
|
|
245
|
+
else if (i.value === 'false') {
|
|
246
|
+
appendData(meta, i.name, false);
|
|
247
|
+
}
|
|
248
|
+
else if (i.value) {
|
|
249
|
+
appendData(meta, i.name, i.value);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
else if (i.type === 'checkbox') {
|
|
253
|
+
if (i.value === '' || i.value === 'on' || i.value === 'true') {
|
|
254
|
+
appendData(meta, i.name, false);
|
|
255
|
+
}
|
|
256
|
+
else if (i.value === 'false') {
|
|
257
|
+
appendData(meta, i.name, true);
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
appendData(meta, i.name, undefined);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
else if (i.type === 'file') {
|
|
265
|
+
if (!files.includes(i.name)) {
|
|
266
|
+
files.push(i.name);
|
|
267
|
+
}
|
|
268
|
+
if (i.files && i.files.length > 0) {
|
|
269
|
+
for (let idx = 0; idx <= i.files.length - 1; idx++) {
|
|
270
|
+
totalFileSize += i.files.item(idx).size;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
else {
|
|
275
|
+
appendData(meta, i.name, i.value);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
if (totalFileSize > 4400000) {
|
|
280
|
+
throw new SkapiError('Total File size cannot exceed 4MB. Use skapi.uploadFiles() instead.', { code: 'INVALID_REQUEST' });
|
|
281
|
+
}
|
|
282
|
+
return { meta, files };
|
|
283
|
+
}
|
|
284
|
+
return null;
|
|
285
|
+
}
|
|
286
|
+
export { extractFormMeta, MD5, generateRandom };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
declare function UserId(id: string, param?: string): string;
|
|
2
|
+
declare function PhoneNumber(value: string): string;
|
|
3
|
+
declare function Birthdate(birthdate: string): string;
|
|
4
|
+
declare function Password(password: string): string;
|
|
5
|
+
declare function Email(email: string, paramName?: string): string;
|
|
6
|
+
declare function Url(url: string | string[]): any;
|
|
7
|
+
declare function specialChars(string: string | string[], p?: string, allowPeriods?: boolean, allowWhiteSpace?: boolean): string | string[];
|
|
8
|
+
declare function Params(params: any, struct: Record<string, any>, required?: string[] | null, bypassCheck?: string[] | null, _parentKey?: string | null): any;
|
|
9
|
+
declare const _default: {
|
|
10
|
+
UserId: typeof UserId;
|
|
11
|
+
PhoneNumber: typeof PhoneNumber;
|
|
12
|
+
Birthdate: typeof Birthdate;
|
|
13
|
+
Password: typeof Password;
|
|
14
|
+
Email: typeof Email;
|
|
15
|
+
Url: typeof Url;
|
|
16
|
+
specialChars: typeof specialChars;
|
|
17
|
+
Params: typeof Params;
|
|
18
|
+
};
|
|
19
|
+
export default _default;
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import SkapiError from '../main/error';
|
|
2
|
+
import { extractFormMeta } from './utils';
|
|
3
|
+
function UserId(id, param = 'User ID') {
|
|
4
|
+
let uuid_regex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
5
|
+
if (!id) {
|
|
6
|
+
throw new SkapiError(`${param} is empty.`, { code: 'INVALID_PARAMETER' });
|
|
7
|
+
}
|
|
8
|
+
else if (typeof id !== 'string') {
|
|
9
|
+
throw new SkapiError(`${param} should be type: string.`, { code: 'INVALID_PARAMETER' });
|
|
10
|
+
}
|
|
11
|
+
else if (!id.match(uuid_regex)) {
|
|
12
|
+
throw new SkapiError(`${param} is invalid.`, { code: 'INVALID_PARAMETER' });
|
|
13
|
+
}
|
|
14
|
+
return id;
|
|
15
|
+
}
|
|
16
|
+
function PhoneNumber(value) {
|
|
17
|
+
if (value) {
|
|
18
|
+
if (typeof value !== 'string' || value.charAt(0) !== '+' || isNaN(Number(value.substring(1)))) {
|
|
19
|
+
throw new SkapiError('"phone_number" is invalid. The format should be "+00123456789". Type: string.', { code: 'INVALID_PARAMETER' });
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return value || '';
|
|
23
|
+
}
|
|
24
|
+
function Birthdate(birthdate) {
|
|
25
|
+
if (birthdate) {
|
|
26
|
+
if (typeof birthdate !== 'string') {
|
|
27
|
+
throw new SkapiError('"birthdate" is invalid. The format should be "yyyy-mm-dd". Type: string.', { code: 'INVALID_PARAMETER' });
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
let date_regex = new RegExp(/([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/);
|
|
31
|
+
if (birthdate.length !== 10 || birthdate.split('-').length !== 3 || !date_regex.test(birthdate)) {
|
|
32
|
+
throw new SkapiError('"birthdate" is invalid. The format should be "yyyy-mm-dd". Type: string.', { code: 'INVALID_PARAMETER' });
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return birthdate || '';
|
|
37
|
+
}
|
|
38
|
+
function Password(password) {
|
|
39
|
+
if (!password) {
|
|
40
|
+
throw new SkapiError('"password" is required.', { code: 'INVALID_PARAMETER' });
|
|
41
|
+
}
|
|
42
|
+
else if (typeof password !== 'string') {
|
|
43
|
+
throw new SkapiError('"password" should be type: string.', { code: 'INVALID_PARAMETER' });
|
|
44
|
+
}
|
|
45
|
+
else if (password.length < 6) {
|
|
46
|
+
throw new SkapiError('"password" should be at least 6 characters.', { code: 'INVALID_PARAMETER' });
|
|
47
|
+
}
|
|
48
|
+
else if (password.length > 60) {
|
|
49
|
+
throw new SkapiError('"password" can be up to 60 characters max.', { code: 'INVALID_PARAMETER' });
|
|
50
|
+
}
|
|
51
|
+
return password;
|
|
52
|
+
}
|
|
53
|
+
function Email(email, paramName = 'email') {
|
|
54
|
+
if (!email) {
|
|
55
|
+
throw new SkapiError(`"${paramName}" is required.`, { code: 'INVALID_PARAMETER' });
|
|
56
|
+
}
|
|
57
|
+
else if (typeof email !== 'string') {
|
|
58
|
+
throw new SkapiError(`"${paramName}"should be type: string.`, { code: 'INVALID_PARAMETER' });
|
|
59
|
+
}
|
|
60
|
+
else if (email.length < 5) {
|
|
61
|
+
throw new SkapiError(`"${paramName}" should be at least 5 characters.`, { code: 'INVALID_PARAMETER' });
|
|
62
|
+
}
|
|
63
|
+
else if (/^[a-zA-Z0-9.!#$%&'+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z]+[a-zA-Z0-9-]+)$/.test(email)) {
|
|
64
|
+
email = email.trim();
|
|
65
|
+
let splitAt = email.split('@');
|
|
66
|
+
let tld = splitAt[1].split('.');
|
|
67
|
+
if (tld.length >= 2) {
|
|
68
|
+
return email.toLowerCase();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
throw new SkapiError(`"${email}" is an invalid email.`, { code: 'INVALID_PARAMETER' });
|
|
72
|
+
}
|
|
73
|
+
function Url(url) {
|
|
74
|
+
const baseUrl = (() => {
|
|
75
|
+
let baseUrl = window.location.origin || null;
|
|
76
|
+
if (baseUrl && baseUrl.slice(-1) === '/') {
|
|
77
|
+
baseUrl = baseUrl.slice(0, -1);
|
|
78
|
+
}
|
|
79
|
+
return baseUrl;
|
|
80
|
+
})();
|
|
81
|
+
let check = (c) => {
|
|
82
|
+
if (typeof c === 'string') {
|
|
83
|
+
if (c === '*') {
|
|
84
|
+
return '*';
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
let cu = c.trim();
|
|
88
|
+
if (!cu.includes(' ') && !cu.includes(',')) {
|
|
89
|
+
if (cu.slice(0, 1) === '/' && baseUrl) {
|
|
90
|
+
cu = baseUrl + cu;
|
|
91
|
+
}
|
|
92
|
+
else if (cu.slice(0, 1) === '.' && baseUrl) {
|
|
93
|
+
cu = window.location.href.split('/').slice(0, -1).join('/') + cu;
|
|
94
|
+
}
|
|
95
|
+
let _url;
|
|
96
|
+
try {
|
|
97
|
+
_url = new URL(cu);
|
|
98
|
+
}
|
|
99
|
+
catch (err) {
|
|
100
|
+
throw new SkapiError(`"${c}" is an invalid url.`, { code: 'INVALID_PARAMETER' });
|
|
101
|
+
}
|
|
102
|
+
if (_url.protocol) {
|
|
103
|
+
let url = _url.href;
|
|
104
|
+
if (url.charAt(url.length - 1) === '/')
|
|
105
|
+
url = url.substring(0, url.length - 1);
|
|
106
|
+
return url;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
throw new SkapiError(`"${c}" is an invalid url.`, { code: 'INVALID_PARAMETER' });
|
|
112
|
+
};
|
|
113
|
+
if (Array.isArray(url)) {
|
|
114
|
+
return url.map(u => check(u));
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
return check(url);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
function specialChars(string, p = 'parameter', allowPeriods = false, allowWhiteSpace = false) {
|
|
121
|
+
let checkStr = (s) => {
|
|
122
|
+
if (typeof s !== 'string') {
|
|
123
|
+
throw new SkapiError(`${p} should be type: <string | string[]>.`, { code: 'INVALID_PARAMETER' });
|
|
124
|
+
}
|
|
125
|
+
if (!allowWhiteSpace && string.includes(' ')) {
|
|
126
|
+
throw new SkapiError(`${p} should not have whitespace.`, { code: 'INVALID_PARAMETER' });
|
|
127
|
+
}
|
|
128
|
+
if (!allowPeriods && string.includes('.')) {
|
|
129
|
+
throw new SkapiError(`${p} should not have periods.`, { code: 'INVALID_PARAMETER' });
|
|
130
|
+
}
|
|
131
|
+
if (/[`!@#$%^&*()_+\-=\[\]{};':"\\|,<>\/?~]/.test(s)) {
|
|
132
|
+
throw new SkapiError(`${p} should not have special characters.`, { code: 'INVALID_PARAMETER' });
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
if (Array.isArray(string)) {
|
|
136
|
+
for (let s of string) {
|
|
137
|
+
checkStr(s);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
checkStr(string);
|
|
142
|
+
}
|
|
143
|
+
return string;
|
|
144
|
+
}
|
|
145
|
+
function Params(params, struct, required = null, bypassCheck = [], _parentKey = null) {
|
|
146
|
+
if (Array.isArray(bypassCheck)) {
|
|
147
|
+
bypassCheck = bypassCheck.concat([
|
|
148
|
+
'service',
|
|
149
|
+
'owner',
|
|
150
|
+
]);
|
|
151
|
+
}
|
|
152
|
+
function isObjectWithKeys(obj) {
|
|
153
|
+
if (obj instanceof Promise) {
|
|
154
|
+
throw new SkapiError('Parameter should not be a promise', { code: 'INVALID_PARAMETER' });
|
|
155
|
+
}
|
|
156
|
+
return obj && typeof obj === 'object' && !Array.isArray(obj) && Object.keys(obj).length;
|
|
157
|
+
}
|
|
158
|
+
function isEmptyObject(obj) {
|
|
159
|
+
return obj && typeof obj === 'object' && !Array.isArray(obj) && !Object.keys(obj).length;
|
|
160
|
+
}
|
|
161
|
+
let _params = params;
|
|
162
|
+
let val;
|
|
163
|
+
let errToThrow = null;
|
|
164
|
+
let isInvalid = _parentKey ? ` in "${_parentKey}" is invalid.` : '. Parameter should be type <object>.';
|
|
165
|
+
if (_parentKey === null) {
|
|
166
|
+
if (isObjectWithKeys(_params)) {
|
|
167
|
+
if (_params instanceof HTMLFormElement || _params instanceof FormData || _params instanceof SubmitEvent) {
|
|
168
|
+
_params = extractFormMeta(params)?.meta;
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
_params = JSON.parse(JSON.stringify(params));
|
|
172
|
+
}
|
|
173
|
+
for (let k in _params) {
|
|
174
|
+
if (!struct.hasOwnProperty(k) && Array.isArray(bypassCheck) && !bypassCheck.includes(k)) {
|
|
175
|
+
throw new SkapiError(`Key name "${k}" is invalid in parameter.`, { code: 'INVALID_PARAMETER' });
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
if (Array.isArray(required) && required.length) {
|
|
179
|
+
for (let k of required) {
|
|
180
|
+
if (!Object.keys(_params).includes(k)) {
|
|
181
|
+
throw new SkapiError(`Key "${k}" is required in parameter.`, { code: 'INVALID_PARAMETER' });
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
else if (isEmptyObject(_params) || typeof _params === 'undefined') {
|
|
187
|
+
let defaults = {};
|
|
188
|
+
for (let s in struct) {
|
|
189
|
+
let structValue = struct[s];
|
|
190
|
+
if (Array.isArray(structValue) && typeof structValue[structValue.length - 1] === 'function')
|
|
191
|
+
defaults[s] = structValue[structValue.length - 1]();
|
|
192
|
+
}
|
|
193
|
+
return Object.keys(defaults).length ? defaults : _params;
|
|
194
|
+
}
|
|
195
|
+
if (_params === null) {
|
|
196
|
+
return null;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
if (isObjectWithKeys(struct) && isObjectWithKeys(_params)) {
|
|
200
|
+
for (let s in struct) {
|
|
201
|
+
let structValue = struct[s];
|
|
202
|
+
if (_params.hasOwnProperty(s) && _params[s] === null) {
|
|
203
|
+
_params[s] = null;
|
|
204
|
+
}
|
|
205
|
+
else if (_params.hasOwnProperty(s) && typeof _params[s] !== 'undefined') {
|
|
206
|
+
_params[s] = Params(_params[s], structValue, null, null, s);
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
let defaultSetter = Array.isArray(structValue) &&
|
|
210
|
+
typeof structValue[structValue.length - 1] === 'function' ? structValue[structValue.length - 1] : null;
|
|
211
|
+
if (defaultSetter) {
|
|
212
|
+
let def = defaultSetter();
|
|
213
|
+
if (def !== undefined) {
|
|
214
|
+
_params[s] = def;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
val = _params;
|
|
220
|
+
}
|
|
221
|
+
else if (Array.isArray(struct)) {
|
|
222
|
+
for (let s of struct) {
|
|
223
|
+
try {
|
|
224
|
+
if (typeof _params !== undefined && typeof s !== 'function') {
|
|
225
|
+
val = Params(_params, s, null, null, _parentKey);
|
|
226
|
+
}
|
|
227
|
+
break;
|
|
228
|
+
}
|
|
229
|
+
catch (err) {
|
|
230
|
+
if (typeof err === 'string' && err.substring(0, 6) === 'BREAK:') {
|
|
231
|
+
err = err.substring(6);
|
|
232
|
+
let errMsg = err.split(':');
|
|
233
|
+
errToThrow = new SkapiError(errMsg[1], { code: errMsg[0] });
|
|
234
|
+
break;
|
|
235
|
+
}
|
|
236
|
+
else {
|
|
237
|
+
errToThrow = err;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
else if (typeof struct === 'function') {
|
|
243
|
+
return struct(_params);
|
|
244
|
+
}
|
|
245
|
+
else if (typeof struct === 'string') {
|
|
246
|
+
if (Array.isArray(_params)) {
|
|
247
|
+
if (struct !== 'array') {
|
|
248
|
+
throw new SkapiError(`Invalid type "${typeof _params}"${isInvalid}`, { code: 'INVALID_PARAMETER' });
|
|
249
|
+
}
|
|
250
|
+
for (let p of _params) {
|
|
251
|
+
if (!['number', 'string', 'boolean'].includes(typeof p) && p !== null) {
|
|
252
|
+
throw new SkapiError(`Invalid type "${typeof p}" in "${_parentKey}" array value.`, { code: 'INVALID_PARAMETER' });
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
val = _params;
|
|
256
|
+
}
|
|
257
|
+
else if (!['number', 'string', 'boolean', 'array', 'function'].includes(struct)) {
|
|
258
|
+
if (_params === struct) {
|
|
259
|
+
val = _params;
|
|
260
|
+
}
|
|
261
|
+
else {
|
|
262
|
+
throw new SkapiError(`Value: ${_params}${isInvalid}`, { code: 'INVALID_PARAMETER' });
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
else if (typeof _params === struct) {
|
|
266
|
+
if (struct === 'number') {
|
|
267
|
+
if (Math.abs(_params) > 4503599627370496) {
|
|
268
|
+
throw `BREAK:INVALID_PARAMETER:"${_parentKey}" integer value should be within -4503599627370496 ~ +4503599627370546.`;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
val = _params;
|
|
272
|
+
}
|
|
273
|
+
else {
|
|
274
|
+
throw new SkapiError(`Value: ${_params}${isInvalid}`, { code: 'INVALID_PARAMETER' });
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
else if (struct === null) {
|
|
278
|
+
val = _params;
|
|
279
|
+
}
|
|
280
|
+
if (val === undefined && errToThrow) {
|
|
281
|
+
throw errToThrow;
|
|
282
|
+
}
|
|
283
|
+
return val;
|
|
284
|
+
}
|
|
285
|
+
export default {
|
|
286
|
+
UserId,
|
|
287
|
+
PhoneNumber,
|
|
288
|
+
Birthdate,
|
|
289
|
+
Password,
|
|
290
|
+
Email,
|
|
291
|
+
Url,
|
|
292
|
+
specialChars,
|
|
293
|
+
Params
|
|
294
|
+
};
|
package/package.json
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "skapi-js",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"description": "Javascript library for Skapi: Complete JAM Stack, front-end driven serverless backend API service.",
|
|
5
5
|
"main": "./dist/skapi.module.js",
|
|
6
|
+
"types": "./js/Main.d.ts",
|
|
6
7
|
"files": [
|
|
8
|
+
"js/",
|
|
7
9
|
"dist/"
|
|
8
10
|
],
|
|
9
11
|
"scripts": {
|
|
10
|
-
"build": "npx tsc --project tsconfig.
|
|
12
|
+
"build": "npx tsc --project tsconfig.json; npx webpack --config webpack.config.js",
|
|
11
13
|
"server": "(cd server; node server.js)"
|
|
12
14
|
},
|
|
13
15
|
"repository": {
|
|
@@ -33,6 +35,6 @@
|
|
|
33
35
|
"jamstack"
|
|
34
36
|
],
|
|
35
37
|
"dependencies": {
|
|
36
|
-
"amazon-cognito-identity-js": "^5.2.
|
|
38
|
+
"amazon-cognito-identity-js": "^5.2.14"
|
|
37
39
|
}
|
|
38
40
|
}
|