@tsed/core 6.72.3 → 6.73.3

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/lib/index.js CHANGED
@@ -254,6 +254,257 @@ exports.DecoratorTypes = void 0;
254
254
  DecoratorTypes["CLASS"] = "class";
255
255
  })(exports.DecoratorTypes || (exports.DecoratorTypes = {}));
256
256
 
257
+ function decoratorTypeOf(args) {
258
+ const [target, propertyKey, descriptor] = args;
259
+
260
+ const staticType = type => {
261
+ return target !== classOf(target) ? type : `${type}.static`;
262
+ };
263
+
264
+ if (typeof descriptor === "number") {
265
+ return propertyKey ? staticType("parameter") : "parameter.constructor";
266
+ }
267
+
268
+ if (descriptor && descriptor.value) {
269
+ return staticType("method");
270
+ }
271
+
272
+ if (propertyKey && descriptor === undefined || descriptor) {
273
+ return staticType("property");
274
+ }
275
+
276
+ return exports.DecoratorTypes.CLASS;
277
+ }
278
+
279
+ /**
280
+ * Return true if value is an array
281
+ * @param target
282
+ * @returns {boolean}
283
+ */
284
+ function isArray(target) {
285
+ return Array.isArray(target);
286
+ }
287
+ /**
288
+ * Return true if the clazz is an array.
289
+ * @param target
290
+ * @returns {boolean}
291
+ * @ignore
292
+ */
293
+
294
+ function isArrayOrArrayClass(target) {
295
+ return target === Array || isArray(target);
296
+ }
297
+
298
+ /**
299
+ *
300
+ * @param target
301
+ * @returns {boolean}
302
+ */
303
+ function isDate(target) {
304
+ return target === Date || target instanceof Date && !isNaN(+target);
305
+ }
306
+
307
+ function isFunction(target) {
308
+ return typeof target === "function";
309
+ }
310
+
311
+ function isNil(value) {
312
+ return value === undefined || value === null;
313
+ }
314
+
315
+ /**
316
+ *
317
+ * @param target
318
+ * @returns {boolean}
319
+ */
320
+ function isBoolean(target) {
321
+ return typeof target === "boolean" || target instanceof Boolean || target === Boolean;
322
+ }
323
+ function isBooleanOrBooleanClass(target) {
324
+ return typeof target === "boolean" || target instanceof Boolean || target === Boolean;
325
+ }
326
+
327
+ /**
328
+ *
329
+ * @param target
330
+ * @returns {boolean}
331
+ */
332
+ function isNumber(target) {
333
+ return typeof target === "number";
334
+ }
335
+ function isNumberOrNumberClass(target) {
336
+ return typeof target === "number" || target instanceof Number || target === Number;
337
+ }
338
+
339
+ /**
340
+ * @param target
341
+ * @returns {boolean}
342
+ */
343
+ function isString(target) {
344
+ return typeof target === "string";
345
+ }
346
+ function isStringOrStringClass(target) {
347
+ return typeof target === "string" || target instanceof String || target === String;
348
+ }
349
+
350
+ /**
351
+ * Return true if the given obj is a primitive.
352
+ * @param target
353
+ * @returns {boolean}
354
+ * @ignore
355
+ */
356
+
357
+ function isPrimitiveOrPrimitiveClass(target) {
358
+ return isStringOrStringClass(target) || isNumberOrNumberClass(target) || isBooleanOrBooleanClass(target);
359
+ }
360
+ /**
361
+ * Return true if the given obj is a primitive.
362
+ * @param target
363
+ * @returns {boolean}
364
+ */
365
+
366
+ function isPrimitive(target) {
367
+ return isString(target) || isNumber(target) || isBoolean(target);
368
+ }
369
+ function isPrimitiveClass(target) {
370
+ return [String, Number, Boolean].includes(target);
371
+ }
372
+
373
+ /**
374
+ *
375
+ * @param target
376
+ */
377
+ function isSymbol(target) {
378
+ return typeof target === "symbol";
379
+ }
380
+ function isSymbolOrSymbolClass(target) {
381
+ return typeof target === "symbol" || target instanceof Symbol || target === Symbol;
382
+ }
383
+
384
+ const isBasicType = source => isNil(source) || isPrimitive(source) || isSymbol(source) || isFunction(source);
385
+ /**
386
+ * Return a cloned value
387
+ * @param source
388
+ */
389
+
390
+
391
+ function deepClone(source) {
392
+ let dest;
393
+
394
+ if (isBasicType(source)) {
395
+ return source;
396
+ }
397
+
398
+ if (isDate(source)) {
399
+ return new Date(source);
400
+ }
401
+
402
+ dest = isArray(source) ? [] : {};
403
+
404
+ for (const key in source) {
405
+ // Use getOwnPropertyDescriptor instead of source[key] to prevent from triggering setter/getter.
406
+ const descriptor = Object.getOwnPropertyDescriptor(source, key);
407
+
408
+ if (descriptor) {
409
+ if (!isFunction(descriptor.value)) {
410
+ dest[key] = deepClone(descriptor.value);
411
+ } else {
412
+ Object.defineProperty(dest, key, descriptor);
413
+ }
414
+ }
415
+ }
416
+
417
+ if (!isArray(source)) {
418
+ const prototype = Reflect.getPrototypeOf(source);
419
+ Reflect.setPrototypeOf(dest, prototype);
420
+ }
421
+
422
+ return dest;
423
+ }
424
+
425
+ function createInstance(obj) {
426
+ return obj ? classOf(obj) !== Object ? Object.create(obj) : {} : {};
427
+ }
428
+
429
+ /**
430
+ * Prevent prototype pollution vulnerability
431
+ * @param key
432
+ */
433
+ function isProtectedKey(key) {
434
+ return ["__proto__", "constructor", "prototype"].includes(key);
435
+ }
436
+
437
+ function objectKeys(obj) {
438
+ return Object.keys(obj).filter(key => !isProtectedKey(key));
439
+ }
440
+
441
+ function mergeReducerBuilder(cb) {
442
+ return (collection, value, options) => {
443
+ const index = collection.findIndex(item => cb(item, value));
444
+
445
+ if (index === -1) {
446
+ return [...collection, value];
447
+ }
448
+
449
+ collection[index] = deepMerge(collection[index], value, options);
450
+ return collection;
451
+ };
452
+ }
453
+ const defaultReducer = mergeReducerBuilder((a, b) => a === b);
454
+
455
+ function getReducer({
456
+ reducers,
457
+ parentKey
458
+ }) {
459
+ if (!reducers) {
460
+ return defaultReducer;
461
+ }
462
+
463
+ if (parentKey && reducers[parentKey]) {
464
+ return reducers[parentKey];
465
+ }
466
+
467
+ return reducers["default"] || defaultReducer;
468
+ }
469
+
470
+ function shouldReturnObj(obj, source) {
471
+ return isPrimitive(obj) || isSymbol(obj) || isFunction(obj) || source === undefined;
472
+ }
473
+
474
+ function shouldReturnSource(obj, source) {
475
+ return obj === undefined || obj === null || obj === "" && source !== "";
476
+ }
477
+
478
+ function deepMerge(source, obj, options = {}) {
479
+ if (shouldReturnSource(obj, source)) {
480
+ return source;
481
+ }
482
+
483
+ if (shouldReturnObj(obj, source)) {
484
+ return obj;
485
+ }
486
+
487
+ if (Array.isArray(source)) {
488
+ const reducer = getReducer(options);
489
+ return [].concat(obj).reduce((out, value) => reducer(out, value, options), [...source]);
490
+ }
491
+
492
+ return [...objectKeys(source), ...objectKeys(obj)].reduce((out, key) => {
493
+ const src = source && source[key];
494
+ const value = deepMerge(src, obj && obj[key], _extends({}, options, {
495
+ parentKey: key
496
+ }));
497
+
498
+ if (options.cleanUndefinedProps && value === undefined) {
499
+ return out;
500
+ }
501
+
502
+ return _extends({}, out, {
503
+ [key]: value
504
+ });
505
+ }, createInstance(source));
506
+ }
507
+
257
508
  /**
258
509
  * Metadata key
259
510
  * @private
@@ -994,28 +1245,6 @@ function decoratorArgs(target, propertyKey) {
994
1245
  return [target, propertyKey, descriptorOf(target, propertyKey)];
995
1246
  }
996
1247
 
997
- function decoratorTypeOf(args) {
998
- const [target, propertyKey, descriptor] = args;
999
-
1000
- const staticType = type => {
1001
- return target !== classOf(target) ? type : `${type}.static`;
1002
- };
1003
-
1004
- if (typeof descriptor === "number") {
1005
- return propertyKey ? staticType("parameter") : "parameter.constructor";
1006
- }
1007
-
1008
- if (descriptor && descriptor.value) {
1009
- return staticType("method");
1010
- }
1011
-
1012
- if (propertyKey && descriptor === undefined || descriptor) {
1013
- return staticType("property");
1014
- }
1015
-
1016
- return exports.DecoratorTypes.CLASS;
1017
- }
1018
-
1019
1248
  function inheritedDescriptorOf(target, propertyKey) {
1020
1249
  for (const klass of ancestorsOf(target)) {
1021
1250
  const descriptor = Object.getOwnPropertyDescriptor(klass && klass.prototype || klass, propertyKey);
@@ -1045,14 +1274,6 @@ function useMethodDecorators(...decorators) {
1045
1274
  };
1046
1275
  }
1047
1276
 
1048
- /**
1049
- * Prevent prototype pollution vulnerability
1050
- * @param key
1051
- */
1052
- function isProtectedKey(key) {
1053
- return ["__proto__", "constructor", "prototype"].includes(key);
1054
- }
1055
-
1056
1277
  /**
1057
1278
  * Remove undefined value
1058
1279
  * @param obj
@@ -1087,160 +1308,6 @@ function constructorOf(target) {
1087
1308
  return getConstructor(target);
1088
1309
  }
1089
1310
 
1090
- /**
1091
- * Return true if value is an array
1092
- * @param target
1093
- * @returns {boolean}
1094
- */
1095
- function isArray(target) {
1096
- return Array.isArray(target);
1097
- }
1098
- /**
1099
- * Return true if the clazz is an array.
1100
- * @param target
1101
- * @returns {boolean}
1102
- * @ignore
1103
- */
1104
-
1105
- function isArrayOrArrayClass(target) {
1106
- return target === Array || isArray(target);
1107
- }
1108
-
1109
- /**
1110
- *
1111
- * @param target
1112
- * @returns {boolean}
1113
- */
1114
- function isDate(target) {
1115
- return target === Date || target instanceof Date && !isNaN(+target);
1116
- }
1117
-
1118
- function isFunction(target) {
1119
- return typeof target === "function";
1120
- }
1121
-
1122
- function isNil(value) {
1123
- return value === undefined || value === null;
1124
- }
1125
-
1126
- /**
1127
- *
1128
- * @param target
1129
- * @returns {boolean}
1130
- */
1131
- function isBoolean(target) {
1132
- return typeof target === "boolean" || target instanceof Boolean || target === Boolean;
1133
- }
1134
- function isBooleanOrBooleanClass(target) {
1135
- return typeof target === "boolean" || target instanceof Boolean || target === Boolean;
1136
- }
1137
-
1138
- /**
1139
- *
1140
- * @param target
1141
- * @returns {boolean}
1142
- */
1143
- function isNumber(target) {
1144
- return typeof target === "number";
1145
- }
1146
- function isNumberOrNumberClass(target) {
1147
- return typeof target === "number" || target instanceof Number || target === Number;
1148
- }
1149
-
1150
- /**
1151
- * @param target
1152
- * @returns {boolean}
1153
- */
1154
- function isString(target) {
1155
- return typeof target === "string";
1156
- }
1157
- function isStringOrStringClass(target) {
1158
- return typeof target === "string" || target instanceof String || target === String;
1159
- }
1160
-
1161
- /**
1162
- * Return true if the given obj is a primitive.
1163
- * @param target
1164
- * @returns {boolean}
1165
- * @ignore
1166
- */
1167
-
1168
- function isPrimitiveOrPrimitiveClass(target) {
1169
- return isStringOrStringClass(target) || isNumberOrNumberClass(target) || isBooleanOrBooleanClass(target);
1170
- }
1171
- /**
1172
- * Return true if the given obj is a primitive.
1173
- * @param target
1174
- * @returns {boolean}
1175
- */
1176
-
1177
- function isPrimitive(target) {
1178
- return isString(target) || isNumber(target) || isBoolean(target);
1179
- }
1180
- function isPrimitiveClass(target) {
1181
- return [String, Number, Boolean].includes(target);
1182
- }
1183
-
1184
- /**
1185
- *
1186
- * @param target
1187
- */
1188
- function isSymbol(target) {
1189
- return typeof target === "symbol";
1190
- }
1191
- function isSymbolOrSymbolClass(target) {
1192
- return typeof target === "symbol" || target instanceof Symbol || target === Symbol;
1193
- }
1194
-
1195
- const isBasicType = source => isNil(source) || isPrimitive(source) || isSymbol(source) || isFunction(source);
1196
- /**
1197
- * Return a cloned value
1198
- * @param source
1199
- */
1200
-
1201
-
1202
- function deepClone(source) {
1203
- let dest;
1204
-
1205
- if (isBasicType(source)) {
1206
- return source;
1207
- }
1208
-
1209
- if (isDate(source)) {
1210
- return new Date(source);
1211
- }
1212
-
1213
- dest = isArray(source) ? [] : {};
1214
-
1215
- for (const key in source) {
1216
- // Use getOwnPropertyDescriptor instead of source[key] to prevent from triggering setter/getter.
1217
- const descriptor = Object.getOwnPropertyDescriptor(source, key);
1218
-
1219
- if (descriptor) {
1220
- if (!isFunction(descriptor.value)) {
1221
- dest[key] = deepClone(descriptor.value);
1222
- } else {
1223
- Object.defineProperty(dest, key, descriptor);
1224
- }
1225
- }
1226
- }
1227
-
1228
- if (!isArray(source)) {
1229
- const prototype = Reflect.getPrototypeOf(source);
1230
- Reflect.setPrototypeOf(dest, prototype);
1231
- }
1232
-
1233
- return dest;
1234
- }
1235
-
1236
- function createInstance(obj) {
1237
- return obj ? classOf(obj) !== Object ? Object.create(obj) : {} : {};
1238
- }
1239
-
1240
- function objectKeys(obj) {
1241
- return Object.keys(obj).filter(key => !isProtectedKey(key));
1242
- }
1243
-
1244
1311
  function reducer() {
1245
1312
  return (collection, value) => {
1246
1313
  collection.indexOf(value) === -1 && collection.push(value);
@@ -1320,73 +1387,6 @@ function deepExtends(out, obj, reducers = {}) {
1320
1387
  return out;
1321
1388
  }
1322
1389
 
1323
- function mergeReducerBuilder(cb) {
1324
- return (collection, value, options) => {
1325
- const index = collection.findIndex(item => cb(item, value));
1326
-
1327
- if (index === -1) {
1328
- return [...collection, value];
1329
- }
1330
-
1331
- collection[index] = deepMerge(collection[index], value, options);
1332
- return collection;
1333
- };
1334
- }
1335
- const defaultReducer = mergeReducerBuilder((a, b) => a === b);
1336
-
1337
- function getReducer({
1338
- reducers,
1339
- parentKey
1340
- }) {
1341
- if (!reducers) {
1342
- return defaultReducer;
1343
- }
1344
-
1345
- if (parentKey && reducers[parentKey]) {
1346
- return reducers[parentKey];
1347
- }
1348
-
1349
- return reducers["default"] || defaultReducer;
1350
- }
1351
-
1352
- function shouldReturnObj(obj, source) {
1353
- return isPrimitive(obj) || isSymbol(obj) || isFunction(obj) || source === undefined;
1354
- }
1355
-
1356
- function shouldReturnSource(obj, source) {
1357
- return obj === undefined || obj === null || obj === "" && source !== "";
1358
- }
1359
-
1360
- function deepMerge(source, obj, options = {}) {
1361
- if (shouldReturnSource(obj, source)) {
1362
- return source;
1363
- }
1364
-
1365
- if (shouldReturnObj(obj, source)) {
1366
- return obj;
1367
- }
1368
-
1369
- if (Array.isArray(source)) {
1370
- const reducer = getReducer(options);
1371
- return [].concat(obj).reduce((out, value) => reducer(out, value, options), [...source]);
1372
- }
1373
-
1374
- return [...objectKeys(source), ...objectKeys(obj)].reduce((out, key) => {
1375
- const src = source && source[key];
1376
- const value = deepMerge(src, obj && obj[key], _extends({}, options, {
1377
- parentKey: key
1378
- }));
1379
-
1380
- if (options.cleanUndefinedProps && value === undefined) {
1381
- return out;
1382
- }
1383
-
1384
- return _extends({}, out, {
1385
- [key]: value
1386
- });
1387
- }, createInstance(source));
1388
- }
1389
-
1390
1390
  function isArrowFn(target) {
1391
1391
  return target && isFunction(target) && !target.prototype;
1392
1392
  }
@@ -1401,7 +1401,7 @@ function isClassObject(target) {
1401
1401
  return target === Object;
1402
1402
  }
1403
1403
  function isPlainObject(target) {
1404
- return isClassObject(classOf(target));
1404
+ return isClassObject(classOf(target)) && target !== Object;
1405
1405
  }
1406
1406
 
1407
1407
  /**
@@ -1590,6 +1590,10 @@ function isStream(obj) {
1590
1590
  return obj !== null && typeof obj === "object" && typeof obj.pipe === "function";
1591
1591
  }
1592
1592
 
1593
+ function isSerializable(data) {
1594
+ return !(Buffer.isBuffer(data) || isStream(data) || isBoolean(data) || isNumber(data) || isString(data) || isNil(data));
1595
+ }
1596
+
1593
1597
  function primitiveOf(target) {
1594
1598
  if (isStringOrStringClass(target)) {
1595
1599
  return "string";
@@ -2348,6 +2352,7 @@ exports.isPrimitiveClass = isPrimitiveClass;
2348
2352
  exports.isPrimitiveOrPrimitiveClass = isPrimitiveOrPrimitiveClass;
2349
2353
  exports.isPromise = isPromise;
2350
2354
  exports.isProtectedKey = isProtectedKey;
2355
+ exports.isSerializable = isSerializable;
2351
2356
  exports.isStream = isStream;
2352
2357
  exports.isString = isString;
2353
2358
  exports.isStringOrStringClass = isStringOrStringClass;