@samet-it/be-base-common 1.0.1

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.
Files changed (46) hide show
  1. package/LICENSE +24 -0
  2. package/README.md +106 -0
  3. package/dist/assets/.gitkeep +0 -0
  4. package/dist/config/base-common.config.d.ts +2 -0
  5. package/dist/config/base-common.config.js +9 -0
  6. package/dist/config/index.d.ts +2 -0
  7. package/dist/config/index.js +18 -0
  8. package/dist/config/index.types.d.ts +12 -0
  9. package/dist/config/index.types.js +2 -0
  10. package/dist/error/base.exception.d.ts +7 -0
  11. package/dist/error/base.exception.js +11 -0
  12. package/dist/error/error.handler.d.ts +2 -0
  13. package/dist/error/error.handler.js +217 -0
  14. package/dist/error/index.d.ts +8 -0
  15. package/dist/error/index.js +24 -0
  16. package/dist/error/index.types.d.ts +31 -0
  17. package/dist/error/index.types.js +2 -0
  18. package/dist/error/initialization.exception.d.ts +4 -0
  19. package/dist/error/initialization.exception.js +11 -0
  20. package/dist/error/page-not-found.exception.d.ts +4 -0
  21. package/dist/error/page-not-found.exception.js +10 -0
  22. package/dist/error/record-duplicated.exception.d.ts +4 -0
  23. package/dist/error/record-duplicated.exception.js +18 -0
  24. package/dist/error/record-not-found.exception.d.ts +4 -0
  25. package/dist/error/record-not-found.exception.js +13 -0
  26. package/dist/error/validation.exception.d.ts +4 -0
  27. package/dist/error/validation.exception.js +23 -0
  28. package/dist/exit/exit-handler.impl.d.ts +2 -0
  29. package/dist/exit/exit-handler.impl.js +13 -0
  30. package/dist/exit/index.d.ts +2 -0
  31. package/dist/exit/index.js +18 -0
  32. package/dist/exit/index.types.d.ts +4 -0
  33. package/dist/exit/index.types.js +2 -0
  34. package/dist/generator/generator.d.ts +2 -0
  35. package/dist/generator/generator.js +417 -0
  36. package/dist/generator/index.d.ts +2 -0
  37. package/dist/generator/index.js +18 -0
  38. package/dist/generator/index.types.d.ts +616 -0
  39. package/dist/generator/index.types.js +2 -0
  40. package/dist/index.d.ts +5 -0
  41. package/dist/index.js +21 -0
  42. package/dist/shared/index.d.ts +1 -0
  43. package/dist/shared/index.js +17 -0
  44. package/dist/shared/index.types.d.ts +11 -0
  45. package/dist/shared/index.types.js +2 -0
  46. package/package.json +70 -0
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ import { GeneratorLike } from "./index.types";
2
+ export declare const generator: GeneratorLike;
@@ -0,0 +1,417 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generator = void 0;
4
+ const node_crypto_1 = require("node:crypto");
5
+ const NUMERIC = '0123456789';
6
+ const UPPER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
7
+ const LOWER = 'abcdefghijklmnopqrstuvwxyz';
8
+ const ALPHABET = NUMERIC + UPPER + LOWER;
9
+ const SECONDS = 1000;
10
+ const MINUTES = SECONDS * 60;
11
+ const HOURS = MINUTES * 60;
12
+ const DAYS = HOURS * 24;
13
+ const MONTHS = DAYS * 12;
14
+ const YEARS = DAYS * 365;
15
+ const LOREM = [
16
+ 'lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur',
17
+ 'adipiscing', 'elit', 'curabitur', 'vel', 'hendrerit', 'libero',
18
+ 'eleifend', 'blandit', 'nunc', 'ornare', 'odio', 'ut',
19
+ 'orci', 'gravida', 'imperdiet', 'nullam', 'purus', 'lacinia',
20
+ 'a', 'pretium', 'quis', 'congue', 'praesent', 'sagittis',
21
+ 'laoreet', 'auctor', 'mauris', 'non', 'velit', 'eros',
22
+ 'dictum', 'proin', 'accumsan', 'sapien', 'nec', 'massa',
23
+ 'volutpat', 'venenatis', 'sed', 'eu', 'molestie', 'lacus',
24
+ 'quisque', 'porttitor', 'ligula', 'dui', 'mollis', 'tempus',
25
+ 'at', 'magna', 'vestibulum', 'turpis', 'ac', 'diam',
26
+ 'tincidunt', 'id', 'condimentum', 'enim', 'sodales', 'in',
27
+ 'hac', 'habitasse', 'platea', 'dictumst', 'aenean', 'neque',
28
+ 'fusce', 'augue', 'leo', 'eget', 'semper', 'mattis',
29
+ 'tortor', 'scelerisque', 'nulla', 'interdum', 'tellus', 'malesuada',
30
+ 'rhoncus', 'porta', 'sem', 'aliquet', 'et', 'nam',
31
+ 'suspendisse', 'potenti', 'vivamus', 'luctus', 'fringilla', 'erat',
32
+ 'donec', 'justo', 'vehicula', 'ultricies', 'varius', 'ante',
33
+ 'primis', 'faucibus', 'ultrices', 'posuere', 'cubilia', 'curae',
34
+ 'etiam', 'cursus', 'aliquam', 'quam', 'dapibus', 'nisl',
35
+ 'feugiat', 'egestas', 'class', 'aptent', 'taciti', 'sociosqu',
36
+ 'ad', 'litora', 'torquent', 'per', 'conubia', 'nostra',
37
+ 'inceptos', 'himenaeos', 'phasellus', 'nibh', 'pulvinar', 'vitae',
38
+ 'urna', 'iaculis', 'lobortis', 'nisi', 'viverra', 'arcu',
39
+ 'morbi', 'pellentesque', 'metus', 'commodo', 'ut', 'facilisis',
40
+ 'felis', 'tristique', 'ullamcorper', 'placerat', 'aenean', 'convallis',
41
+ 'sollicitudin', 'integer', 'rutrum', 'duis', 'est', 'etiam',
42
+ 'bibendum', 'donec', 'pharetra', 'vulputate', 'maecenas', 'mi',
43
+ 'fermentum', 'consequat', 'suscipit', 'aliquam', 'habitant', 'senectus',
44
+ 'netus', 'fames', 'quisque', 'euismod', 'curabitur', 'lectus',
45
+ 'elementum', 'tempor', 'risus', 'cras'
46
+ ];
47
+ class Generator {
48
+ constructor() {
49
+ this._start = new Date().getTime();
50
+ }
51
+ /**
52
+ * Checks number value in a range
53
+ *
54
+ * @param {number} value - value should be verified
55
+ * @param {number} min - min limit
56
+ * @param {number} max - max limit
57
+ * @param {number} def - default limit
58
+ * */
59
+ _range(value, min, max, def) {
60
+ if (typeof value !== "number") {
61
+ return def !== null && def !== void 0 ? def : min;
62
+ }
63
+ if (value < min) {
64
+ return min;
65
+ }
66
+ if (value > max) {
67
+ return max;
68
+ }
69
+ return value;
70
+ }
71
+ /**
72
+ * Shuffles an array
73
+ *
74
+ * @param {Array} list - array
75
+ * */
76
+ _shuffle(list) {
77
+ const items = [...list];
78
+ for (let i = items.length - 1; i >= 0; i--) {
79
+ const j = Math.floor(Math.random() * (i + 1));
80
+ [items[i], items[j]] = [items[j], items[i]];
81
+ }
82
+ return items;
83
+ }
84
+ /**
85
+ * Handles min max
86
+ *
87
+ * @param {number} p1 - min or max
88
+ * @param {number} p2 - max
89
+ * @returns {Array} min max tuple
90
+ * */
91
+ _minMax(p1, p2) {
92
+ let min;
93
+ let max;
94
+ if (typeof p2 === 'number') {
95
+ max = p2;
96
+ if (typeof p1 === 'number') {
97
+ min = p1;
98
+ }
99
+ }
100
+ else if (typeof p1 === 'number') {
101
+ max = p1;
102
+ }
103
+ if (min === undefined) {
104
+ if (max === undefined) {
105
+ min = 0;
106
+ max = 1000;
107
+ }
108
+ else {
109
+ min = max - 1000;
110
+ }
111
+ }
112
+ else if (max === undefined) {
113
+ max = min + 1000;
114
+ }
115
+ if (min > max) {
116
+ const swap = min;
117
+ min = max;
118
+ max = swap;
119
+ }
120
+ if (min === max) {
121
+ max = min + 1;
122
+ }
123
+ return [min, max];
124
+ }
125
+ /**
126
+ * Checks size
127
+ *
128
+ * @param {(number|boolean)} size - size of array, if it's true, use randomize
129
+ * */
130
+ _checkSize(size) {
131
+ if (size === true) {
132
+ return 1 + Math.floor(Math.random() * 5);
133
+ }
134
+ return Math.floor(this._range(size, 1, 5, 3));
135
+ }
136
+ /**
137
+ * Helper to produce an array of enum values.
138
+ * @param enumeration Enumeration object.
139
+ */
140
+ _enumToArray(enumeration) {
141
+ return Object.keys(enumeration)
142
+ .filter(key => isNaN(Number(key)))
143
+ .map(key => enumeration[key])
144
+ .filter(val => typeof val === "number" || typeof val === "string");
145
+ }
146
+ /** {@inheritDoc} */
147
+ buildConstructor(instance, given) {
148
+ if (given && typeof given === 'object' && !Array.isArray(given)) {
149
+ for (const [k, v] of Object.entries(given)) {
150
+ instance[k] = v;
151
+ }
152
+ }
153
+ }
154
+ /** {@inheritDoc} */
155
+ forLength(size, fn) {
156
+ size = this._checkSize(size);
157
+ const items = [];
158
+ for (let i = 0; i < size; i++) {
159
+ items.push(fn());
160
+ }
161
+ return items;
162
+ }
163
+ /** {@inheritDoc} */
164
+ word(p1, p2) {
165
+ let length;
166
+ let alphabet;
167
+ if (typeof p1 === 'number') {
168
+ length = p1;
169
+ if (typeof p2 === 'string') {
170
+ alphabet = p2.trim();
171
+ }
172
+ }
173
+ else if (typeof p1 === 'string') {
174
+ alphabet = p1.trim();
175
+ }
176
+ if (!alphabet) {
177
+ alphabet = ALPHABET;
178
+ }
179
+ length = this._range(length, 1, 100, 5);
180
+ let result = '';
181
+ const charactersLength = alphabet.length;
182
+ let counter = 0;
183
+ while (counter < length) {
184
+ result += alphabet.charAt(Math.floor(Math.random() * charactersLength));
185
+ counter += 1;
186
+ }
187
+ return result;
188
+ }
189
+ /** {@inheritDoc} */
190
+ wordArray(size, p1, p2) {
191
+ return this.forLength(size, () => this.word(p1, p2));
192
+ }
193
+ /** {@inheritDoc} */
194
+ numeric(length) {
195
+ return this.word(length, NUMERIC);
196
+ }
197
+ /** {@inheritDoc} */
198
+ numericArray(size, length) {
199
+ return this.forLength(size, () => this.numeric(length));
200
+ }
201
+ /** {@inheritDoc} */
202
+ firstUpper(length) {
203
+ return this.word(1, UPPER) + this.word(length, LOWER);
204
+ }
205
+ /** {@inheritDoc} */
206
+ firstUpperArray(size, length) {
207
+ return this.forLength(size, () => this.firstUpper(length));
208
+ }
209
+ /** {@inheritDoc} */
210
+ upper(length) {
211
+ return this.word(length, UPPER);
212
+ }
213
+ /** {@inheritDoc} */
214
+ upperArray(size, length) {
215
+ return this.forLength(size, () => this.upper(length));
216
+ }
217
+ /** {@inheritDoc} */
218
+ lower(length) {
219
+ return this.word(length, LOWER);
220
+ }
221
+ /** {@inheritDoc} */
222
+ lowerArray(size, length) {
223
+ return this.forLength(size, () => this.lower(length));
224
+ }
225
+ /** {@inheritDoc} */
226
+ loremIpsum(count) {
227
+ const items = this._shuffle(LOREM);
228
+ count = this._range(count, 1, items.length);
229
+ return items.slice(0, count).join(' ');
230
+ }
231
+ /** {@inheritDoc} */
232
+ loremIpsumArray(size, count) {
233
+ return this.forLength(size, () => this.loremIpsum(count));
234
+ }
235
+ /** {@inheritDoc} */
236
+ integer(p1, p2) {
237
+ let [min, max] = this._minMax(p1, p2);
238
+ min = Math.ceil(min);
239
+ max = Math.ceil(max);
240
+ return min + Math.floor(Math.random() * (max - min + 1));
241
+ }
242
+ /** {@inheritDoc} */
243
+ integerArray(size, p1, p2) {
244
+ return this.forLength(size, () => this.integer(p1, p2));
245
+ }
246
+ /** {@inheritDoc} */
247
+ double(p1, p2) {
248
+ let [min, max] = this._minMax(p1, p2);
249
+ return min + (Math.random() * (max - min));
250
+ }
251
+ /** {@inheritDoc} */
252
+ doubleArray(size, p1, p2) {
253
+ return this.forLength(size, () => this.double(p1, p2));
254
+ }
255
+ /** {@inheritDoc} */
256
+ uuid() {
257
+ return (0, node_crypto_1.randomUUID)();
258
+ }
259
+ /** {@inheritDoc} */
260
+ uuidArray(size) {
261
+ return this.forLength(size, () => this.uuid());
262
+ }
263
+ /** {@inheritDoc} */
264
+ boolean() {
265
+ return Math.random() >= 0.5;
266
+ }
267
+ /** {@inheritDoc} */
268
+ booleanArray(size) {
269
+ return this.forLength(size, () => this.boolean());
270
+ }
271
+ /** {@inheritDoc} */
272
+ timestamp(type, value, pastFuture) {
273
+ let diff;
274
+ value = this._range(value, 1, 1000, 10);
275
+ switch (type) {
276
+ case "year":
277
+ case "y":
278
+ diff = YEARS * value;
279
+ break;
280
+ case "month":
281
+ case "m":
282
+ diff = MONTHS * value;
283
+ break;
284
+ case "day":
285
+ case "d":
286
+ diff = DAYS * value;
287
+ break;
288
+ case "hour":
289
+ case "h":
290
+ diff = HOURS * value;
291
+ break;
292
+ case "minute":
293
+ case "i":
294
+ diff = MINUTES * value;
295
+ break;
296
+ case "second":
297
+ case "s":
298
+ diff = SECONDS * value;
299
+ break;
300
+ default:
301
+ diff = DAYS * value;
302
+ break;
303
+ }
304
+ switch (pastFuture) {
305
+ case "past":
306
+ diff *= -1;
307
+ break;
308
+ case "future":
309
+ break;
310
+ default: // both
311
+ if (this.boolean()) {
312
+ diff *= -1;
313
+ }
314
+ break;
315
+ }
316
+ return this._start + Math.random() * diff;
317
+ }
318
+ /** {@inheritDoc} */
319
+ timestampArray(size, type, value, pastFuture) {
320
+ return this.forLength(size, () => this.timestamp(type, value, pastFuture));
321
+ }
322
+ /** {@inheritDoc} */
323
+ datetime(type, value, pastFuture) {
324
+ return new Date(this.timestamp(type, value, pastFuture));
325
+ }
326
+ /** {@inheritDoc} */
327
+ datetimeArray(size, type, value, pastFuture) {
328
+ return this.forLength(size, () => this.datetime(type, value, pastFuture));
329
+ }
330
+ /** {@inheritDoc} */
331
+ isoDate(type, value, pastFuture) {
332
+ return new Date(this.timestamp(type, value, pastFuture)).toISOString();
333
+ }
334
+ /** {@inheritDoc} */
335
+ isoDateArray(size, type, value, pastFuture) {
336
+ return this.forLength(size, () => this.isoDate(type, value, pastFuture));
337
+ }
338
+ /** {@inheritDoc} */
339
+ date(type, value, pastFuture) {
340
+ return this.isoDate(type, value, pastFuture).substring(0, 10);
341
+ }
342
+ /** {@inheritDoc} */
343
+ dateArray(size, type, value, pastFuture) {
344
+ return this.forLength(size, () => this.date(type, value, pastFuture));
345
+ }
346
+ /** {@inheritDoc} */
347
+ time(type, value, pastFuture) {
348
+ return this.isoDate(type, value, pastFuture).substring(11, 8);
349
+ }
350
+ /** {@inheritDoc} */
351
+ timeArray(size, type, value, pastFuture) {
352
+ return this.forLength(size, () => this.time(type, value, pastFuture));
353
+ }
354
+ /** {@inheritDoc} */
355
+ enum(keys) {
356
+ if (keys && typeof keys === 'object' && !Array.isArray(keys)) {
357
+ const arr = this._enumToArray(keys);
358
+ return arr[Math.floor(Math.random() * arr.length)];
359
+ }
360
+ else if (Array.isArray(keys)) {
361
+ return keys[Math.floor(Math.random() * keys.length)];
362
+ }
363
+ return null;
364
+ }
365
+ /** {@inheritDoc} */
366
+ enumArray(size, keys) {
367
+ let items;
368
+ if (keys && typeof keys === 'object' && !Array.isArray(keys)) {
369
+ items = this._enumToArray(keys);
370
+ }
371
+ else if (Array.isArray(keys)) {
372
+ items = [...keys];
373
+ }
374
+ items = this._shuffle(items);
375
+ if (size > items.length) {
376
+ size = items.length;
377
+ }
378
+ return items.slice(0, size);
379
+ }
380
+ /** {@inheritDoc} */
381
+ urn(...parts) {
382
+ let size = 1;
383
+ if (parts.length > 0) {
384
+ if (typeof parts[0] === "number") {
385
+ size = parts.shift();
386
+ }
387
+ }
388
+ const domains = parts.map(part => {
389
+ switch (typeof part) {
390
+ case "string":
391
+ const str = part.trim();
392
+ return str ? str : undefined;
393
+ case "number":
394
+ case "bigint":
395
+ return part.toString(10);
396
+ case "boolean":
397
+ return part ? 'true' : 'false';
398
+ default:
399
+ return undefined;
400
+ }
401
+ }).filter(part => !!part);
402
+ if (domains.length < 1) {
403
+ domains.push(this.word(3), this.word(5));
404
+ }
405
+ if (!Number.isInteger(size)) {
406
+ size = 1;
407
+ }
408
+ const keys = [];
409
+ for (let i = 0; i < size; i++) {
410
+ keys.push(this.integer());
411
+ }
412
+ domains.push(keys.join('|'));
413
+ return domains.join(':');
414
+ }
415
+ }
416
+ // noinspection JSUnusedGlobalSymbols
417
+ exports.generator = new Generator();
@@ -0,0 +1,2 @@
1
+ export * from './generator';
2
+ export * from './index.types';
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./generator"), exports);
18
+ __exportStar(require("./index.types"), exports);