topsyde-utils 1.0.202 → 1.0.203

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.
@@ -20,20 +20,15 @@ export declare abstract class Dto {
20
20
  */
21
21
  toJSON<T = Record<string, unknown>>(include_undefined?: boolean, options?: ClassTransformOptions): T;
22
22
  /**
23
- * Creates a new instance of the DTO with validation
24
- * @param cls - The class constructor to create an instance from
23
+ * Creates a new instance of the DTO with validation (infers class from `this`)
25
24
  * @param data - Data to create the DTO from
26
- * @param options - Class transformer options for controlling exposure and transformation
25
+ * @param options - Class transformer options
27
26
  * @returns New instance of the DTO
28
- * @throws ValidationError[] if validation fails and validate is true
29
27
  */
30
- static Create<T extends Dto>(cls: ClassConstructor<T>, data: Record<string, unknown>, options?: ClassTransformOptions): T;
28
+ static Create<T extends Dto>(this: ClassConstructor<T>, // 🔑 Key change: use 'this' parameter
29
+ data: Record<string, unknown>, options?: ClassTransformOptions): T;
31
30
  /**
32
31
  * Creates an array of DTOs from an array of plain objects
33
- * @param cls - The class constructor to create instances from
34
- * @param dataArray - Array of data to create DTOs from
35
- * @param options - Class transformer options for controlling exposure and transformation
36
- * @returns Array of DTO instances
37
32
  */
38
- static CreateMany<T extends Dto>(cls: ClassConstructor<T>, dataArray: Record<string, unknown>[], options?: ClassTransformOptions): T[];
33
+ static CreateMany<T extends Dto>(this: ClassConstructor<T>, dataArray: Record<string, unknown>[], options?: ClassTransformOptions): T[];
39
34
  }
@@ -35,15 +35,15 @@ export class Dto {
35
35
  return value;
36
36
  }
37
37
  /**
38
- * Creates a new instance of the DTO with validation
39
- * @param cls - The class constructor to create an instance from
38
+ * Creates a new instance of the DTO with validation (infers class from `this`)
40
39
  * @param data - Data to create the DTO from
41
- * @param options - Class transformer options for controlling exposure and transformation
40
+ * @param options - Class transformer options
42
41
  * @returns New instance of the DTO
43
- * @throws ValidationError[] if validation fails and validate is true
44
42
  */
45
- static Create(cls, data, options = {}) {
46
- const instance = plainToInstance(cls, data, {
43
+ static Create(// 🔑 Key change: use 'this' parameter
44
+ data, options = {}) {
45
+ const instance = plainToInstance(this, data, {
46
+ // 🔑 Use 'this' instead of 'cls'
47
47
  ...Dto.defaultTransformOptions,
48
48
  ...options,
49
49
  });
@@ -51,13 +51,12 @@ export class Dto {
51
51
  }
52
52
  /**
53
53
  * Creates an array of DTOs from an array of plain objects
54
- * @param cls - The class constructor to create instances from
55
- * @param dataArray - Array of data to create DTOs from
56
- * @param options - Class transformer options for controlling exposure and transformation
57
- * @returns Array of DTO instances
58
54
  */
59
- static CreateMany(cls, dataArray, options = {}) {
60
- return dataArray.map((data) => Dto.Create(cls, data, options));
55
+ static CreateMany(dataArray, options = {}) {
56
+ return plainToInstance(this, dataArray, {
57
+ ...Dto.defaultTransformOptions,
58
+ ...options,
59
+ });
61
60
  }
62
61
  }
63
62
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"BaseDto.js","sourceRoot":"","sources":["../../src/utils/BaseDto.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACrE,OAAO,MAAM,MAAM,UAAU,CAAC;AAC9B,OAAO,EAAmB,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAEhE;;GAEG;AACH,MAAM,OAAgB,GAAG;IAWxB;;;OAGG;IACI,QAAQ;QACd,MAAM,MAAM,GAAG,YAAY,CAAC,IAAI,EAAE;YACjC,eAAe,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE;YAClC,mBAAmB,EAAE,IAAI;SACzB,CAAC,CAAC;QACH,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACvB,MAAM,MAAM,CAAC;QACd,CAAC;QACD,OAAO,MAAM,CAAC;IACf,CAAC;IAED;;;;OAIG;IACI,MAAM,CAA8B,oBAA6B,IAAI,EAAE,OAA+B;QAC5G,MAAM,KAAK,GAAG,eAAe,CAAC,IAAI,EAAE;YACnC,GAAG,GAAG,CAAC,uBAAuB;YAC9B,GAAG,OAAO;SACV,CAAM,CAAC;QAER,IAAI,CAAC,iBAAiB,EAAE,CAAC;YACxB,OAAO,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,OAAO,CAAC,KAAgC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAM,CAAC;QACvH,CAAC;QAED,OAAO,KAAK,CAAC;IACd,CAAC;IAED;;;;;;;OAOG;IACI,MAAM,CAAC,MAAM,CAAgB,GAAwB,EAAE,IAA6B,EAAE,UAAiC,EAAE;QAC/H,MAAM,QAAQ,GAAG,eAAe,CAAC,GAAG,EAAE,IAAI,EAAE;YAC3C,GAAG,GAAG,CAAC,uBAAuB;YAC9B,GAAG,OAAO;SACV,CAAC,CAAC;QAEH,OAAO,QAAQ,CAAC;IACjB,CAAC;IAED;;;;;;OAMG;IACI,MAAM,CAAC,UAAU,CAAgB,GAAwB,EAAE,SAAoC,EAAE,UAAiC,EAAE;QAC1I,OAAO,SAAS,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC;IAChE,CAAC;;AArED;;GAEG;AACuB,2BAAuB,GAA0B;IAC1E,uBAAuB,EAAE,IAAI;IAC7B,mBAAmB,EAAE,IAAI;IACzB,mBAAmB,EAAE,IAAI;IACzB,wBAAwB,EAAE,KAAK,EAAE,uDAAuD;CACxF,CAAC","sourcesContent":["import type { ClassConstructor, ClassTransformOptions } from \"class-transformer\";\nimport { instanceToPlain, plainToInstance } from \"class-transformer\";\nimport Guards from \"./Guards\";\nimport { ValidationError, validateSync } from \"class-validator\";\n\n/**\n * Base typesafe class for Data Transfer Objects (DTOs)\n */\nexport abstract class Dto {\n\t/**\n\t * Default options for class transformation\n\t */\n\tprotected static readonly defaultTransformOptions: ClassTransformOptions = {\n\t\texcludeExtraneousValues: true,\n\t\tenableCircularCheck: true,\n\t\texposeDefaultValues: true,\n\t\tenableImplicitConversion: false, // Safer default, especially when using class-validator\n\t};\n\n\t/**\n\t * Validates the DTO instance\n\t * @throws ValidationError[] if validation fails\n\t */\n\tpublic validate(): ValidationError[] {\n\t\tconst errors = validateSync(this, {\n\t\t\tvalidationError: { target: false },\n\t\t\tforbidUnknownValues: true,\n\t\t});\n\t\tif (errors.length > 0) {\n\t\t\tthrow errors;\n\t\t}\n\t\treturn errors;\n\t}\n\n\t/**\n\t * Converts the DTO to a plain object\n\t * @param options - Class transformer options for controlling exposure and transformation\n\t * @returns Plain object representation of the DTO\n\t */\n\tpublic toJSON<T = Record<string, unknown>>(include_undefined: boolean = true, options?: ClassTransformOptions): T {\n\t\tconst value = instanceToPlain(this, {\n\t\t\t...Dto.defaultTransformOptions,\n\t\t\t...options,\n\t\t}) as T;\n\n\t\tif (!include_undefined) {\n\t\t\treturn Object.fromEntries(Object.entries(value as Record<string, unknown>).filter(([_, v]) => !Guards.IsNil(v))) as T;\n\t\t}\n\n\t\treturn value;\n\t}\n\n\t/**\n\t * Creates a new instance of the DTO with validation\n\t * @param cls - The class constructor to create an instance from\n\t * @param data - Data to create the DTO from\n\t * @param options - Class transformer options for controlling exposure and transformation\n\t * @returns New instance of the DTO\n\t * @throws ValidationError[] if validation fails and validate is true\n\t */\n\tpublic static Create<T extends Dto>(cls: ClassConstructor<T>, data: Record<string, unknown>, options: ClassTransformOptions = {}): T {\n\t\tconst instance = plainToInstance(cls, data, {\n\t\t\t...Dto.defaultTransformOptions,\n\t\t\t...options,\n\t\t});\n\n\t\treturn instance;\n\t}\n\n\t/**\n\t * Creates an array of DTOs from an array of plain objects\n\t * @param cls - The class constructor to create instances from\n\t * @param dataArray - Array of data to create DTOs from\n\t * @param options - Class transformer options for controlling exposure and transformation\n\t * @returns Array of DTO instances\n\t */\n\tpublic static CreateMany<T extends Dto>(cls: ClassConstructor<T>, dataArray: Record<string, unknown>[], options: ClassTransformOptions = {}): T[] {\n\t\treturn dataArray.map((data) => Dto.Create(cls, data, options));\n\t}\n}\n"]}
1
+ {"version":3,"file":"BaseDto.js","sourceRoot":"","sources":["../../src/utils/BaseDto.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACrE,OAAO,MAAM,MAAM,UAAU,CAAC;AAC9B,OAAO,EAAmB,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAEhE;;GAEG;AACH,MAAM,OAAgB,GAAG;IAWxB;;;OAGG;IACI,QAAQ;QACd,MAAM,MAAM,GAAG,YAAY,CAAC,IAAI,EAAE;YACjC,eAAe,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE;YAClC,mBAAmB,EAAE,IAAI;SACzB,CAAC,CAAC;QACH,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACvB,MAAM,MAAM,CAAC;QACd,CAAC;QACD,OAAO,MAAM,CAAC;IACf,CAAC;IAED;;;;OAIG;IACI,MAAM,CAA8B,oBAA6B,IAAI,EAAE,OAA+B;QAC5G,MAAM,KAAK,GAAG,eAAe,CAAC,IAAI,EAAE;YACnC,GAAG,GAAG,CAAC,uBAAuB;YAC9B,GAAG,OAAO;SACV,CAAM,CAAC;QAER,IAAI,CAAC,iBAAiB,EAAE,CAAC;YACxB,OAAO,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,OAAO,CAAC,KAAgC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAM,CAAC;QACvH,CAAC;QAED,OAAO,KAAK,CAAC;IACd,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,MAAM,CACQ,sCAAsC;IACjE,IAA6B,EAC7B,UAAiC,EAAE;QAEnC,MAAM,QAAQ,GAAG,eAAe,CAAC,IAAI,EAAE,IAAI,EAAE;YAC5C,iCAAiC;YACjC,GAAG,GAAG,CAAC,uBAAuB;YAC9B,GAAG,OAAO;SACV,CAAC,CAAC;QAEH,OAAO,QAAQ,CAAC;IACjB,CAAC;IAED;;OAEG;IACI,MAAM,CAAC,UAAU,CAA2C,SAAoC,EAAE,UAAiC,EAAE;QAC3I,OAAO,eAAe,CAAC,IAAI,EAAE,SAAS,EAAE;YACvC,GAAG,GAAG,CAAC,uBAAuB;YAC9B,GAAG,OAAO;SACV,CAAQ,CAAC;IACX,CAAC;;AAvED;;GAEG;AACuB,2BAAuB,GAA0B;IAC1E,uBAAuB,EAAE,IAAI;IAC7B,mBAAmB,EAAE,IAAI;IACzB,mBAAmB,EAAE,IAAI;IACzB,wBAAwB,EAAE,KAAK,EAAE,uDAAuD;CACxF,CAAC","sourcesContent":["import type { ClassConstructor, ClassTransformOptions } from \"class-transformer\";\nimport { instanceToPlain, plainToInstance } from \"class-transformer\";\nimport Guards from \"./Guards\";\nimport { ValidationError, validateSync } from \"class-validator\";\n\n/**\n * Base typesafe class for Data Transfer Objects (DTOs)\n */\nexport abstract class Dto {\n\t/**\n\t * Default options for class transformation\n\t */\n\tprotected static readonly defaultTransformOptions: ClassTransformOptions = {\n\t\texcludeExtraneousValues: true,\n\t\tenableCircularCheck: true,\n\t\texposeDefaultValues: true,\n\t\tenableImplicitConversion: false, // Safer default, especially when using class-validator\n\t};\n\n\t/**\n\t * Validates the DTO instance\n\t * @throws ValidationError[] if validation fails\n\t */\n\tpublic validate(): ValidationError[] {\n\t\tconst errors = validateSync(this, {\n\t\t\tvalidationError: { target: false },\n\t\t\tforbidUnknownValues: true,\n\t\t});\n\t\tif (errors.length > 0) {\n\t\t\tthrow errors;\n\t\t}\n\t\treturn errors;\n\t}\n\n\t/**\n\t * Converts the DTO to a plain object\n\t * @param options - Class transformer options for controlling exposure and transformation\n\t * @returns Plain object representation of the DTO\n\t */\n\tpublic toJSON<T = Record<string, unknown>>(include_undefined: boolean = true, options?: ClassTransformOptions): T {\n\t\tconst value = instanceToPlain(this, {\n\t\t\t...Dto.defaultTransformOptions,\n\t\t\t...options,\n\t\t}) as T;\n\n\t\tif (!include_undefined) {\n\t\t\treturn Object.fromEntries(Object.entries(value as Record<string, unknown>).filter(([_, v]) => !Guards.IsNil(v))) as T;\n\t\t}\n\n\t\treturn value;\n\t}\n\n\t/**\n\t * Creates a new instance of the DTO with validation (infers class from `this`)\n\t * @param data - Data to create the DTO from\n\t * @param options - Class transformer options\n\t * @returns New instance of the DTO\n\t */\n\tpublic static Create<T extends Dto>(\n\t\tthis: ClassConstructor<T>, // 🔑 Key change: use 'this' parameter\n\t\tdata: Record<string, unknown>,\n\t\toptions: ClassTransformOptions = {},\n\t): T {\n\t\tconst instance = plainToInstance(this, data, {\n\t\t\t// 🔑 Use 'this' instead of 'cls'\n\t\t\t...Dto.defaultTransformOptions,\n\t\t\t...options,\n\t\t});\n\n\t\treturn instance;\n\t}\n\n\t/**\n\t * Creates an array of DTOs from an array of plain objects\n\t */\n\tpublic static CreateMany<T extends Dto>(this: ClassConstructor<T>, dataArray: Record<string, unknown>[], options: ClassTransformOptions = {}): T[] {\n\t\treturn plainToInstance(this, dataArray, {\n\t\t\t...Dto.defaultTransformOptions,\n\t\t\t...options,\n\t\t}) as T[];\n\t}\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "topsyde-utils",
3
- "version": "1.0.202",
3
+ "version": "1.0.203",
4
4
  "description": "A bundle of TypeScript utility classes and functions",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -51,15 +51,18 @@ export abstract class Dto {
51
51
  }
52
52
 
53
53
  /**
54
- * Creates a new instance of the DTO with validation
55
- * @param cls - The class constructor to create an instance from
54
+ * Creates a new instance of the DTO with validation (infers class from `this`)
56
55
  * @param data - Data to create the DTO from
57
- * @param options - Class transformer options for controlling exposure and transformation
56
+ * @param options - Class transformer options
58
57
  * @returns New instance of the DTO
59
- * @throws ValidationError[] if validation fails and validate is true
60
58
  */
61
- public static Create<T extends Dto>(cls: ClassConstructor<T>, data: Record<string, unknown>, options: ClassTransformOptions = {}): T {
62
- const instance = plainToInstance(cls, data, {
59
+ public static Create<T extends Dto>(
60
+ this: ClassConstructor<T>, // 🔑 Key change: use 'this' parameter
61
+ data: Record<string, unknown>,
62
+ options: ClassTransformOptions = {},
63
+ ): T {
64
+ const instance = plainToInstance(this, data, {
65
+ // 🔑 Use 'this' instead of 'cls'
63
66
  ...Dto.defaultTransformOptions,
64
67
  ...options,
65
68
  });
@@ -69,12 +72,11 @@ export abstract class Dto {
69
72
 
70
73
  /**
71
74
  * Creates an array of DTOs from an array of plain objects
72
- * @param cls - The class constructor to create instances from
73
- * @param dataArray - Array of data to create DTOs from
74
- * @param options - Class transformer options for controlling exposure and transformation
75
- * @returns Array of DTO instances
76
75
  */
77
- public static CreateMany<T extends Dto>(cls: ClassConstructor<T>, dataArray: Record<string, unknown>[], options: ClassTransformOptions = {}): T[] {
78
- return dataArray.map((data) => Dto.Create(cls, data, options));
76
+ public static CreateMany<T extends Dto>(this: ClassConstructor<T>, dataArray: Record<string, unknown>[], options: ClassTransformOptions = {}): T[] {
77
+ return plainToInstance(this, dataArray, {
78
+ ...Dto.defaultTransformOptions,
79
+ ...options,
80
+ }) as T[];
79
81
  }
80
82
  }