topsyde-utils 1.0.199 → 1.0.200
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/utils/BaseDto.d.ts +2 -2
- package/dist/utils/BaseDto.js +3 -3
- package/dist/utils/BaseDto.js.map +1 -1
- package/dist/utils/BaseEntity.d.ts +8 -8
- package/dist/utils/BaseEntity.js +12 -12
- package/dist/utils/BaseEntity.js.map +1 -1
- package/package.json +1 -1
- package/src/utils/BaseDto.ts +3 -3
- package/src/utils/BaseEntity.ts +13 -13
package/dist/utils/BaseDto.d.ts
CHANGED
@@ -27,7 +27,7 @@ export declare abstract class Dto {
|
|
27
27
|
* @returns New instance of the DTO
|
28
28
|
* @throws ValidationError[] if validation fails and validate is true
|
29
29
|
*/
|
30
|
-
static
|
30
|
+
static Create<T extends Dto>(cls: ClassConstructor<T>, data: Record<string, unknown>, options?: ClassTransformOptions): T;
|
31
31
|
/**
|
32
32
|
* Creates an array of DTOs from an array of plain objects
|
33
33
|
* @param cls - The class constructor to create instances from
|
@@ -35,5 +35,5 @@ export declare abstract class Dto {
|
|
35
35
|
* @param options - Class transformer options for controlling exposure and transformation
|
36
36
|
* @returns Array of DTO instances
|
37
37
|
*/
|
38
|
-
static
|
38
|
+
static CreateMany<T extends Dto>(cls: ClassConstructor<T>, dataArray: Record<string, unknown>[], options?: ClassTransformOptions): T[];
|
39
39
|
}
|
package/dist/utils/BaseDto.js
CHANGED
@@ -42,7 +42,7 @@ export class Dto {
|
|
42
42
|
* @returns New instance of the DTO
|
43
43
|
* @throws ValidationError[] if validation fails and validate is true
|
44
44
|
*/
|
45
|
-
static
|
45
|
+
static Create(cls, data, options = {}) {
|
46
46
|
const instance = plainToInstance(cls, data, {
|
47
47
|
...Dto.defaultTransformOptions,
|
48
48
|
...options,
|
@@ -56,8 +56,8 @@ export class Dto {
|
|
56
56
|
* @param options - Class transformer options for controlling exposure and transformation
|
57
57
|
* @returns Array of DTO instances
|
58
58
|
*/
|
59
|
-
static
|
60
|
-
return dataArray.map((data) => Dto.
|
59
|
+
static CreateMany(cls, dataArray, options = {}) {
|
60
|
+
return dataArray.map((data) => Dto.Create(cls, data, options));
|
61
61
|
}
|
62
62
|
}
|
63
63
|
/**
|
@@ -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;
|
1
|
+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AAC;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"]}
|
@@ -9,21 +9,21 @@ export default abstract class BaseEntity {
|
|
9
9
|
* Abstract method - entities must define how to convert to DTO
|
10
10
|
*/
|
11
11
|
abstract toDto(): Dto;
|
12
|
+
/**
|
13
|
+
* Updates entity with partial data (immutable - returns new instance)
|
14
|
+
* @param data - Partial data to update
|
15
|
+
* @param validate - Whether to validate after update (default: true)
|
16
|
+
*/
|
17
|
+
update<T extends BaseEntity>(this: T, data: Partial<T>): T;
|
12
18
|
/**
|
13
19
|
* Creates a new entity instance from DTO with validation
|
14
20
|
* @param cls - Entity class constructor
|
15
21
|
* @param dto - DTO to create entity from
|
16
22
|
* @param validate - Whether to validate after creation (default: true)
|
17
23
|
*/
|
18
|
-
static
|
24
|
+
static FromDto<T extends BaseEntity>(cls: ClassConstructor<T>, dto: Dto): T;
|
19
25
|
/**
|
20
26
|
* Creates multiple entities from DTOs
|
21
27
|
*/
|
22
|
-
static
|
23
|
-
/**
|
24
|
-
* Updates entity with partial data (immutable - returns new instance)
|
25
|
-
* @param data - Partial data to update
|
26
|
-
* @param validate - Whether to validate after update (default: true)
|
27
|
-
*/
|
28
|
-
update<T extends BaseEntity>(this: T, data: Partial<T>): T;
|
28
|
+
static FromDtos<T extends BaseEntity>(cls: ClassConstructor<T>, dtos: Dto[]): T[];
|
29
29
|
}
|
package/dist/utils/BaseEntity.js
CHANGED
@@ -6,30 +6,30 @@ export default class BaseEntity {
|
|
6
6
|
toJSON() {
|
7
7
|
return instanceToPlain(this);
|
8
8
|
}
|
9
|
+
/**
|
10
|
+
* Updates entity with partial data (immutable - returns new instance)
|
11
|
+
* @param data - Partial data to update
|
12
|
+
* @param validate - Whether to validate after update (default: true)
|
13
|
+
*/
|
14
|
+
update(data) {
|
15
|
+
const updated = Object.assign(Object.create(Object.getPrototypeOf(this)), this, data);
|
16
|
+
return updated;
|
17
|
+
}
|
9
18
|
/**
|
10
19
|
* Creates a new entity instance from DTO with validation
|
11
20
|
* @param cls - Entity class constructor
|
12
21
|
* @param dto - DTO to create entity from
|
13
22
|
* @param validate - Whether to validate after creation (default: true)
|
14
23
|
*/
|
15
|
-
static
|
24
|
+
static FromDto(cls, dto) {
|
16
25
|
const instance = plainToInstance(cls, dto.toJSON());
|
17
26
|
return instance;
|
18
27
|
}
|
19
28
|
/**
|
20
29
|
* Creates multiple entities from DTOs
|
21
30
|
*/
|
22
|
-
static
|
23
|
-
return dtos.map((dto) => BaseEntity.
|
24
|
-
}
|
25
|
-
/**
|
26
|
-
* Updates entity with partial data (immutable - returns new instance)
|
27
|
-
* @param data - Partial data to update
|
28
|
-
* @param validate - Whether to validate after update (default: true)
|
29
|
-
*/
|
30
|
-
update(data) {
|
31
|
-
const updated = Object.assign(Object.create(Object.getPrototypeOf(this)), this, data);
|
32
|
-
return updated;
|
31
|
+
static FromDtos(cls, dtos) {
|
32
|
+
return dtos.map((dto) => BaseEntity.FromDto(cls, dto));
|
33
33
|
}
|
34
34
|
}
|
35
35
|
//# sourceMappingURL=BaseEntity.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"BaseEntity.js","sourceRoot":"","sources":["../../src/utils/BaseEntity.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,eAAe,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAGvF,MAAM,CAAC,OAAO,OAAgB,UAAU;IACvC;;OAEG;IACI,MAAM;QACZ,OAAO,eAAe,CAAC,IAAI,CAAM,CAAC;IACnC,CAAC;IAOD
|
1
|
+
{"version":3,"file":"BaseEntity.js","sourceRoot":"","sources":["../../src/utils/BaseEntity.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,eAAe,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAGvF,MAAM,CAAC,OAAO,OAAgB,UAAU;IACvC;;OAEG;IACI,MAAM;QACZ,OAAO,eAAe,CAAC,IAAI,CAAM,CAAC;IACnC,CAAC;IAOD;;;;OAIG;IACI,MAAM,CAAgC,IAAgB;QAC5D,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;QACtF,OAAO,OAAO,CAAC;IAChB,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,OAAO,CAAuB,GAAwB,EAAE,GAAQ;QAC7E,MAAM,QAAQ,GAAG,eAAe,CAAC,GAAG,EAAE,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC;QACpD,OAAO,QAAQ,CAAC;IACjB,CAAC;IAED;;OAEG;IACI,MAAM,CAAC,QAAQ,CAAuB,GAAwB,EAAE,IAAW;QACjF,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;IACxD,CAAC;CACD","sourcesContent":["import { ClassConstructor, instanceToPlain, plainToInstance } from \"class-transformer\";\nimport { Dto } from \"./BaseDto\";\n\nexport default abstract class BaseEntity {\n\t/**\n\t * Converts entity to plain object\n\t */\n\tpublic toJSON<T = Record<string, unknown>>(): T {\n\t\treturn instanceToPlain(this) as T;\n\t}\n\n\t/**\n\t * Abstract method - entities must define how to convert to DTO\n\t */\n\tpublic abstract toDto(): Dto;\n\n\t/**\n\t * Updates entity with partial data (immutable - returns new instance)\n\t * @param data - Partial data to update\n\t * @param validate - Whether to validate after update (default: true)\n\t */\n\tpublic update<T extends BaseEntity>(this: T, data: Partial<T>): T {\n\t\tconst updated = Object.assign(Object.create(Object.getPrototypeOf(this)), this, data);\n\t\treturn updated;\n\t}\n\n\t/**\n\t * Creates a new entity instance from DTO with validation\n\t * @param cls - Entity class constructor\n\t * @param dto - DTO to create entity from\n\t * @param validate - Whether to validate after creation (default: true)\n\t */\n\tpublic static FromDto<T extends BaseEntity>(cls: ClassConstructor<T>, dto: Dto): T {\n\t\tconst instance = plainToInstance(cls, dto.toJSON());\n\t\treturn instance;\n\t}\n\n\t/**\n\t * Creates multiple entities from DTOs\n\t */\n\tpublic static FromDtos<T extends BaseEntity>(cls: ClassConstructor<T>, dtos: Dto[]): T[] {\n\t\treturn dtos.map((dto) => BaseEntity.FromDto(cls, dto));\n\t}\n}\n"]}
|
package/package.json
CHANGED
package/src/utils/BaseDto.ts
CHANGED
@@ -58,7 +58,7 @@ export abstract class Dto {
|
|
58
58
|
* @returns New instance of the DTO
|
59
59
|
* @throws ValidationError[] if validation fails and validate is true
|
60
60
|
*/
|
61
|
-
static
|
61
|
+
public static Create<T extends Dto>(cls: ClassConstructor<T>, data: Record<string, unknown>, options: ClassTransformOptions = {}): T {
|
62
62
|
const instance = plainToInstance(cls, data, {
|
63
63
|
...Dto.defaultTransformOptions,
|
64
64
|
...options,
|
@@ -74,7 +74,7 @@ export abstract class Dto {
|
|
74
74
|
* @param options - Class transformer options for controlling exposure and transformation
|
75
75
|
* @returns Array of DTO instances
|
76
76
|
*/
|
77
|
-
static
|
78
|
-
return dataArray.map((data) => Dto.
|
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));
|
79
79
|
}
|
80
80
|
}
|
package/src/utils/BaseEntity.ts
CHANGED
@@ -14,13 +14,23 @@ export default abstract class BaseEntity {
|
|
14
14
|
*/
|
15
15
|
public abstract toDto(): Dto;
|
16
16
|
|
17
|
+
/**
|
18
|
+
* Updates entity with partial data (immutable - returns new instance)
|
19
|
+
* @param data - Partial data to update
|
20
|
+
* @param validate - Whether to validate after update (default: true)
|
21
|
+
*/
|
22
|
+
public update<T extends BaseEntity>(this: T, data: Partial<T>): T {
|
23
|
+
const updated = Object.assign(Object.create(Object.getPrototypeOf(this)), this, data);
|
24
|
+
return updated;
|
25
|
+
}
|
26
|
+
|
17
27
|
/**
|
18
28
|
* Creates a new entity instance from DTO with validation
|
19
29
|
* @param cls - Entity class constructor
|
20
30
|
* @param dto - DTO to create entity from
|
21
31
|
* @param validate - Whether to validate after creation (default: true)
|
22
32
|
*/
|
23
|
-
static
|
33
|
+
public static FromDto<T extends BaseEntity>(cls: ClassConstructor<T>, dto: Dto): T {
|
24
34
|
const instance = plainToInstance(cls, dto.toJSON());
|
25
35
|
return instance;
|
26
36
|
}
|
@@ -28,17 +38,7 @@ export default abstract class BaseEntity {
|
|
28
38
|
/**
|
29
39
|
* Creates multiple entities from DTOs
|
30
40
|
*/
|
31
|
-
static
|
32
|
-
return dtos.map((dto) => BaseEntity.
|
33
|
-
}
|
34
|
-
|
35
|
-
/**
|
36
|
-
* Updates entity with partial data (immutable - returns new instance)
|
37
|
-
* @param data - Partial data to update
|
38
|
-
* @param validate - Whether to validate after update (default: true)
|
39
|
-
*/
|
40
|
-
public update<T extends BaseEntity>(this: T, data: Partial<T>): T {
|
41
|
-
const updated = Object.assign(Object.create(Object.getPrototypeOf(this)), this, data);
|
42
|
-
return updated;
|
41
|
+
public static FromDtos<T extends BaseEntity>(cls: ClassConstructor<T>, dtos: Dto[]): T[] {
|
42
|
+
return dtos.map((dto) => BaseEntity.FromDto(cls, dto));
|
43
43
|
}
|
44
44
|
}
|