@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.
- package/.prettierignore +2 -0
- package/.prettierrc +6 -0
- package/dist/cache/index.d.ts +3 -0
- package/dist/cache/index.js +20 -0
- package/dist/cache/index.js.map +1 -0
- package/dist/cache/interfaces/ttlCache.interface.d.ts +1 -1
- package/dist/cache/redisTtlCache.d.ts +3 -2
- package/dist/cache/redisTtlCache.js +2 -2
- package/dist/cache/redisTtlCache.js.map +1 -1
- package/dist/cache/types/{ttlCacheRecord.js → ttlCacheRecord.types.js} +1 -1
- package/dist/cache/types/ttlCacheRecord.types.js.map +1 -0
- package/dist/controllers/index.d.ts +1 -0
- package/dist/controllers/index.js +18 -0
- package/dist/controllers/index.js.map +1 -0
- package/dist/database/index.d.ts +1 -0
- package/dist/database/index.js +18 -0
- package/dist/database/index.js.map +1 -0
- package/dist/database/mikro/models/entities/base.entity.js +1 -1
- package/dist/entityMapper/index.d.ts +2 -0
- package/dist/entityMapper/index.js +19 -0
- package/dist/entityMapper/index.js.map +1 -0
- package/dist/entityMapper/interfaces/entityMapper.interface.d.ts +3 -3
- package/dist/entityMapper/models/baseEntityMapper.model.d.ts +23 -23
- package/dist/entityMapper/models/baseEntityMapper.model.js +17 -17
- package/dist/entityMapper/models/baseEntityMapper.model.js.map +1 -1
- package/dist/entityMapper/models/requestEntityMapper.model.d.ts +17 -23
- package/dist/entityMapper/models/requestEntityMapper.model.js +13 -19
- package/dist/entityMapper/models/requestEntityMapper.model.js.map +1 -1
- package/dist/entityMapper/models/responseEntityMapper.model.d.ts +15 -16
- package/dist/entityMapper/models/responseEntityMapper.model.js +8 -8
- package/dist/entityMapper/models/responseEntityMapper.model.js.map +1 -1
- package/dist/entityMapper/types/entityMapper.types.d.ts +3 -22
- package/dist/http/index.d.ts +2 -0
- package/dist/http/index.js +19 -0
- package/dist/http/index.js.map +1 -0
- package/dist/http/middlewares/index.d.ts +2 -0
- package/dist/http/middlewares/index.js +19 -0
- package/dist/http/middlewares/index.js.map +1 -0
- package/dist/http/middlewares/request.middleware.d.ts +11 -11
- package/dist/http/middlewares/request.middleware.js +32 -31
- package/dist/http/middlewares/request.middleware.js.map +1 -1
- package/dist/http/middlewares/response.middleware.d.ts +3 -2
- package/dist/http/middlewares/response.middleware.js +9 -6
- package/dist/http/middlewares/response.middleware.js.map +1 -1
- package/dist/http/types/api.types.d.ts +16 -13
- package/dist/http/types/index.d.ts +2 -0
- package/dist/http/types/index.js +19 -0
- package/dist/http/types/index.js.map +1 -0
- package/dist/http/types/primitive.types.d.ts +14 -13
- package/dist/index.d.ts +6 -1
- package/dist/index.js +20 -5
- package/dist/index.js.map +1 -1
- package/dist/services/index.d.ts +1 -0
- package/dist/services/index.js +18 -0
- package/dist/services/index.js.map +1 -0
- package/dist/services/interfaces/baseService.d.ts +1 -1
- package/dist/tests/{dto.test.js → entityMapper.test.js} +10 -32
- package/dist/tests/entityMapper.test.js.map +1 -0
- package/dist/tests/http.middleware.test.d.ts +1 -0
- package/dist/tests/http.middleware.test.js +79 -0
- package/dist/tests/http.middleware.test.js.map +1 -0
- package/dist/tests/redisTtlCache.test.js +32 -15
- package/dist/tests/redisTtlCache.test.js.map +1 -1
- package/entityMapper/index.ts +2 -0
- package/entityMapper/interfaces/entityMapper.interface.ts +11 -11
- package/entityMapper/models/baseEntityMapper.model.ts +90 -73
- package/entityMapper/models/requestEntityMapper.model.ts +96 -76
- package/entityMapper/models/responseEntityMapper.model.ts +83 -62
- package/entityMapper/types/entityMapper.types.ts +6 -23
- package/http/index.ts +2 -0
- package/http/middlewares/index.ts +2 -0
- package/http/middlewares/request.middleware.ts +118 -62
- package/http/middlewares/response.middleware.ts +56 -30
- package/http/types/api.types.ts +43 -25
- package/http/types/index.ts +2 -0
- package/http/types/primitive.types.ts +65 -44
- package/index.ts +6 -5
- package/jest.config.ts +2 -2
- package/package.json +13 -4
- package/tests/entityMapper.test.ts +219 -0
- package/tests/http.middleware.test.ts +99 -0
- package/tests/redisTtlCache.test.ts +49 -29
- package/dist/cache/types/ttlCacheRecord.js.map +0 -1
- package/dist/tests/dto.test.js.map +0 -1
- package/tests/dto.test.ts +0 -235
- /package/dist/cache/types/{ttlCacheRecord.d.ts → ttlCacheRecord.types.d.ts} +0 -0
- /package/dist/tests/{dto.test.d.ts → entityMapper.test.d.ts} +0 -0
@@ -1,8 +1,7 @@
|
|
1
|
-
import {
|
2
|
-
import { BaseEntity } from
|
3
|
-
import { EntityMapperConstructor } from
|
4
|
-
import {
|
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<
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
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
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
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
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
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
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
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
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
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
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
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 {
|
2
|
-
import { BaseEntity } from
|
3
|
-
import { EntityMapperConstructor } from
|
4
|
-
import {
|
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<
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
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
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
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
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
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
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
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
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
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
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
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 {
|
2
|
-
import {
|
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
|
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