topsyde-utils 1.0.196 → 1.0.198
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/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/utils/BaseDto.d.ts +39 -0
- package/dist/utils/BaseDto.js +72 -0
- package/dist/utils/BaseDto.js.map +1 -0
- package/dist/utils/BaseEntity.d.ts +29 -0
- package/dist/utils/BaseEntity.js +35 -0
- package/dist/utils/BaseEntity.js.map +1 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.js +3 -0
- package/dist/utils/index.js.map +1 -1
- package/package.json +3 -1
- package/src/index.ts +3 -0
- package/src/utils/BaseDto.ts +80 -0
- package/src/utils/BaseEntity.ts +44 -0
- package/src/utils/index.ts +3 -0
package/dist/index.d.ts
CHANGED
@@ -25,7 +25,9 @@ export * from "./server/bun/websocket/websocket.types";
|
|
25
25
|
export * from "./utils/Rxjs";
|
26
26
|
export * from "./utils/Lib";
|
27
27
|
export * from "./utils/Guards";
|
28
|
+
export * from "./utils/BaseEntity";
|
28
29
|
export * from "./utils/Console";
|
30
|
+
export * from "./utils/BaseDto";
|
29
31
|
export { default as Singleton } from "./singleton";
|
30
32
|
export { default as Initializable } from "./initializable";
|
31
33
|
export { default as Application } from "./application";
|
@@ -42,6 +44,7 @@ export { default as Channel } from "./server/bun/websocket/Channel";
|
|
42
44
|
export { default as Client } from "./server/bun/websocket/Client";
|
43
45
|
export { default as Lib } from "./utils/Lib";
|
44
46
|
export { default as Guards } from "./utils/Guards";
|
47
|
+
export { default as BaseEntity } from "./utils/BaseEntity";
|
45
48
|
export { default as Console } from "./utils/Console";
|
46
49
|
export { ERROR_CODE, HTTP_ERROR_CODE, WS_ERROR_CODE } from "./errors";
|
47
50
|
export { InitializableOptions, InitializableEvent } from "./initializable";
|
package/dist/index.js
CHANGED
@@ -28,7 +28,9 @@ export * from "./server/bun/websocket/websocket.types.js";
|
|
28
28
|
export * from "./utils/Rxjs.js";
|
29
29
|
export * from "./utils/Lib.js";
|
30
30
|
export * from "./utils/Guards.js";
|
31
|
+
export * from "./utils/BaseEntity.js";
|
31
32
|
export * from "./utils/Console.js";
|
33
|
+
export * from "./utils/BaseDto.js";
|
32
34
|
// Export default classes
|
33
35
|
export { default as Singleton } from "./singleton.js";
|
34
36
|
export { default as Initializable } from "./initializable.js";
|
@@ -46,6 +48,7 @@ export { default as Channel } from "./server/bun/websocket/Channel.js";
|
|
46
48
|
export { default as Client } from "./server/bun/websocket/Client.js";
|
47
49
|
export { default as Lib } from "./utils/Lib.js";
|
48
50
|
export { default as Guards } from "./utils/Guards.js";
|
51
|
+
export { default as BaseEntity } from "./utils/BaseEntity.js";
|
49
52
|
export { default as Console } from "./utils/Console.js";
|
50
53
|
// Re-export specific items for backward compatibility
|
51
54
|
export { ERROR_CODE, HTTP_ERROR_CODE, WS_ERROR_CODE } from "./errors.js";
|
package/dist/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,6DAA6D;AAC7D,iCAAiC;AAEjC,qBAAqB;AACrB,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,iBAAiB,CAAC;AAChC,cAAc,eAAe,CAAC;AAC9B,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,SAAS,CAAC;AACxB,cAAc,uBAAuB,CAAC;AACtC,cAAc,8CAA8C,CAAC;AAC7D,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC;AACjC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,0CAA0C,CAAC;AACzD,cAAc,4BAA4B,CAAC;AAC3C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,qCAAqC,CAAC;AACpD,cAAc,kCAAkC,CAAC;AACjD,cAAc,yCAAy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MAAM,EAAE,MAAM,gBAAgB,CAAC;AACnD,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAErD,sDAAsD;AACtD,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAEtE,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAC;AACrF,OAAO,EAAE,sBAAsB,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAEzE,OAAO,EAAE,IAAI,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAK/C,OAAO,EAAE,sBAAsB,EAAE,0BAA0B,EAAE,MAAM,wCAAwC,CAAC;AAmB5G,OAAO,EAAE,aAAa,EAAiC,YAAY,EAAE,eAAe,EAAE,aAAa,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC","sourcesContent":["// This file is auto-generated by scripts/generate-indexes.ts\n// Do not edit this file directly\n\n// Export all modules\nexport * from \"./errors\";\nexport * from \"./singleton\";\nexport * from \"./initializable\";\nexport * from \"./application\";\nexport * from \"./consts\";\nexport * from \"./types\";\nexport * from \"./throwable\";\nexport * from \"./enums\";\nexport * from \"./client/rxjs/useRxjs\";\nexport * from \"./client/vite/plugins/topsydeUtilsVitePlugin\";\nexport * from \"./server/controller\";\nexport * from \"./server/service\";\nexport * from \"./server/base/base.database\";\nexport * from \"./server/bun/router/controller-discovery\";\nexport * from \"./server/bun/router/routes\";\nexport * from \"./server/bun/router/router\";\nexport * from \"./server/bun/router/router.internal\";\nexport * from \"./server/bun/websocket/Websocket\";\nexport * from \"./server/bun/websocket/websocket.guards\";\nexport * from \"./server/bun/websocket/Message\";\nexport * from \"./server/bun/websocket/Channel\";\nexport * from \"./server/bun/websocket/Client\";\nexport * from \"./server/bun/websocket/websocket.enums\";\nexport * from \"./server/bun/websocket/websocket.types\";\nexport * from \"./utils/Rxjs\";\nexport * from \"./utils/Lib\";\nexport * from \"./utils/Guards\";\nexport * from \"./utils/Console\";\n\n// Export default classes\nexport { default as Singleton } from \"./singleton\";\nexport { default as Initializable } from \"./initializable\";\nexport { default as Application } from \"./application\";\nexport { default as Throwable } from \"./throwable\";\nexport { default as TopsydeUtilsVitePlugin } from \"./client/vite/plugins/topsydeUtilsVitePlugin\";\nexport { default as Controller } from \"./server/controller\";\nexport { default as Service } from \"./server/service\";\nexport { default as Database } from \"./server/base/base.database\";\nexport { default as Router } from \"./server/bun/router/router\";\nexport { default as Router_Internal } from \"./server/bun/router/router.internal\";\nexport { default as Websocket } from \"./server/bun/websocket/Websocket\";\nexport { default as Message } from \"./server/bun/websocket/Message\";\nexport { default as Channel } from \"./server/bun/websocket/Channel\";\nexport { default as Client } from \"./server/bun/websocket/Client\";\nexport { default as Lib } from \"./utils/Lib\";\nexport { default as Guards } from \"./utils/Guards\";\nexport { default as Console } from \"./utils/Console\";\n\n// Re-export specific items for backward compatibility\nexport { ERROR_CODE, HTTP_ERROR_CODE, WS_ERROR_CODE } from \"./errors\";\nexport { InitializableOptions, InitializableEvent } from \"./initializable\";\nexport { RESPONSE_INIT, HEADERS_INIT, RESPONSE_METHOD_OPTIONS } from \"./application\";\nexport { DEFAULT_FALSE_RESPONSE, LOG_COLORS, LOG_ICONS } from \"./consts\";\nexport { ClassConstructor, NonNullableType, ObjectKeys, KVObj, I_ApplicationResponse } from \"./types\";\nexport { E_IS, E_ENVIRONMENTS } from \"./enums\";\nexport { RxjsDataType, NamespaceActions, MultiNamespaceActions } from \"./client/rxjs/useRxjs\";\nexport { ControllerResponse, ControllerAction, ControllerMap, ControllerOptions } from \"./server/controller\";\nexport { Routes } from \"./server/bun/router/routes\";\nexport { WebsocketConstructorOptions, I_WebsocketConstructor } from \"./server/bun/websocket/Websocket\";\nexport { E_WebsocketMessageType, E_WebsocketMessagePriority } from \"./server/bun/websocket/websocket.enums\";\nexport {\n\tBunWebsocketMessage,\n\tWebsocketChannel,\n\tWebsocketClients,\n\tWebsocketMessageOptions,\n\tWebsocketMessage,\n\tWebsocketStructuredMessage,\n\tWebsocketEntityId,\n\tWebsocketEntityName,\n\tWebsocketEntityData,\n\tI_WebsocketEntity,\n\tI_WebsocketClient,\n\tI_WebsocketChannelEntity,\n\tBroadcastOptions,\n\tI_WebsocketChannel,\n\tWebsocketInterfaceHandlers,\n\tI_WebsocketInterface,\n} from \"./server/bun/websocket/websocket.types\";\nexport { E_SUBJET_TYPE, I_RxjsPayload, RxjsNamespaces, AsyncSubject, BehaviorSubject, ReplaySubject, Subject, Subscription } from \"./utils/Rxjs\";\n"]}
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,6DAA6D;AAC7D,iCAAiC;AAEjC,qBAAqB;AACrB,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,iBAAiB,CAAC;AAChC,cAAc,eAAe,CAAC;AAC9B,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,SAAS,CAAC;AACxB,cAAc,uBAAuB,CAAC;AACtC,cAAc,8CAA8C,CAAC;AAC7D,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC;AACjC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,0CAA0C,CAAC;AACzD,cAAc,4BAA4B,CAAC;AAC3C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,qCAAqC,CAAC;AACpD,cAAc,kCAAkC,CAAC;AACjD,cAAc,yCAAyC,CAAC;AACxD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,wCAAwC,CAAC;AACvD,cAAc,wCAAwC,CAAC;AACvD,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,oBAAoB,CAAC;AACnC,cAA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Ae,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAEtE,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAC;AACrF,OAAO,EAAE,sBAAsB,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAEzE,OAAO,EAAE,IAAI,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAK/C,OAAO,EAAE,sBAAsB,EAAE,0BAA0B,EAAE,MAAM,wCAAwC,CAAC;AAmB5G,OAAO,EAAE,aAAa,EAAiC,YAAY,EAAE,eAAe,EAAE,aAAa,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC","sourcesContent":["// This file is auto-generated by scripts/generate-indexes.ts\n// Do not edit this file directly\n\n// Export all modules\nexport * from \"./errors\";\nexport * from \"./singleton\";\nexport * from \"./initializable\";\nexport * from \"./application\";\nexport * from \"./consts\";\nexport * from \"./types\";\nexport * from \"./throwable\";\nexport * from \"./enums\";\nexport * from \"./client/rxjs/useRxjs\";\nexport * from \"./client/vite/plugins/topsydeUtilsVitePlugin\";\nexport * from \"./server/controller\";\nexport * from \"./server/service\";\nexport * from \"./server/base/base.database\";\nexport * from \"./server/bun/router/controller-discovery\";\nexport * from \"./server/bun/router/routes\";\nexport * from \"./server/bun/router/router\";\nexport * from \"./server/bun/router/router.internal\";\nexport * from \"./server/bun/websocket/Websocket\";\nexport * from \"./server/bun/websocket/websocket.guards\";\nexport * from \"./server/bun/websocket/Message\";\nexport * from \"./server/bun/websocket/Channel\";\nexport * from \"./server/bun/websocket/Client\";\nexport * from \"./server/bun/websocket/websocket.enums\";\nexport * from \"./server/bun/websocket/websocket.types\";\nexport * from \"./utils/Rxjs\";\nexport * from \"./utils/Lib\";\nexport * from \"./utils/Guards\";\nexport * from \"./utils/BaseEntity\";\nexport * from \"./utils/Console\";\nexport * from \"./utils/BaseDto\";\n\n// Export default classes\nexport { default as Singleton } from \"./singleton\";\nexport { default as Initializable } from \"./initializable\";\nexport { default as Application } from \"./application\";\nexport { default as Throwable } from \"./throwable\";\nexport { default as TopsydeUtilsVitePlugin } from \"./client/vite/plugins/topsydeUtilsVitePlugin\";\nexport { default as Controller } from \"./server/controller\";\nexport { default as Service } from \"./server/service\";\nexport { default as Database } from \"./server/base/base.database\";\nexport { default as Router } from \"./server/bun/router/router\";\nexport { default as Router_Internal } from \"./server/bun/router/router.internal\";\nexport { default as Websocket } from \"./server/bun/websocket/Websocket\";\nexport { default as Message } from \"./server/bun/websocket/Message\";\nexport { default as Channel } from \"./server/bun/websocket/Channel\";\nexport { default as Client } from \"./server/bun/websocket/Client\";\nexport { default as Lib } from \"./utils/Lib\";\nexport { default as Guards } from \"./utils/Guards\";\nexport { default as BaseEntity } from \"./utils/BaseEntity\";\nexport { default as Console } from \"./utils/Console\";\n\n// Re-export specific items for backward compatibility\nexport { ERROR_CODE, HTTP_ERROR_CODE, WS_ERROR_CODE } from \"./errors\";\nexport { InitializableOptions, InitializableEvent } from \"./initializable\";\nexport { RESPONSE_INIT, HEADERS_INIT, RESPONSE_METHOD_OPTIONS } from \"./application\";\nexport { DEFAULT_FALSE_RESPONSE, LOG_COLORS, LOG_ICONS } from \"./consts\";\nexport { ClassConstructor, NonNullableType, ObjectKeys, KVObj, I_ApplicationResponse } from \"./types\";\nexport { E_IS, E_ENVIRONMENTS } from \"./enums\";\nexport { RxjsDataType, NamespaceActions, MultiNamespaceActions } from \"./client/rxjs/useRxjs\";\nexport { ControllerResponse, ControllerAction, ControllerMap, ControllerOptions } from \"./server/controller\";\nexport { Routes } from \"./server/bun/router/routes\";\nexport { WebsocketConstructorOptions, I_WebsocketConstructor } from \"./server/bun/websocket/Websocket\";\nexport { E_WebsocketMessageType, E_WebsocketMessagePriority } from \"./server/bun/websocket/websocket.enums\";\nexport {\n\tBunWebsocketMessage,\n\tWebsocketChannel,\n\tWebsocketClients,\n\tWebsocketMessageOptions,\n\tWebsocketMessage,\n\tWebsocketStructuredMessage,\n\tWebsocketEntityId,\n\tWebsocketEntityName,\n\tWebsocketEntityData,\n\tI_WebsocketEntity,\n\tI_WebsocketClient,\n\tI_WebsocketChannelEntity,\n\tBroadcastOptions,\n\tI_WebsocketChannel,\n\tWebsocketInterfaceHandlers,\n\tI_WebsocketInterface,\n} from \"./server/bun/websocket/websocket.types\";\nexport { E_SUBJET_TYPE, I_RxjsPayload, RxjsNamespaces, AsyncSubject, BehaviorSubject, ReplaySubject, Subject, Subscription } from \"./utils/Rxjs\";\n"]}
|
@@ -0,0 +1,39 @@
|
|
1
|
+
import type { ClassConstructor, ClassTransformOptions } from "class-transformer";
|
2
|
+
import { ValidationError } from "class-validator";
|
3
|
+
/**
|
4
|
+
* Base typesafe class for Data Transfer Objects (DTOs)
|
5
|
+
*/
|
6
|
+
export declare abstract class Dto {
|
7
|
+
/**
|
8
|
+
* Default options for class transformation
|
9
|
+
*/
|
10
|
+
protected static readonly defaultTransformOptions: ClassTransformOptions;
|
11
|
+
/**
|
12
|
+
* Validates the DTO instance
|
13
|
+
* @throws ValidationError[] if validation fails
|
14
|
+
*/
|
15
|
+
validate(): ValidationError[];
|
16
|
+
/**
|
17
|
+
* Converts the DTO to a plain object
|
18
|
+
* @param options - Class transformer options for controlling exposure and transformation
|
19
|
+
* @returns Plain object representation of the DTO
|
20
|
+
*/
|
21
|
+
toJSON<T = Record<string, unknown>>(include_undefined?: boolean, options?: ClassTransformOptions): T;
|
22
|
+
/**
|
23
|
+
* Creates a new instance of the DTO with validation
|
24
|
+
* @param cls - The class constructor to create an instance from
|
25
|
+
* @param data - Data to create the DTO from
|
26
|
+
* @param options - Class transformer options for controlling exposure and transformation
|
27
|
+
* @returns New instance of the DTO
|
28
|
+
* @throws ValidationError[] if validation fails and validate is true
|
29
|
+
*/
|
30
|
+
static create<T extends Dto, D extends Record<string, unknown>>(cls: ClassConstructor<T>, data: D, options?: ClassTransformOptions): T;
|
31
|
+
/**
|
32
|
+
* 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
|
+
*/
|
38
|
+
static createMany<T extends Dto, D extends Record<string, unknown>>(cls: ClassConstructor<T>, dataArray: D[], options?: ClassTransformOptions): T[];
|
39
|
+
}
|
@@ -0,0 +1,72 @@
|
|
1
|
+
import { instanceToPlain, plainToInstance } from "class-transformer";
|
2
|
+
import Guards from "./Guards.js";
|
3
|
+
import { validateSync } from "class-validator";
|
4
|
+
/**
|
5
|
+
* Base typesafe class for Data Transfer Objects (DTOs)
|
6
|
+
*/
|
7
|
+
export class Dto {
|
8
|
+
/**
|
9
|
+
* Validates the DTO instance
|
10
|
+
* @throws ValidationError[] if validation fails
|
11
|
+
*/
|
12
|
+
validate() {
|
13
|
+
const errors = validateSync(this, {
|
14
|
+
validationError: { target: false },
|
15
|
+
forbidUnknownValues: true,
|
16
|
+
});
|
17
|
+
if (errors.length > 0) {
|
18
|
+
throw errors;
|
19
|
+
}
|
20
|
+
return errors;
|
21
|
+
}
|
22
|
+
/**
|
23
|
+
* Converts the DTO to a plain object
|
24
|
+
* @param options - Class transformer options for controlling exposure and transformation
|
25
|
+
* @returns Plain object representation of the DTO
|
26
|
+
*/
|
27
|
+
toJSON(include_undefined = true, options) {
|
28
|
+
const value = instanceToPlain(this, {
|
29
|
+
...Dto.defaultTransformOptions,
|
30
|
+
...options,
|
31
|
+
});
|
32
|
+
if (!include_undefined) {
|
33
|
+
return Object.fromEntries(Object.entries(value).filter(([_, v]) => !Guards.IsNil(v)));
|
34
|
+
}
|
35
|
+
return value;
|
36
|
+
}
|
37
|
+
/**
|
38
|
+
* Creates a new instance of the DTO with validation
|
39
|
+
* @param cls - The class constructor to create an instance from
|
40
|
+
* @param data - Data to create the DTO from
|
41
|
+
* @param options - Class transformer options for controlling exposure and transformation
|
42
|
+
* @returns New instance of the DTO
|
43
|
+
* @throws ValidationError[] if validation fails and validate is true
|
44
|
+
*/
|
45
|
+
static create(cls, data, options = {}) {
|
46
|
+
const instance = plainToInstance(cls, data, {
|
47
|
+
...Dto.defaultTransformOptions,
|
48
|
+
...options,
|
49
|
+
});
|
50
|
+
return instance;
|
51
|
+
}
|
52
|
+
/**
|
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
|
+
*/
|
59
|
+
static createMany(cls, dataArray, options = {}) {
|
60
|
+
return dataArray.map((data) => Dto.create(cls, data, options));
|
61
|
+
}
|
62
|
+
}
|
63
|
+
/**
|
64
|
+
* Default options for class transformation
|
65
|
+
*/
|
66
|
+
Dto.defaultTransformOptions = {
|
67
|
+
excludeExtraneousValues: true,
|
68
|
+
enableCircularCheck: true,
|
69
|
+
exposeDefaultValues: true,
|
70
|
+
enableImplicitConversion: false, // Safer default, especially when using class-validator
|
71
|
+
};
|
72
|
+
//# sourceMappingURL=BaseDto.js.map
|
@@ -0,0 +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;Q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ain, 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\tstatic create<T extends Dto, D extends Record<string, unknown>>(cls: ClassConstructor<T>, data: D, 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\tstatic createMany<T extends Dto, D extends Record<string, unknown>>(cls: ClassConstructor<T>, dataArray: D[], options: ClassTransformOptions = {}): T[] {\n\t\treturn dataArray.map((data) => Dto.create(cls, data, options));\n\t}\n}\n"]}
|
@@ -0,0 +1,29 @@
|
|
1
|
+
import { ClassConstructor } from "class-transformer";
|
2
|
+
import { Dto } from "./BaseDto";
|
3
|
+
export default abstract class BaseEntity {
|
4
|
+
/**
|
5
|
+
* Converts entity to plain object
|
6
|
+
*/
|
7
|
+
toJSON<T = Record<string, unknown>>(): T;
|
8
|
+
/**
|
9
|
+
* Abstract method - entities must define how to convert to DTO
|
10
|
+
*/
|
11
|
+
abstract toDto(): Dto;
|
12
|
+
/**
|
13
|
+
* Creates a new entity instance from DTO with validation
|
14
|
+
* @param cls - Entity class constructor
|
15
|
+
* @param dto - DTO to create entity from
|
16
|
+
* @param validate - Whether to validate after creation (default: true)
|
17
|
+
*/
|
18
|
+
static fromDto<T extends BaseEntity>(cls: ClassConstructor<T>, dto: Dto): T;
|
19
|
+
/**
|
20
|
+
* Creates multiple entities from DTOs
|
21
|
+
*/
|
22
|
+
static fromDtos<T extends BaseEntity>(cls: ClassConstructor<T>, dtos: Dto[]): T[];
|
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;
|
29
|
+
}
|
@@ -0,0 +1,35 @@
|
|
1
|
+
import { instanceToPlain, plainToInstance } from "class-transformer";
|
2
|
+
export default class BaseEntity {
|
3
|
+
/**
|
4
|
+
* Converts entity to plain object
|
5
|
+
*/
|
6
|
+
toJSON() {
|
7
|
+
return instanceToPlain(this);
|
8
|
+
}
|
9
|
+
/**
|
10
|
+
* Creates a new entity instance from DTO with validation
|
11
|
+
* @param cls - Entity class constructor
|
12
|
+
* @param dto - DTO to create entity from
|
13
|
+
* @param validate - Whether to validate after creation (default: true)
|
14
|
+
*/
|
15
|
+
static fromDto(cls, dto) {
|
16
|
+
const instance = plainToInstance(cls, dto.toJSON());
|
17
|
+
return instance;
|
18
|
+
}
|
19
|
+
/**
|
20
|
+
* Creates multiple entities from DTOs
|
21
|
+
*/
|
22
|
+
static fromDtos(cls, dtos) {
|
23
|
+
return dtos.map((dto) => BaseEntity.fromDto(cls, dto));
|
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;
|
33
|
+
}
|
34
|
+
}
|
35
|
+
//# sourceMappingURL=BaseEntity.js.map
|
@@ -0,0 +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;;;;;OAKG;IACH,MAAM,CAAC,OAAO,CAAuB,GAAwB,EAAE,GAAQ;QACtE,MAAM,QAAQ,GAAG,eAAe,CAAC,GAAG,EAAE,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC;QACpD,OAAO,QAAQ,CAAC;IACjB,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,QAAQ,CAAuB,GAAwB,EAAE,IAAW;QAC1E,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;IACxD,CAAC;IAED;;;;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;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 * 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\tstatic 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\tstatic fromDtos<T extends BaseEntity>(cls: ClassConstructor<T>, dtos: Dto[]): T[] {\n\t\treturn dtos.map((dto) => BaseEntity.fromDto(cls, dto));\n\t}\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"]}
|
package/dist/utils/index.d.ts
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
export * from './Rxjs';
|
2
2
|
export * from './Lib';
|
3
3
|
export * from './Guards';
|
4
|
+
export * from './BaseEntity';
|
4
5
|
export * from './Console';
|
6
|
+
export * from './BaseDto';
|
5
7
|
export { default as Lib } from './Lib';
|
6
8
|
export { default as Guards } from './Guards';
|
9
|
+
export { default as BaseEntity } from './BaseEntity';
|
7
10
|
export { default as Console } from './Console';
|
package/dist/utils/index.js
CHANGED
@@ -3,8 +3,11 @@
|
|
3
3
|
export * from './Rxjs.js';
|
4
4
|
export * from './Lib.js';
|
5
5
|
export * from './Guards.js';
|
6
|
+
export * from './BaseEntity.js';
|
6
7
|
export * from './Console.js';
|
8
|
+
export * from './BaseDto.js';
|
7
9
|
export { default as Lib } from './Lib.js';
|
8
10
|
export { default as Guards } from './Guards.js';
|
11
|
+
export { default as BaseEntity } from './BaseEntity.js';
|
9
12
|
export { default as Console } from './Console.js';
|
10
13
|
//# sourceMappingURL=index.js.map
|
package/dist/utils/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,6DAA6D;AAC7D,iCAAiC;AAEjC,cAAc,QAAQ,CAAC;AACvB,cAAc,OAAO,CAAC;AACtB,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,OAAO,EAAE,OAAO,IAAI,GAAG,EAAE,MAAM,OAAO,CAAC;AACvC,OAAO,EAAE,OAAO,IAAI,MAAM,EAAE,MAAM,UAAU,CAAC;AAC7C,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,WAAW,CAAC","sourcesContent":["// This file is auto-generated by scripts/generate-indexes.ts\n// Do not edit this file directly\n\nexport * from './Rxjs';\nexport * from './Lib';\nexport * from './Guards';\nexport * from './Console';\nexport { default as Lib } from './Lib';\nexport { default as Guards } from './Guards';\nexport { default as Console } from './Console';\n"]}
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,6DAA6D;AAC7D,iCAAiC;AAEjC,cAAc,QAAQ,CAAC;AACvB,cAAc,OAAO,CAAC;AACtB,cAAc,UAAU,CAAC;AACzB,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,OAAO,EAAE,OAAO,IAAI,GAAG,EAAE,MAAM,OAAO,CAAC;AACvC,OAAO,EAAE,OAAO,IAAI,MAAM,EAAE,MAAM,UAAU,CAAC;AAC7C,OAAO,EAAE,OAAO,IAAI,UAAU,EAAE,MAAM,cAAc,CAAC;AACrD,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,WAAW,CAAC","sourcesContent":["// This file is auto-generated by scripts/generate-indexes.ts\n// Do not edit this file directly\n\nexport * from './Rxjs';\nexport * from './Lib';\nexport * from './Guards';\nexport * from './BaseEntity';\nexport * from './Console';\nexport * from './BaseDto';\nexport { default as Lib } from './Lib';\nexport { default as Guards } from './Guards';\nexport { default as BaseEntity } from './BaseEntity';\nexport { default as Console } from './Console';\n"]}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "topsyde-utils",
|
3
|
-
"version": "1.0.
|
3
|
+
"version": "1.0.198",
|
4
4
|
"description": "A bundle of TypeScript utility classes and functions",
|
5
5
|
"type": "module",
|
6
6
|
"main": "dist/index.js",
|
@@ -110,6 +110,8 @@
|
|
110
110
|
"node": ">=14.0.0"
|
111
111
|
},
|
112
112
|
"dependencies": {
|
113
|
+
"class-transformer": "^0.5.1",
|
114
|
+
"class-validator": "^0.14.2",
|
113
115
|
"rxjs": "^7.8.2",
|
114
116
|
"vite": "^6.2.0",
|
115
117
|
"vue": "^3.5.13"
|
package/src/index.ts
CHANGED
@@ -29,7 +29,9 @@ export * from "./server/bun/websocket/websocket.types";
|
|
29
29
|
export * from "./utils/Rxjs";
|
30
30
|
export * from "./utils/Lib";
|
31
31
|
export * from "./utils/Guards";
|
32
|
+
export * from "./utils/BaseEntity";
|
32
33
|
export * from "./utils/Console";
|
34
|
+
export * from "./utils/BaseDto";
|
33
35
|
|
34
36
|
// Export default classes
|
35
37
|
export { default as Singleton } from "./singleton";
|
@@ -48,6 +50,7 @@ export { default as Channel } from "./server/bun/websocket/Channel";
|
|
48
50
|
export { default as Client } from "./server/bun/websocket/Client";
|
49
51
|
export { default as Lib } from "./utils/Lib";
|
50
52
|
export { default as Guards } from "./utils/Guards";
|
53
|
+
export { default as BaseEntity } from "./utils/BaseEntity";
|
51
54
|
export { default as Console } from "./utils/Console";
|
52
55
|
|
53
56
|
// Re-export specific items for backward compatibility
|
@@ -0,0 +1,80 @@
|
|
1
|
+
import type { ClassConstructor, ClassTransformOptions } from "class-transformer";
|
2
|
+
import { instanceToPlain, plainToInstance } from "class-transformer";
|
3
|
+
import Guards from "./Guards";
|
4
|
+
import { ValidationError, validateSync } from "class-validator";
|
5
|
+
|
6
|
+
/**
|
7
|
+
* Base typesafe class for Data Transfer Objects (DTOs)
|
8
|
+
*/
|
9
|
+
export abstract class Dto {
|
10
|
+
/**
|
11
|
+
* Default options for class transformation
|
12
|
+
*/
|
13
|
+
protected static readonly defaultTransformOptions: ClassTransformOptions = {
|
14
|
+
excludeExtraneousValues: true,
|
15
|
+
enableCircularCheck: true,
|
16
|
+
exposeDefaultValues: true,
|
17
|
+
enableImplicitConversion: false, // Safer default, especially when using class-validator
|
18
|
+
};
|
19
|
+
|
20
|
+
/**
|
21
|
+
* Validates the DTO instance
|
22
|
+
* @throws ValidationError[] if validation fails
|
23
|
+
*/
|
24
|
+
public validate(): ValidationError[] {
|
25
|
+
const errors = validateSync(this, {
|
26
|
+
validationError: { target: false },
|
27
|
+
forbidUnknownValues: true,
|
28
|
+
});
|
29
|
+
if (errors.length > 0) {
|
30
|
+
throw errors;
|
31
|
+
}
|
32
|
+
return errors;
|
33
|
+
}
|
34
|
+
|
35
|
+
/**
|
36
|
+
* Converts the DTO to a plain object
|
37
|
+
* @param options - Class transformer options for controlling exposure and transformation
|
38
|
+
* @returns Plain object representation of the DTO
|
39
|
+
*/
|
40
|
+
public toJSON<T = Record<string, unknown>>(include_undefined: boolean = true, options?: ClassTransformOptions): T {
|
41
|
+
const value = instanceToPlain(this, {
|
42
|
+
...Dto.defaultTransformOptions,
|
43
|
+
...options,
|
44
|
+
}) as T;
|
45
|
+
|
46
|
+
if (!include_undefined) {
|
47
|
+
return Object.fromEntries(Object.entries(value as Record<string, unknown>).filter(([_, v]) => !Guards.IsNil(v))) as T;
|
48
|
+
}
|
49
|
+
|
50
|
+
return value;
|
51
|
+
}
|
52
|
+
|
53
|
+
/**
|
54
|
+
* Creates a new instance of the DTO with validation
|
55
|
+
* @param cls - The class constructor to create an instance from
|
56
|
+
* @param data - Data to create the DTO from
|
57
|
+
* @param options - Class transformer options for controlling exposure and transformation
|
58
|
+
* @returns New instance of the DTO
|
59
|
+
* @throws ValidationError[] if validation fails and validate is true
|
60
|
+
*/
|
61
|
+
static create<T extends Dto, D extends Record<string, unknown>>(cls: ClassConstructor<T>, data: D, options: ClassTransformOptions = {}): T {
|
62
|
+
const instance = plainToInstance(cls, data, {
|
63
|
+
...Dto.defaultTransformOptions,
|
64
|
+
...options,
|
65
|
+
});
|
66
|
+
|
67
|
+
return instance;
|
68
|
+
}
|
69
|
+
|
70
|
+
/**
|
71
|
+
* 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
|
+
*/
|
77
|
+
static createMany<T extends Dto, D extends Record<string, unknown>>(cls: ClassConstructor<T>, dataArray: D[], options: ClassTransformOptions = {}): T[] {
|
78
|
+
return dataArray.map((data) => Dto.create(cls, data, options));
|
79
|
+
}
|
80
|
+
}
|
@@ -0,0 +1,44 @@
|
|
1
|
+
import { ClassConstructor, instanceToPlain, plainToInstance } from "class-transformer";
|
2
|
+
import { Dto } from "./BaseDto";
|
3
|
+
|
4
|
+
export default abstract class BaseEntity {
|
5
|
+
/**
|
6
|
+
* Converts entity to plain object
|
7
|
+
*/
|
8
|
+
public toJSON<T = Record<string, unknown>>(): T {
|
9
|
+
return instanceToPlain(this) as T;
|
10
|
+
}
|
11
|
+
|
12
|
+
/**
|
13
|
+
* Abstract method - entities must define how to convert to DTO
|
14
|
+
*/
|
15
|
+
public abstract toDto(): Dto;
|
16
|
+
|
17
|
+
/**
|
18
|
+
* Creates a new entity instance from DTO with validation
|
19
|
+
* @param cls - Entity class constructor
|
20
|
+
* @param dto - DTO to create entity from
|
21
|
+
* @param validate - Whether to validate after creation (default: true)
|
22
|
+
*/
|
23
|
+
static fromDto<T extends BaseEntity>(cls: ClassConstructor<T>, dto: Dto): T {
|
24
|
+
const instance = plainToInstance(cls, dto.toJSON());
|
25
|
+
return instance;
|
26
|
+
}
|
27
|
+
|
28
|
+
/**
|
29
|
+
* Creates multiple entities from DTOs
|
30
|
+
*/
|
31
|
+
static fromDtos<T extends BaseEntity>(cls: ClassConstructor<T>, dtos: Dto[]): T[] {
|
32
|
+
return dtos.map((dto) => BaseEntity.fromDto(cls, dto));
|
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;
|
43
|
+
}
|
44
|
+
}
|
package/src/utils/index.ts
CHANGED
@@ -4,7 +4,10 @@
|
|
4
4
|
export * from './Rxjs';
|
5
5
|
export * from './Lib';
|
6
6
|
export * from './Guards';
|
7
|
+
export * from './BaseEntity';
|
7
8
|
export * from './Console';
|
9
|
+
export * from './BaseDto';
|
8
10
|
export { default as Lib } from './Lib';
|
9
11
|
export { default as Guards } from './Guards';
|
12
|
+
export { default as BaseEntity } from './BaseEntity';
|
10
13
|
export { default as Console } from './Console';
|