@forklaunch/core 0.1.0 → 0.1.2

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 (87) hide show
  1. package/.prettierignore +2 -0
  2. package/.prettierrc +6 -0
  3. package/dist/cache/index.d.ts +3 -0
  4. package/dist/cache/index.js +20 -0
  5. package/dist/cache/index.js.map +1 -0
  6. package/dist/cache/interfaces/ttlCache.interface.d.ts +1 -1
  7. package/dist/cache/redisTtlCache.d.ts +3 -2
  8. package/dist/cache/redisTtlCache.js +2 -2
  9. package/dist/cache/redisTtlCache.js.map +1 -1
  10. package/dist/cache/types/{ttlCacheRecord.js → ttlCacheRecord.types.js} +1 -1
  11. package/dist/cache/types/ttlCacheRecord.types.js.map +1 -0
  12. package/dist/controllers/index.d.ts +1 -0
  13. package/dist/controllers/index.js +18 -0
  14. package/dist/controllers/index.js.map +1 -0
  15. package/dist/database/index.d.ts +1 -0
  16. package/dist/database/index.js +18 -0
  17. package/dist/database/index.js.map +1 -0
  18. package/dist/database/mikro/models/entities/base.entity.js +1 -1
  19. package/dist/entityMapper/index.d.ts +2 -0
  20. package/dist/entityMapper/index.js +19 -0
  21. package/dist/entityMapper/index.js.map +1 -0
  22. package/dist/entityMapper/interfaces/entityMapper.interface.d.ts +3 -3
  23. package/dist/entityMapper/models/baseEntityMapper.model.d.ts +23 -23
  24. package/dist/entityMapper/models/baseEntityMapper.model.js +17 -17
  25. package/dist/entityMapper/models/baseEntityMapper.model.js.map +1 -1
  26. package/dist/entityMapper/models/requestEntityMapper.model.d.ts +17 -23
  27. package/dist/entityMapper/models/requestEntityMapper.model.js +13 -19
  28. package/dist/entityMapper/models/requestEntityMapper.model.js.map +1 -1
  29. package/dist/entityMapper/models/responseEntityMapper.model.d.ts +15 -16
  30. package/dist/entityMapper/models/responseEntityMapper.model.js +8 -8
  31. package/dist/entityMapper/models/responseEntityMapper.model.js.map +1 -1
  32. package/dist/entityMapper/types/entityMapper.types.d.ts +3 -22
  33. package/dist/http/index.d.ts +2 -0
  34. package/dist/http/index.js +19 -0
  35. package/dist/http/index.js.map +1 -0
  36. package/dist/http/middlewares/index.d.ts +2 -0
  37. package/dist/http/middlewares/index.js +19 -0
  38. package/dist/http/middlewares/index.js.map +1 -0
  39. package/dist/http/middlewares/request.middleware.d.ts +11 -11
  40. package/dist/http/middlewares/request.middleware.js +32 -31
  41. package/dist/http/middlewares/request.middleware.js.map +1 -1
  42. package/dist/http/middlewares/response.middleware.d.ts +3 -2
  43. package/dist/http/middlewares/response.middleware.js +9 -6
  44. package/dist/http/middlewares/response.middleware.js.map +1 -1
  45. package/dist/http/types/api.types.d.ts +16 -13
  46. package/dist/http/types/index.d.ts +2 -0
  47. package/dist/http/types/index.js +19 -0
  48. package/dist/http/types/index.js.map +1 -0
  49. package/dist/http/types/primitive.types.d.ts +14 -13
  50. package/dist/index.d.ts +6 -1
  51. package/dist/index.js +20 -5
  52. package/dist/index.js.map +1 -1
  53. package/dist/services/index.d.ts +1 -0
  54. package/dist/services/index.js +18 -0
  55. package/dist/services/index.js.map +1 -0
  56. package/dist/services/interfaces/baseService.d.ts +1 -1
  57. package/dist/tests/{dto.test.js → entityMapper.test.js} +10 -32
  58. package/dist/tests/entityMapper.test.js.map +1 -0
  59. package/dist/tests/http.middleware.test.d.ts +1 -0
  60. package/dist/tests/http.middleware.test.js +79 -0
  61. package/dist/tests/http.middleware.test.js.map +1 -0
  62. package/dist/tests/redisTtlCache.test.js +32 -15
  63. package/dist/tests/redisTtlCache.test.js.map +1 -1
  64. package/entityMapper/index.ts +2 -0
  65. package/entityMapper/interfaces/entityMapper.interface.ts +11 -11
  66. package/entityMapper/models/baseEntityMapper.model.ts +90 -73
  67. package/entityMapper/models/requestEntityMapper.model.ts +96 -76
  68. package/entityMapper/models/responseEntityMapper.model.ts +83 -62
  69. package/entityMapper/types/entityMapper.types.ts +6 -23
  70. package/http/index.ts +2 -0
  71. package/http/middlewares/index.ts +2 -0
  72. package/http/middlewares/request.middleware.ts +118 -62
  73. package/http/middlewares/response.middleware.ts +56 -30
  74. package/http/types/api.types.ts +43 -25
  75. package/http/types/index.ts +2 -0
  76. package/http/types/primitive.types.ts +65 -44
  77. package/index.ts +6 -5
  78. package/jest.config.ts +2 -2
  79. package/package.json +13 -4
  80. package/tests/entityMapper.test.ts +219 -0
  81. package/tests/http.middleware.test.ts +99 -0
  82. package/tests/redisTtlCache.test.ts +49 -29
  83. package/dist/cache/types/ttlCacheRecord.js.map +0 -1
  84. package/dist/tests/dto.test.js.map +0 -1
  85. package/tests/dto.test.ts +0 -235
  86. /package/dist/cache/types/{ttlCacheRecord.d.ts → ttlCacheRecord.types.d.ts} +0 -0
  87. /package/dist/tests/{dto.test.d.ts → entityMapper.test.d.ts} +0 -0
@@ -1,8 +1,7 @@
1
- import { SchemaValidator } from "@forklaunch/validator/interfaces";
2
- import { BaseEntity } from "../../database/mikro/models/entities/base.entity";
3
- import { EntityMapperConstructor } from "../interfaces/entityMapper.interface";
4
- import { EntityMapperStaticSchema } from "../types/entityMapper.types";
5
- import { BaseEntityMapper, construct } from "./baseEntityMapper.model";
1
+ import { AnySchemaValidator } from '@forklaunch/validator';
2
+ import { BaseEntity } from '../../database/mikro/models/entities/base.entity';
3
+ import { EntityMapperConstructor } from '../interfaces/entityMapper.interface';
4
+ import { BaseEntityMapper, construct } from './baseEntityMapper.model';
6
5
 
7
6
  /**
8
7
  * Abstract class representing a request entityMapper.
@@ -11,81 +10,102 @@ import { BaseEntityMapper, construct } from "./baseEntityMapper.model";
11
10
  * @template SV - A type that extends SchemaValidator.
12
11
  * @extends {BaseEntityMapper<SV>}
13
12
  */
14
- export abstract class RequestEntityMapper<Entity extends BaseEntity | unknown, SV extends SchemaValidator> extends BaseEntityMapper<SV> {
15
- /**
16
- * The entity.
17
- * @type {Entity}
18
- * @protected
19
- */
20
- _Entity!: Entity;
21
-
22
- /**
23
- * Creates an instance of RequestEntityMapper.
24
- *
25
- * @param {SV} schemaValidator - The schema provider.
26
- */
27
- constructor(schemaValidator: SV) {
28
- super(schemaValidator);
29
- }
13
+ export abstract class RequestEntityMapper<
14
+ Entity extends BaseEntity,
15
+ SV extends AnySchemaValidator
16
+ > extends BaseEntityMapper<SV> {
17
+ /**
18
+ * The entity.
19
+ * @type {Entity}
20
+ * @protected
21
+ */
22
+ _Entity!: Entity;
30
23
 
31
- /**
32
- * Converts the underlying DTO to an entity.
33
- *
34
- * @abstract
35
- * @param {...unknown[]} additionalArgs - Additional arguments.
36
- * @returns {Entity} - The entity.
37
- */
38
- abstract toEntity(...additionalArgs: unknown[]): Entity;
24
+ /**
25
+ * Converts the underlying DTO to an entity.
26
+ *
27
+ * @abstract
28
+ * @param {...unknown[]} additionalArgs - Additional arguments.
29
+ * @returns {Entity} - The entity.
30
+ */
31
+ abstract toEntity(...additionalArgs: unknown[]): Entity;
39
32
 
40
- /**
41
- * Populates the DTO with data from a JSON object.
42
- *
43
- * @param {this['_dto']} json - The JSON object.
44
- * @returns {this} - The instance of the RequestEntityMapper.
45
- */
46
- fromJson(json: this['_dto']): this {
47
- if (!this.schemaValidator.validate(this.schemaValidator.schemify(this.schema), json)) {
48
- throw new Error('Invalid DTO');
49
- }
50
- this.dto = json;
51
- return this;
33
+ /**
34
+ * Populates the DTO with data from a JSON object.
35
+ *
36
+ * @param {this['_dto']} json - The JSON object.
37
+ * @returns {this} - The instance of the RequestEntityMapper.
38
+ */
39
+ fromJson(json: this['_dto']): this {
40
+ if (
41
+ !this.schemaValidator.validate(
42
+ this.schemaValidator.schemify(this.schema),
43
+ json
44
+ )
45
+ ) {
46
+ throw new Error('Invalid DTO');
52
47
  }
48
+ this.dto = json;
49
+ return this;
50
+ }
53
51
 
54
- /**
55
- * Deserializes a JSON object to an entity.
56
- *
57
- * @param {this['_dto']} json - The JSON object.
58
- * @param {...unknown[]} additionalArgs - Additional arguments.
59
- * @returns {Entity} - The entity.
60
- */
61
- deserializeJsonToEntity(json: this['_dto'], ...additionalArgs: unknown[]): Entity {
62
- return this.fromJson(json).toEntity(...additionalArgs);
63
- }
52
+ /**
53
+ * Deserializes a JSON object to an entity.
54
+ *
55
+ * @param {this['_dto']} json - The JSON object.
56
+ * @param {...unknown[]} additionalArgs - Additional arguments.
57
+ * @returns {Entity} - The entity.
58
+ */
59
+ deserializeJsonToEntity(
60
+ json: this['_dto'],
61
+ ...additionalArgs: unknown[]
62
+ ): Entity {
63
+ return this.fromJson(json).toEntity(...additionalArgs);
64
+ }
64
65
 
65
- /**
66
- * Creates an instance of a RequestEntityMapper from a JSON object.
67
- *
68
- * @template EntityMapperType - A type that extends RequestEntityMapper.
69
- * @param {EntityMapperConstructor<EntityMapperType>} this - The constructor of the EntityMapperType.
70
- * @param {EntityMapperType['_SV']} schemaValidator - The schema provider.
71
- * @param {EntityMapperStaticSchema<EntityMapperType>} json - The JSON object.
72
- * @returns {EntityMapperType} - An instance of the EntityMapperType.
73
- */
74
- static fromJson<EntityMapperType extends RequestEntityMapper<unknown, any>>(this: EntityMapperConstructor<EntityMapperType, EntityMapperType['_SV']>, schemaValidator: EntityMapperType['_SV'], json: EntityMapperType['_dto']): EntityMapperType {
75
- return construct(this, schemaValidator).fromJson(json as unknown as EntityMapperType['_dto']);
76
- }
66
+ /**
67
+ * Creates an instance of a RequestEntityMapper from a JSON object.
68
+ *
69
+ * @template T - A type that extends RequestEntityMapper.
70
+ * @param {EntityMapperConstructor<T>} this - The constructor of the T.
71
+ * @param {T['_SV']} schemaValidator - The schema provider.
72
+ * @param {T['_dto']} json - The JSON object.
73
+ * @returns {T} - An instance of the T.
74
+ */
75
+ static fromJson<
76
+ T extends RequestEntityMapper<BaseEntity, SV>,
77
+ SV extends AnySchemaValidator,
78
+ JsonType extends T['_dto']
79
+ >(
80
+ this: EntityMapperConstructor<T, SV>,
81
+ schemaValidator: SV,
82
+ json: JsonType
83
+ ): T {
84
+ return construct(this, schemaValidator).fromJson(json);
85
+ }
77
86
 
78
- /**
79
- * Deserializes a JSON object to an entity.
80
- *
81
- * @template EntityMapperType - A type that extends RequestEntityMapper.
82
- * @param {EntityMapperConstructor<EntityMapperType>} this - The constructor of the EntityMapperType.
83
- * @param {EntityMapperType['_SV']} schemaValidator - The schema provider.
84
- * @param {EntityMapperStaticSchema<EntityMapperType>} json - The JSON object.
85
- * @param {...unknown[]} additionalArgs - Additional arguments.
86
- * @returns {EntityMapperType['_Entity']} - The entity.
87
- */
88
- static deserializeJsonToEntity<EntityMapperType extends RequestEntityMapper<unknown, any>>(this: EntityMapperConstructor<EntityMapperType, EntityMapperType['_SV']>, schemaValidator: EntityMapperType['_SV'], json: EntityMapperType['_dto'], ...additionalArgs: unknown[]): EntityMapperType['_Entity'] {
89
- return construct(this, schemaValidator).fromJson(json as EntityMapperType['_dto']).toEntity(...additionalArgs);
90
- }
87
+ /**
88
+ * Deserializes a JSON object to an entity.
89
+ *
90
+ * @template T - A type that extends RequestEntityMapper.
91
+ * @param {EntityMapperConstructor<T>} this - The constructor of the T.
92
+ * @param {T['_SV']} schemaValidator - The schema provider.
93
+ * @param {T['_dto']} json - The JSON object.
94
+ * @param {...unknown[]} additionalArgs - Additional arguments.
95
+ * @returns {T['_Entity']} - The entity.
96
+ */
97
+ static deserializeJsonToEntity<
98
+ T extends RequestEntityMapper<BaseEntity, SV>,
99
+ SV extends AnySchemaValidator,
100
+ JsonType extends T['_dto']
101
+ >(
102
+ this: EntityMapperConstructor<T, SV>,
103
+ schemaValidator: SV,
104
+ json: JsonType,
105
+ ...additionalArgs: unknown[]
106
+ ): T['_Entity'] {
107
+ return construct(this, schemaValidator)
108
+ .fromJson(json)
109
+ .toEntity(...additionalArgs);
110
+ }
91
111
  }
@@ -1,8 +1,7 @@
1
- import { SchemaValidator } from "@forklaunch/validator/interfaces";
2
- import { BaseEntity } from "../../database/mikro/models/entities/base.entity";
3
- import { EntityMapperConstructor } from "../interfaces/entityMapper.interface";
4
- import { EntityMapperStaticSchema } from "../types/entityMapper.types";
5
- import { BaseEntityMapper, construct } from "./baseEntityMapper.model";
1
+ import { AnySchemaValidator } from '@forklaunch/validator';
2
+ import { BaseEntity } from '../../database/mikro/models/entities/base.entity';
3
+ import { EntityMapperConstructor } from '../interfaces/entityMapper.interface';
4
+ import { BaseEntityMapper, construct } from './baseEntityMapper.model';
6
5
 
7
6
  /**
8
7
  * Abstract class representing a response entityMapper.
@@ -11,67 +10,89 @@ import { BaseEntityMapper, construct } from "./baseEntityMapper.model";
11
10
  * @template SV - A type that extends SchemaValidator.
12
11
  * @extends {BaseEntityMapper<SV>}
13
12
  */
14
- export abstract class ResponseEntityMapper<Entity extends BaseEntity | unknown, SV extends SchemaValidator> extends BaseEntityMapper<SV> {
15
- /**
16
- * The entity type.
17
- * @type {Entity}
18
- * @protected
19
- */
20
- _Entity!: Entity;
13
+ export abstract class ResponseEntityMapper<
14
+ Entity extends BaseEntity,
15
+ SV extends AnySchemaValidator
16
+ > extends BaseEntityMapper<SV> {
17
+ /**
18
+ * The entity type.
19
+ * @type {Entity}
20
+ * @protected
21
+ */
22
+ _Entity!: Entity;
21
23
 
22
- /**
23
- * Populates entityMapper with DTO from an entity.
24
- *
25
- * @abstract
26
- * @param {Entity} entity - The entity to convert.
27
- * @returns {this} - The instance of the ResponseEntityMapper.
28
- */
29
- abstract fromEntity(entity: Entity, ...additionalArgs: unknown[]): this;
24
+ /**
25
+ * Populates entityMapper with DTO from an entity.
26
+ *
27
+ * @abstract
28
+ * @param {Entity} entity - The entity to convert.
29
+ * @returns {this} - The instance of the ResponseEntityMapper.
30
+ */
31
+ abstract fromEntity(entity: Entity, ...additionalArgs: unknown[]): this;
30
32
 
31
- /**
32
- * Converts the underlying DTO to a JSON object.
33
- *
34
- * @param {...unknown[]} additionalArgs - Additional arguments.
35
- * @returns {this['_dto']} - The JSON object.
36
- */
37
- toJson(): this['_dto'] {
38
- if (!this.schemaValidator.validate(this.schemaValidator.schemify(this.schema), this.dto)) {
39
- throw new Error('Invalid DTO');
40
- }
41
- return this.dto;
33
+ /**
34
+ * Converts the underlying DTO to a JSON object.
35
+ *
36
+ * @param {...unknown[]} additionalArgs - Additional arguments.
37
+ * @returns {this['_dto']} - The JSON object.
38
+ */
39
+ toJson(): this['_dto'] {
40
+ if (
41
+ !this.schemaValidator.validate(
42
+ this.schemaValidator.schemify(this.schema),
43
+ this.dto
44
+ )
45
+ ) {
46
+ throw new Error('Invalid DTO');
42
47
  }
48
+ return this.dto;
49
+ }
43
50
 
44
- /**
45
- * Serializes an entity to a JSON object.
46
- *
47
- * @param {Entity} entity - The entity to serialize.
48
- * @returns {this['_dto']} - The JSON object.
49
- */
50
- serializeEntityToJson(entity: Entity): this['_dto'] {
51
- return this.fromEntity(entity).toJson();
52
- }
51
+ /**
52
+ * Serializes an entity to a JSON object.
53
+ *
54
+ * @param {Entity} entity - The entity to serialize.
55
+ * @returns {this['_dto']} - The JSON object.
56
+ */
57
+ serializeEntityToJson(entity: Entity): this['_dto'] {
58
+ return this.fromEntity(entity).toJson();
59
+ }
53
60
 
54
- /**
55
- * Populates entityMapper with DTO from an entity.
56
- *
57
- * @template EntityMapperType - A type that extends ResponseEntityMapper.
58
- * @param {EntityMapperConstructor<EntityMapperType>} this - The constructor of the EntityMapperType.
59
- * @param {EntityMapperType['_Entity']} entity - The entity to convert.
60
- * @returns {EntityMapperType} - An instance of the EntityMapperType.
61
- */
62
- static fromEntity<EntityMapperType extends ResponseEntityMapper<unknown, any>>(this: EntityMapperConstructor<EntityMapperType, EntityMapperType['_SV']>, schemaValidator: EntityMapperType['_SV'], entity: EntityMapperType['_Entity']): EntityMapperType {
63
- return construct(this, schemaValidator).fromEntity(entity);
64
- }
61
+ /**
62
+ * Populates entityMapper with DTO from an entity.
63
+ *
64
+ * @template T - A type that extends ResponseEntityMapper.
65
+ * @param {EntityMapperConstructor<T>} this - The constructor of the T.
66
+ * @param {T['_Entity']} entity - The entity to convert.
67
+ * @returns {T} - An instance of the T.
68
+ */
69
+ static fromEntity<
70
+ T extends ResponseEntityMapper<BaseEntity, SV>,
71
+ SV extends AnySchemaValidator
72
+ >(
73
+ this: EntityMapperConstructor<T, SV>,
74
+ schemaValidator: SV,
75
+ entity: T['_Entity']
76
+ ): T {
77
+ return construct(this, schemaValidator).fromEntity(entity);
78
+ }
65
79
 
66
- /**
67
- * Serializes an entity to a JSON object.
68
- *
69
- * @template EntityMapperType - A type that extends ResponseEntityMapper.
70
- * @param {EntityMapperConstructor<EntityMapperType>} this - The constructor of the EntityMapperType.
71
- * @param {EntityMapperType['_Entity']} entity - The entity to serialize.
72
- * @returns {EntityMapperStaticSchema<EntityMapperType>} - The JSON object.
73
- */
74
- static serializeEntityToJson<EntityMapperType extends ResponseEntityMapper<unknown, any>>(this: EntityMapperConstructor<EntityMapperType, EntityMapperType['_SV']>, schemaValidator: EntityMapperType['_SV'], entity: EntityMapperType['_Entity']): EntityMapperStaticSchema<EntityMapperType> {
75
- return construct(this, schemaValidator).serializeEntityToJson(entity) as EntityMapperStaticSchema<EntityMapperType>;
76
- }
80
+ /**
81
+ * Serializes an entity to a JSON object.
82
+ *
83
+ * @template T - A type that extends ResponseEntityMapper.
84
+ * @param {EntityMapperConstructor<T>} this - The constructor of the T.
85
+ * @param {T['_Entity']} entity - The entity to serialize.
86
+ * @returns {T['_dto']} - The JSON object.
87
+ */
88
+ static serializeEntityToJson<
89
+ T extends ResponseEntityMapper<BaseEntity, SV>,
90
+ SV extends AnySchemaValidator
91
+ >(
92
+ this: EntityMapperConstructor<T, SV>,
93
+ schemaValidator: SV,
94
+ entity: T['_Entity']
95
+ ): T['_dto'] {
96
+ return construct(this, schemaValidator).serializeEntityToJson(entity);
97
+ }
77
98
  }
@@ -1,29 +1,12 @@
1
- import { Schema, SchemaCatchall, ValidSchemaObject } from "@forklaunch/validator/";
2
- import { SchemaValidator } from "@forklaunch/validator/interfaces";
3
- import { UnboxedObjectSchema } from "@forklaunch/validator/types";
4
- import { BaseEntityMapper } from "../models/baseEntityMapper.model";
1
+ import { AnySchemaValidator } from '@forklaunch/validator';
2
+ import { UnboxedObjectSchema } from '@forklaunch/validator/types';
5
3
 
6
4
  /**
7
5
  * Type representing a schema validator object for an entity mapper.
8
- *
6
+ *
9
7
  * @template SV - A type that extends SchemaValidator.
10
8
  * @typedef {ValidSchemaObject<SV> | UnboxedObjectSchema<SchemaCatchall<SV>> & {}} EntityMapperSchemaValidatorObject
11
9
  */
12
- export type EntityMapperSchemaValidatorObject<SV extends SchemaValidator> = ValidSchemaObject<SV> | UnboxedObjectSchema<SchemaCatchall<SV>>;
13
-
14
- /**
15
- * Type representing the static schema of an entity mapper.
16
- *
17
- * @template T - A type that extends BaseEntityMapper with a schema property.
18
- * @typedef {Schema<T['schema'], T['_SV']> & {}} EntityMapperStaticSchema
19
- */
20
- export type EntityMapperStaticSchema<T extends BaseEntityMapper<any> & { schema: any }> = Schema<T['schema'], T['_SV']>;
21
-
22
- /**
23
- * Type representing the schema of an entity mapper.
24
- *
25
- * @template T - A type that extends ValidSchemaObject or UnboxedObjectSchema with SchemaCatchall.
26
- * @template SV - A type that extends SchemaValidator.
27
- * @typedef {Schema<T, SV> & {}} EntityMapperSchema
28
- */
29
- export type EntityMapperSchema<T extends ValidSchemaObject<SV> | UnboxedObjectSchema<SchemaCatchall<SV>>, SV extends SchemaValidator> = Schema<T, SV>;
10
+ export type EntityMapperSchemaValidatorObject<SV extends AnySchemaValidator> =
11
+ | SV['_ValidSchemaObject']
12
+ | UnboxedObjectSchema<SV>;
package/http/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './middlewares';
2
+ export * from './types';
@@ -0,0 +1,2 @@
1
+ export * from './request.middleware';
2
+ export * from './response.middleware';