@forklaunch/core 0.13.8 → 0.14.0
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/lib/mappers/index.d.mts +15 -212
- package/lib/mappers/index.d.ts +15 -212
- package/lib/mappers/index.js +32 -212
- package/lib/mappers/index.js.map +1 -1
- package/lib/mappers/index.mjs +31 -214
- package/lib/mappers/index.mjs.map +1 -1
- package/package.json +9 -9
package/lib/mappers/index.d.mts
CHANGED
@@ -1,214 +1,17 @@
|
|
1
|
-
import { AnySchemaValidator,
|
1
|
+
import { AnySchemaValidator, IdiomaticSchema, Schema } from '@forklaunch/validator';
|
2
|
+
import { Constructor } from '@mikro-orm/core';
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
* @returns {T} - A new instance of the entity mapper.
|
16
|
-
*/
|
17
|
-
new (schemaValidator: SV): T;
|
18
|
-
}
|
4
|
+
declare function mapper<SV extends AnySchemaValidator, DtoSchema extends IdiomaticSchema<SV>, Entity, AdditionalArgs extends unknown[] = []>(schemaValidator: SV, dtoSchema: DtoSchema, _entityConstructor: Constructor<Entity>, mapperDefinition: {
|
5
|
+
toEntity: (dto: Schema<DtoSchema, SV>, ...args: AdditionalArgs) => Promise<Entity>;
|
6
|
+
} | {
|
7
|
+
toDto: (entity: Entity, ...args: AdditionalArgs) => Promise<Schema<DtoSchema, SV>>;
|
8
|
+
} | {
|
9
|
+
toEntity: (dto: Schema<DtoSchema, SV>, ...args: AdditionalArgs) => Promise<Entity>;
|
10
|
+
toDto: (entity: Entity, ...args: AdditionalArgs) => Promise<Schema<DtoSchema, SV>>;
|
11
|
+
}): {
|
12
|
+
schema: DtoSchema;
|
13
|
+
toEntity: (dto: Schema<DtoSchema, SV>, ...args: AdditionalArgs) => Promise<Entity>;
|
14
|
+
toDto: (entity: Entity, ...args: AdditionalArgs) => Promise<Schema<DtoSchema, SV>>;
|
15
|
+
};
|
19
16
|
|
20
|
-
|
21
|
-
* Type representing a schema validator object for an entity mapper.
|
22
|
-
*
|
23
|
-
* @template SV - A type that extends AnySchemaValidator.
|
24
|
-
* @typedef {SV['_ValidSchemaObject'] | UnboxedObjectSchema<SV>} MapperSchemaValidatorObject
|
25
|
-
*/
|
26
|
-
type MapperSchemaValidatorObject<SV extends AnySchemaValidator> = SV['_ValidSchemaObject'] | UnboxedObjectSchema<SV>;
|
27
|
-
|
28
|
-
/**
|
29
|
-
* Abstract class representing a base entity mapper.
|
30
|
-
*
|
31
|
-
* @template SV - A type that extends AnySchemaValidator.
|
32
|
-
*/
|
33
|
-
declare abstract class BaseMapper<SV extends AnySchemaValidator> {
|
34
|
-
/**
|
35
|
-
* The schema validator exact type.
|
36
|
-
* @type {SV}
|
37
|
-
* @protected
|
38
|
-
*/
|
39
|
-
_SV: SV;
|
40
|
-
/**
|
41
|
-
* The schema validator as a general type.
|
42
|
-
* @type {SchemaValidator}
|
43
|
-
* @protected
|
44
|
-
*/
|
45
|
-
protected schemaValidator: SchemaValidator;
|
46
|
-
/**
|
47
|
-
* The schema definition.
|
48
|
-
* @type {MapperSchemaValidatorObject<SV>}
|
49
|
-
* @abstract
|
50
|
-
*/
|
51
|
-
abstract schema: MapperSchemaValidatorObject<SV>;
|
52
|
-
/**
|
53
|
-
* The Data Transfer Object (DTO).
|
54
|
-
* @type {Schema<this['schema'], SV>}
|
55
|
-
*/
|
56
|
-
_dto: Schema<this['schema'], SV>;
|
57
|
-
/**
|
58
|
-
* Creates an instance of BaseMapper.
|
59
|
-
*
|
60
|
-
* @param {SV} schemaValidator - The schema provider.
|
61
|
-
*/
|
62
|
-
constructor(schemaValidator: SV);
|
63
|
-
/**
|
64
|
-
* Validates and sets the Data Transfer Object (DTO).
|
65
|
-
*
|
66
|
-
* @param {this['_dto']} dto - The Data Transfer Object (DTO).
|
67
|
-
* @throws {Error} - Throws an error if the DTO is invalid.
|
68
|
-
*/
|
69
|
-
set dto(_dto: this['_dto']);
|
70
|
-
/**
|
71
|
-
* Validates and gets the Data Transfer Object (DTO).
|
72
|
-
*
|
73
|
-
* @returns {this['_dto']} - The Data Transfer Object (DTO).
|
74
|
-
*/
|
75
|
-
get dto(): this['_dto'];
|
76
|
-
/**
|
77
|
-
* Gets the schema of a T.
|
78
|
-
*
|
79
|
-
* @template T - A type that extends BaseMapper.
|
80
|
-
* @template SV - A type that extends AnySchemaValidator.
|
81
|
-
* @param {MapperConstructor<T, SV>} this - The constructor of the T.
|
82
|
-
* @returns {T['schema']} - The schema of the T.
|
83
|
-
*/
|
84
|
-
static schema<T extends BaseMapper<SV>, SV extends AnySchemaValidator>(this: MapperConstructor<T, SV>): T['schema'];
|
85
|
-
}
|
86
|
-
|
87
|
-
/**
|
88
|
-
* Abstract class representing a request entity mapper.
|
89
|
-
*
|
90
|
-
* @template Entity - A type that extends SqlBaseEntity.
|
91
|
-
* @template SV - A type that extends AnySchemaValidator.
|
92
|
-
* @extends {BaseMapper<SV>}
|
93
|
-
*/
|
94
|
-
declare abstract class RequestMapper<Entity, SV extends AnySchemaValidator> extends BaseMapper<SV> {
|
95
|
-
/**
|
96
|
-
* The entity type.
|
97
|
-
* @type {Entity}
|
98
|
-
* @protected
|
99
|
-
*/
|
100
|
-
_Entity: Entity;
|
101
|
-
/**
|
102
|
-
* Converts the underlying DTO to an entity.
|
103
|
-
*
|
104
|
-
* @abstract
|
105
|
-
* @param {...unknown[]} additionalArgs - Additional arguments.
|
106
|
-
* @returns {Promise<Entity>} - The entity.
|
107
|
-
*/
|
108
|
-
abstract toEntity(...additionalArgs: unknown[]): Promise<Entity>;
|
109
|
-
/**
|
110
|
-
* Populates the DTO with data from a JSON object.
|
111
|
-
*
|
112
|
-
* @param {this['_dto']} json - The JSON object.
|
113
|
-
* @returns {Promise<this>} - The instance of the RequestMapper.
|
114
|
-
*/
|
115
|
-
fromDto(json: this['_dto']): Promise<this>;
|
116
|
-
/**
|
117
|
-
* Deserializes a JSON object to an entity.
|
118
|
-
*
|
119
|
-
* @param {this['_dto']} json - The JSON object.
|
120
|
-
* @param {...unknown[]} additionalArgs - Additional arguments.
|
121
|
-
* @returns {Entity} - The entity.
|
122
|
-
*/
|
123
|
-
deserializeDtoToEntity(json: this['_dto'], ...additionalArgs: Parameters<this['toEntity']>): Promise<Entity>;
|
124
|
-
/**
|
125
|
-
* Creates an instance of a RequestMapper from a JSON object.
|
126
|
-
*
|
127
|
-
* @template T - A type that extends RequestMapper.
|
128
|
-
* @template SV - A type that extends AnySchemaValidator.
|
129
|
-
* @template JsonType - The type of the JSON object.
|
130
|
-
* @param {MapperConstructor<T, SV>} this - The constructor of the T.
|
131
|
-
* @param {SV} schemaValidator - The schema provider.
|
132
|
-
* @param {JsonType} json - The JSON object.
|
133
|
-
* @returns {T} - An instance of the T.
|
134
|
-
*/
|
135
|
-
static fromDto<T extends RequestMapper<unknown, SV>, SV extends AnySchemaValidator, JsonType extends T['_dto']>(this: MapperConstructor<T, SV>, schemaValidator: SV, json: JsonType): Promise<T>;
|
136
|
-
/**
|
137
|
-
* Deserializes a JSON object to an entity.
|
138
|
-
*
|
139
|
-
* @template T - A type that extends RequestMapper.
|
140
|
-
* @template SV - A type that extends AnySchemaValidator.
|
141
|
-
* @template JsonType - The type of the JSON object.
|
142
|
-
* @param {MapperConstructor<T, SV>} this - The constructor of the T.
|
143
|
-
* @param {SV} schemaValidator - The schema provider.
|
144
|
-
* @param {JsonType} json - The JSON object.
|
145
|
-
* @param {...unknown[]} additionalArgs - Additional arguments.
|
146
|
-
* @returns {T['_Entity']} - The entity.
|
147
|
-
*/
|
148
|
-
static deserializeDtoToEntity<T extends RequestMapper<unknown, SV>, SV extends AnySchemaValidator, JsonType extends T['_dto']>(this: MapperConstructor<T, SV>, schemaValidator: SV, json: JsonType, ...additionalArgs: Parameters<T['toEntity']>): Promise<T['_Entity']>;
|
149
|
-
}
|
150
|
-
|
151
|
-
/**
|
152
|
-
* Abstract class representing a response entity mapper.
|
153
|
-
*
|
154
|
-
* @template Entity - A type that extends SqlBaseEntity.
|
155
|
-
* @template SV - A type that extends AnySchemaValidator.
|
156
|
-
* @extends {BaseMapper<SV>}
|
157
|
-
*/
|
158
|
-
declare abstract class ResponseMapper<Entity, SV extends AnySchemaValidator> extends BaseMapper<SV> {
|
159
|
-
/**
|
160
|
-
* The entity type.
|
161
|
-
* @type {Entity}
|
162
|
-
* @protected
|
163
|
-
*/
|
164
|
-
_Entity: Entity;
|
165
|
-
/**
|
166
|
-
* Populates entity mapper with DTO from an entity.
|
167
|
-
*
|
168
|
-
* @abstract
|
169
|
-
* @param {Entity} entity - The entity to convert.
|
170
|
-
* @param {...unknown[]} additionalArgs - Additional arguments.
|
171
|
-
* @returns {this} - The instance of the ResponseMapper.
|
172
|
-
*/
|
173
|
-
abstract fromEntity(entity: Entity, ...additionalArgs: unknown[]): Promise<this>;
|
174
|
-
/**
|
175
|
-
* Converts the underlying DTO to a JSON object.
|
176
|
-
*
|
177
|
-
* @returns {this['_dto']} - The JSON object.
|
178
|
-
* @throws {Error} - Throws an error if the DTO is invalid.
|
179
|
-
*/
|
180
|
-
toDto(): Promise<this['_dto']>;
|
181
|
-
/**
|
182
|
-
* Serializes an entity to a JSON object.
|
183
|
-
*
|
184
|
-
* @param {Entity} entity - The entity to serialize.
|
185
|
-
* @returns {this['_dto']} - The JSON object.
|
186
|
-
* @throws {Error} - Throws an error if the DTO is invalid.
|
187
|
-
*/
|
188
|
-
serializeEntityToDto(...[entity, ...additionalArgs]: Parameters<this['fromEntity']>): Promise<this['_dto']>;
|
189
|
-
/**
|
190
|
-
* Populates entity mapper with DTO from an entity.
|
191
|
-
*
|
192
|
-
* @template T - A type that extends ResponseMapper.
|
193
|
-
* @template SV - A type that extends AnySchemaValidator.
|
194
|
-
* @param {MapperConstructor<T, SV>} this - The constructor of the T.
|
195
|
-
* @param {SV} schemaValidator - The schema provider.
|
196
|
-
* @param {T['_Entity']} entity - The entity to convert.
|
197
|
-
* @returns {T} - An instance of the T.
|
198
|
-
*/
|
199
|
-
static fromEntity<T extends ResponseMapper<unknown, SV>, SV extends AnySchemaValidator>(this: MapperConstructor<T, SV>, schemaValidator: SV, ...[entity, ...additionalArgs]: Parameters<T['fromEntity']>): Promise<T>;
|
200
|
-
/**
|
201
|
-
* Serializes an entity to a JSON object.
|
202
|
-
*
|
203
|
-
* @template T - A type that extends ResponseMapper.
|
204
|
-
* @template SV - A type that extends AnySchemaValidator.
|
205
|
-
* @param {MapperConstructor<T, SV>} this - The constructor of the T.
|
206
|
-
* @param {SV} schemaValidator - The schema provider.
|
207
|
-
* @param {T['_Entity']} entity - The entity to serialize.
|
208
|
-
* @returns {T['_dto']} - The JSON object.
|
209
|
-
* @throws {Error} - Throws an error if the DTO is invalid.
|
210
|
-
*/
|
211
|
-
static serializeEntityToDto<T extends ResponseMapper<unknown, SV>, SV extends AnySchemaValidator, DtoType extends T['_dto']>(this: MapperConstructor<T, SV>, schemaValidator: SV, ...[entity, ...additionalArgs]: Parameters<T['fromEntity']>): Promise<DtoType>;
|
212
|
-
}
|
213
|
-
|
214
|
-
export { RequestMapper, ResponseMapper };
|
17
|
+
export { mapper };
|
package/lib/mappers/index.d.ts
CHANGED
@@ -1,214 +1,17 @@
|
|
1
|
-
import { AnySchemaValidator,
|
1
|
+
import { AnySchemaValidator, IdiomaticSchema, Schema } from '@forklaunch/validator';
|
2
|
+
import { Constructor } from '@mikro-orm/core';
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
* @returns {T} - A new instance of the entity mapper.
|
16
|
-
*/
|
17
|
-
new (schemaValidator: SV): T;
|
18
|
-
}
|
4
|
+
declare function mapper<SV extends AnySchemaValidator, DtoSchema extends IdiomaticSchema<SV>, Entity, AdditionalArgs extends unknown[] = []>(schemaValidator: SV, dtoSchema: DtoSchema, _entityConstructor: Constructor<Entity>, mapperDefinition: {
|
5
|
+
toEntity: (dto: Schema<DtoSchema, SV>, ...args: AdditionalArgs) => Promise<Entity>;
|
6
|
+
} | {
|
7
|
+
toDto: (entity: Entity, ...args: AdditionalArgs) => Promise<Schema<DtoSchema, SV>>;
|
8
|
+
} | {
|
9
|
+
toEntity: (dto: Schema<DtoSchema, SV>, ...args: AdditionalArgs) => Promise<Entity>;
|
10
|
+
toDto: (entity: Entity, ...args: AdditionalArgs) => Promise<Schema<DtoSchema, SV>>;
|
11
|
+
}): {
|
12
|
+
schema: DtoSchema;
|
13
|
+
toEntity: (dto: Schema<DtoSchema, SV>, ...args: AdditionalArgs) => Promise<Entity>;
|
14
|
+
toDto: (entity: Entity, ...args: AdditionalArgs) => Promise<Schema<DtoSchema, SV>>;
|
15
|
+
};
|
19
16
|
|
20
|
-
|
21
|
-
* Type representing a schema validator object for an entity mapper.
|
22
|
-
*
|
23
|
-
* @template SV - A type that extends AnySchemaValidator.
|
24
|
-
* @typedef {SV['_ValidSchemaObject'] | UnboxedObjectSchema<SV>} MapperSchemaValidatorObject
|
25
|
-
*/
|
26
|
-
type MapperSchemaValidatorObject<SV extends AnySchemaValidator> = SV['_ValidSchemaObject'] | UnboxedObjectSchema<SV>;
|
27
|
-
|
28
|
-
/**
|
29
|
-
* Abstract class representing a base entity mapper.
|
30
|
-
*
|
31
|
-
* @template SV - A type that extends AnySchemaValidator.
|
32
|
-
*/
|
33
|
-
declare abstract class BaseMapper<SV extends AnySchemaValidator> {
|
34
|
-
/**
|
35
|
-
* The schema validator exact type.
|
36
|
-
* @type {SV}
|
37
|
-
* @protected
|
38
|
-
*/
|
39
|
-
_SV: SV;
|
40
|
-
/**
|
41
|
-
* The schema validator as a general type.
|
42
|
-
* @type {SchemaValidator}
|
43
|
-
* @protected
|
44
|
-
*/
|
45
|
-
protected schemaValidator: SchemaValidator;
|
46
|
-
/**
|
47
|
-
* The schema definition.
|
48
|
-
* @type {MapperSchemaValidatorObject<SV>}
|
49
|
-
* @abstract
|
50
|
-
*/
|
51
|
-
abstract schema: MapperSchemaValidatorObject<SV>;
|
52
|
-
/**
|
53
|
-
* The Data Transfer Object (DTO).
|
54
|
-
* @type {Schema<this['schema'], SV>}
|
55
|
-
*/
|
56
|
-
_dto: Schema<this['schema'], SV>;
|
57
|
-
/**
|
58
|
-
* Creates an instance of BaseMapper.
|
59
|
-
*
|
60
|
-
* @param {SV} schemaValidator - The schema provider.
|
61
|
-
*/
|
62
|
-
constructor(schemaValidator: SV);
|
63
|
-
/**
|
64
|
-
* Validates and sets the Data Transfer Object (DTO).
|
65
|
-
*
|
66
|
-
* @param {this['_dto']} dto - The Data Transfer Object (DTO).
|
67
|
-
* @throws {Error} - Throws an error if the DTO is invalid.
|
68
|
-
*/
|
69
|
-
set dto(_dto: this['_dto']);
|
70
|
-
/**
|
71
|
-
* Validates and gets the Data Transfer Object (DTO).
|
72
|
-
*
|
73
|
-
* @returns {this['_dto']} - The Data Transfer Object (DTO).
|
74
|
-
*/
|
75
|
-
get dto(): this['_dto'];
|
76
|
-
/**
|
77
|
-
* Gets the schema of a T.
|
78
|
-
*
|
79
|
-
* @template T - A type that extends BaseMapper.
|
80
|
-
* @template SV - A type that extends AnySchemaValidator.
|
81
|
-
* @param {MapperConstructor<T, SV>} this - The constructor of the T.
|
82
|
-
* @returns {T['schema']} - The schema of the T.
|
83
|
-
*/
|
84
|
-
static schema<T extends BaseMapper<SV>, SV extends AnySchemaValidator>(this: MapperConstructor<T, SV>): T['schema'];
|
85
|
-
}
|
86
|
-
|
87
|
-
/**
|
88
|
-
* Abstract class representing a request entity mapper.
|
89
|
-
*
|
90
|
-
* @template Entity - A type that extends SqlBaseEntity.
|
91
|
-
* @template SV - A type that extends AnySchemaValidator.
|
92
|
-
* @extends {BaseMapper<SV>}
|
93
|
-
*/
|
94
|
-
declare abstract class RequestMapper<Entity, SV extends AnySchemaValidator> extends BaseMapper<SV> {
|
95
|
-
/**
|
96
|
-
* The entity type.
|
97
|
-
* @type {Entity}
|
98
|
-
* @protected
|
99
|
-
*/
|
100
|
-
_Entity: Entity;
|
101
|
-
/**
|
102
|
-
* Converts the underlying DTO to an entity.
|
103
|
-
*
|
104
|
-
* @abstract
|
105
|
-
* @param {...unknown[]} additionalArgs - Additional arguments.
|
106
|
-
* @returns {Promise<Entity>} - The entity.
|
107
|
-
*/
|
108
|
-
abstract toEntity(...additionalArgs: unknown[]): Promise<Entity>;
|
109
|
-
/**
|
110
|
-
* Populates the DTO with data from a JSON object.
|
111
|
-
*
|
112
|
-
* @param {this['_dto']} json - The JSON object.
|
113
|
-
* @returns {Promise<this>} - The instance of the RequestMapper.
|
114
|
-
*/
|
115
|
-
fromDto(json: this['_dto']): Promise<this>;
|
116
|
-
/**
|
117
|
-
* Deserializes a JSON object to an entity.
|
118
|
-
*
|
119
|
-
* @param {this['_dto']} json - The JSON object.
|
120
|
-
* @param {...unknown[]} additionalArgs - Additional arguments.
|
121
|
-
* @returns {Entity} - The entity.
|
122
|
-
*/
|
123
|
-
deserializeDtoToEntity(json: this['_dto'], ...additionalArgs: Parameters<this['toEntity']>): Promise<Entity>;
|
124
|
-
/**
|
125
|
-
* Creates an instance of a RequestMapper from a JSON object.
|
126
|
-
*
|
127
|
-
* @template T - A type that extends RequestMapper.
|
128
|
-
* @template SV - A type that extends AnySchemaValidator.
|
129
|
-
* @template JsonType - The type of the JSON object.
|
130
|
-
* @param {MapperConstructor<T, SV>} this - The constructor of the T.
|
131
|
-
* @param {SV} schemaValidator - The schema provider.
|
132
|
-
* @param {JsonType} json - The JSON object.
|
133
|
-
* @returns {T} - An instance of the T.
|
134
|
-
*/
|
135
|
-
static fromDto<T extends RequestMapper<unknown, SV>, SV extends AnySchemaValidator, JsonType extends T['_dto']>(this: MapperConstructor<T, SV>, schemaValidator: SV, json: JsonType): Promise<T>;
|
136
|
-
/**
|
137
|
-
* Deserializes a JSON object to an entity.
|
138
|
-
*
|
139
|
-
* @template T - A type that extends RequestMapper.
|
140
|
-
* @template SV - A type that extends AnySchemaValidator.
|
141
|
-
* @template JsonType - The type of the JSON object.
|
142
|
-
* @param {MapperConstructor<T, SV>} this - The constructor of the T.
|
143
|
-
* @param {SV} schemaValidator - The schema provider.
|
144
|
-
* @param {JsonType} json - The JSON object.
|
145
|
-
* @param {...unknown[]} additionalArgs - Additional arguments.
|
146
|
-
* @returns {T['_Entity']} - The entity.
|
147
|
-
*/
|
148
|
-
static deserializeDtoToEntity<T extends RequestMapper<unknown, SV>, SV extends AnySchemaValidator, JsonType extends T['_dto']>(this: MapperConstructor<T, SV>, schemaValidator: SV, json: JsonType, ...additionalArgs: Parameters<T['toEntity']>): Promise<T['_Entity']>;
|
149
|
-
}
|
150
|
-
|
151
|
-
/**
|
152
|
-
* Abstract class representing a response entity mapper.
|
153
|
-
*
|
154
|
-
* @template Entity - A type that extends SqlBaseEntity.
|
155
|
-
* @template SV - A type that extends AnySchemaValidator.
|
156
|
-
* @extends {BaseMapper<SV>}
|
157
|
-
*/
|
158
|
-
declare abstract class ResponseMapper<Entity, SV extends AnySchemaValidator> extends BaseMapper<SV> {
|
159
|
-
/**
|
160
|
-
* The entity type.
|
161
|
-
* @type {Entity}
|
162
|
-
* @protected
|
163
|
-
*/
|
164
|
-
_Entity: Entity;
|
165
|
-
/**
|
166
|
-
* Populates entity mapper with DTO from an entity.
|
167
|
-
*
|
168
|
-
* @abstract
|
169
|
-
* @param {Entity} entity - The entity to convert.
|
170
|
-
* @param {...unknown[]} additionalArgs - Additional arguments.
|
171
|
-
* @returns {this} - The instance of the ResponseMapper.
|
172
|
-
*/
|
173
|
-
abstract fromEntity(entity: Entity, ...additionalArgs: unknown[]): Promise<this>;
|
174
|
-
/**
|
175
|
-
* Converts the underlying DTO to a JSON object.
|
176
|
-
*
|
177
|
-
* @returns {this['_dto']} - The JSON object.
|
178
|
-
* @throws {Error} - Throws an error if the DTO is invalid.
|
179
|
-
*/
|
180
|
-
toDto(): Promise<this['_dto']>;
|
181
|
-
/**
|
182
|
-
* Serializes an entity to a JSON object.
|
183
|
-
*
|
184
|
-
* @param {Entity} entity - The entity to serialize.
|
185
|
-
* @returns {this['_dto']} - The JSON object.
|
186
|
-
* @throws {Error} - Throws an error if the DTO is invalid.
|
187
|
-
*/
|
188
|
-
serializeEntityToDto(...[entity, ...additionalArgs]: Parameters<this['fromEntity']>): Promise<this['_dto']>;
|
189
|
-
/**
|
190
|
-
* Populates entity mapper with DTO from an entity.
|
191
|
-
*
|
192
|
-
* @template T - A type that extends ResponseMapper.
|
193
|
-
* @template SV - A type that extends AnySchemaValidator.
|
194
|
-
* @param {MapperConstructor<T, SV>} this - The constructor of the T.
|
195
|
-
* @param {SV} schemaValidator - The schema provider.
|
196
|
-
* @param {T['_Entity']} entity - The entity to convert.
|
197
|
-
* @returns {T} - An instance of the T.
|
198
|
-
*/
|
199
|
-
static fromEntity<T extends ResponseMapper<unknown, SV>, SV extends AnySchemaValidator>(this: MapperConstructor<T, SV>, schemaValidator: SV, ...[entity, ...additionalArgs]: Parameters<T['fromEntity']>): Promise<T>;
|
200
|
-
/**
|
201
|
-
* Serializes an entity to a JSON object.
|
202
|
-
*
|
203
|
-
* @template T - A type that extends ResponseMapper.
|
204
|
-
* @template SV - A type that extends AnySchemaValidator.
|
205
|
-
* @param {MapperConstructor<T, SV>} this - The constructor of the T.
|
206
|
-
* @param {SV} schemaValidator - The schema provider.
|
207
|
-
* @param {T['_Entity']} entity - The entity to serialize.
|
208
|
-
* @returns {T['_dto']} - The JSON object.
|
209
|
-
* @throws {Error} - Throws an error if the DTO is invalid.
|
210
|
-
*/
|
211
|
-
static serializeEntityToDto<T extends ResponseMapper<unknown, SV>, SV extends AnySchemaValidator, DtoType extends T['_dto']>(this: MapperConstructor<T, SV>, schemaValidator: SV, ...[entity, ...additionalArgs]: Parameters<T['fromEntity']>): Promise<DtoType>;
|
212
|
-
}
|
213
|
-
|
214
|
-
export { RequestMapper, ResponseMapper };
|
17
|
+
export { mapper };
|
package/lib/mappers/index.js
CHANGED
@@ -20,224 +20,44 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
20
20
|
// src/mappers/index.ts
|
21
21
|
var mappers_exports = {};
|
22
22
|
__export(mappers_exports, {
|
23
|
-
|
24
|
-
ResponseMapper: () => ResponseMapper
|
23
|
+
mapper: () => mapper
|
25
24
|
});
|
26
25
|
module.exports = __toCommonJS(mappers_exports);
|
27
26
|
|
28
|
-
// src/mappers/
|
29
|
-
var import_validator2 = require("@forklaunch/validator");
|
30
|
-
|
31
|
-
// src/mappers/models/baseMapper.model.ts
|
27
|
+
// src/mappers/mapper.ts
|
32
28
|
var import_validator = require("@forklaunch/validator");
|
33
|
-
function
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
this.schemaValidator = schemaValidator;
|
61
|
-
}
|
62
|
-
/**
|
63
|
-
* Validates and sets the Data Transfer Object (DTO).
|
64
|
-
*
|
65
|
-
* @param {this['_dto']} dto - The Data Transfer Object (DTO).
|
66
|
-
* @throws {Error} - Throws an error if the DTO is invalid.
|
67
|
-
*/
|
68
|
-
set dto(_dto) {
|
69
|
-
const parsedSchema = this.schemaValidator.parse(
|
70
|
-
this.schemaValidator.schemify(this.schema),
|
71
|
-
_dto
|
72
|
-
);
|
73
|
-
if (!parsedSchema.ok) {
|
74
|
-
throw new Error((0, import_validator.prettyPrintParseErrors)(parsedSchema.errors, "DTO"));
|
75
|
-
}
|
76
|
-
this._dto = _dto;
|
77
|
-
}
|
78
|
-
/**
|
79
|
-
* Validates and gets the Data Transfer Object (DTO).
|
80
|
-
*
|
81
|
-
* @returns {this['_dto']} - The Data Transfer Object (DTO).
|
82
|
-
*/
|
83
|
-
get dto() {
|
84
|
-
return this._dto;
|
85
|
-
}
|
86
|
-
/**
|
87
|
-
* Gets the schema of a T.
|
88
|
-
*
|
89
|
-
* @template T - A type that extends BaseMapper.
|
90
|
-
* @template SV - A type that extends AnySchemaValidator.
|
91
|
-
* @param {MapperConstructor<T, SV>} this - The constructor of the T.
|
92
|
-
* @returns {T['schema']} - The schema of the T.
|
93
|
-
*/
|
94
|
-
static schema() {
|
95
|
-
return construct(this).schema;
|
96
|
-
}
|
97
|
-
};
|
98
|
-
|
99
|
-
// src/mappers/models/requestMapper.model.ts
|
100
|
-
var RequestMapper = class extends BaseMapper {
|
101
|
-
/**
|
102
|
-
* The entity type.
|
103
|
-
* @type {Entity}
|
104
|
-
* @protected
|
105
|
-
*/
|
106
|
-
_Entity;
|
107
|
-
/**
|
108
|
-
* Populates the DTO with data from a JSON object.
|
109
|
-
*
|
110
|
-
* @param {this['_dto']} json - The JSON object.
|
111
|
-
* @returns {Promise<this>} - The instance of the RequestMapper.
|
112
|
-
*/
|
113
|
-
fromDto(json) {
|
114
|
-
const parsedSchema = this.schemaValidator.parse(
|
115
|
-
this.schemaValidator.schemify(this.schema),
|
116
|
-
json
|
117
|
-
);
|
118
|
-
if (!parsedSchema.ok) {
|
119
|
-
throw new Error((0, import_validator2.prettyPrintParseErrors)(parsedSchema.errors, "DTO"));
|
120
|
-
}
|
121
|
-
this.dto = json;
|
122
|
-
return Promise.resolve(this);
|
123
|
-
}
|
124
|
-
/**
|
125
|
-
* Deserializes a JSON object to an entity.
|
126
|
-
*
|
127
|
-
* @param {this['_dto']} json - The JSON object.
|
128
|
-
* @param {...unknown[]} additionalArgs - Additional arguments.
|
129
|
-
* @returns {Entity} - The entity.
|
130
|
-
*/
|
131
|
-
deserializeDtoToEntity(json, ...additionalArgs) {
|
132
|
-
const result = this.fromDto(json);
|
133
|
-
return result.then((r) => r.toEntity(...additionalArgs));
|
134
|
-
}
|
135
|
-
/**
|
136
|
-
* Creates an instance of a RequestMapper from a JSON object.
|
137
|
-
*
|
138
|
-
* @template T - A type that extends RequestMapper.
|
139
|
-
* @template SV - A type that extends AnySchemaValidator.
|
140
|
-
* @template JsonType - The type of the JSON object.
|
141
|
-
* @param {MapperConstructor<T, SV>} this - The constructor of the T.
|
142
|
-
* @param {SV} schemaValidator - The schema provider.
|
143
|
-
* @param {JsonType} json - The JSON object.
|
144
|
-
* @returns {T} - An instance of the T.
|
145
|
-
*/
|
146
|
-
static fromDto(schemaValidator, json) {
|
147
|
-
return construct(this, schemaValidator).fromDto(json);
|
148
|
-
}
|
149
|
-
/**
|
150
|
-
* Deserializes a JSON object to an entity.
|
151
|
-
*
|
152
|
-
* @template T - A type that extends RequestMapper.
|
153
|
-
* @template SV - A type that extends AnySchemaValidator.
|
154
|
-
* @template JsonType - The type of the JSON object.
|
155
|
-
* @param {MapperConstructor<T, SV>} this - The constructor of the T.
|
156
|
-
* @param {SV} schemaValidator - The schema provider.
|
157
|
-
* @param {JsonType} json - The JSON object.
|
158
|
-
* @param {...unknown[]} additionalArgs - Additional arguments.
|
159
|
-
* @returns {T['_Entity']} - The entity.
|
160
|
-
*/
|
161
|
-
static deserializeDtoToEntity(schemaValidator, json, ...additionalArgs) {
|
162
|
-
const result = construct(this, schemaValidator).fromDto(json);
|
163
|
-
return result.then((r) => r.toEntity(...additionalArgs));
|
164
|
-
}
|
165
|
-
};
|
166
|
-
|
167
|
-
// src/mappers/models/responseMapper.model.ts
|
168
|
-
var import_validator3 = require("@forklaunch/validator");
|
169
|
-
var ResponseMapper = class extends BaseMapper {
|
170
|
-
/**
|
171
|
-
* The entity type.
|
172
|
-
* @type {Entity}
|
173
|
-
* @protected
|
174
|
-
*/
|
175
|
-
_Entity;
|
176
|
-
/**
|
177
|
-
* Converts the underlying DTO to a JSON object.
|
178
|
-
*
|
179
|
-
* @returns {this['_dto']} - The JSON object.
|
180
|
-
* @throws {Error} - Throws an error if the DTO is invalid.
|
181
|
-
*/
|
182
|
-
toDto() {
|
183
|
-
const parsedSchema = this.schemaValidator.parse(
|
184
|
-
this.schemaValidator.schemify(this.schema),
|
185
|
-
this.dto
|
186
|
-
);
|
187
|
-
if (!parsedSchema.ok) {
|
188
|
-
throw new Error((0, import_validator3.prettyPrintParseErrors)(parsedSchema.errors, "DTO"));
|
29
|
+
function mapper(schemaValidator, dtoSchema, _entityConstructor, mapperDefinition) {
|
30
|
+
const sv = schemaValidator;
|
31
|
+
return {
|
32
|
+
schema: dtoSchema,
|
33
|
+
toEntity: async (dto, ...args) => {
|
34
|
+
if (!("toEntity" in mapperDefinition)) {
|
35
|
+
throw new Error("toEntity not found in mapperDefinition");
|
36
|
+
}
|
37
|
+
const parsedSchema = sv.parse(sv.schemify(dtoSchema), dto);
|
38
|
+
if (!parsedSchema.ok) {
|
39
|
+
throw new Error((0, import_validator.prettyPrintParseErrors)(parsedSchema.errors, "DTO"));
|
40
|
+
}
|
41
|
+
return mapperDefinition.toEntity(
|
42
|
+
dto,
|
43
|
+
...args
|
44
|
+
);
|
45
|
+
},
|
46
|
+
toDto: async (entity, ...args) => {
|
47
|
+
if (!("toDto" in mapperDefinition)) {
|
48
|
+
throw new Error("toDto not found in mapperDefinition");
|
49
|
+
}
|
50
|
+
const dto = await mapperDefinition.toDto(entity, ...args);
|
51
|
+
const parsedSchema = sv.parse(sv.schemify(dtoSchema), dto);
|
52
|
+
if (!parsedSchema.ok) {
|
53
|
+
throw new Error((0, import_validator.prettyPrintParseErrors)(parsedSchema.errors, "DTO"));
|
54
|
+
}
|
55
|
+
return dto;
|
189
56
|
}
|
190
|
-
|
191
|
-
|
192
|
-
/**
|
193
|
-
* Serializes an entity to a JSON object.
|
194
|
-
*
|
195
|
-
* @param {Entity} entity - The entity to serialize.
|
196
|
-
* @returns {this['_dto']} - The JSON object.
|
197
|
-
* @throws {Error} - Throws an error if the DTO is invalid.
|
198
|
-
*/
|
199
|
-
serializeEntityToDto(...[entity, ...additionalArgs]) {
|
200
|
-
const result = this.fromEntity(entity, ...additionalArgs);
|
201
|
-
return result.then((r) => r.toDto());
|
202
|
-
}
|
203
|
-
/**
|
204
|
-
* Populates entity mapper with DTO from an entity.
|
205
|
-
*
|
206
|
-
* @template T - A type that extends ResponseMapper.
|
207
|
-
* @template SV - A type that extends AnySchemaValidator.
|
208
|
-
* @param {MapperConstructor<T, SV>} this - The constructor of the T.
|
209
|
-
* @param {SV} schemaValidator - The schema provider.
|
210
|
-
* @param {T['_Entity']} entity - The entity to convert.
|
211
|
-
* @returns {T} - An instance of the T.
|
212
|
-
*/
|
213
|
-
static fromEntity(schemaValidator, ...[entity, ...additionalArgs]) {
|
214
|
-
return construct(this, schemaValidator).fromEntity(
|
215
|
-
entity,
|
216
|
-
...additionalArgs
|
217
|
-
);
|
218
|
-
}
|
219
|
-
/**
|
220
|
-
* Serializes an entity to a JSON object.
|
221
|
-
*
|
222
|
-
* @template T - A type that extends ResponseMapper.
|
223
|
-
* @template SV - A type that extends AnySchemaValidator.
|
224
|
-
* @param {MapperConstructor<T, SV>} this - The constructor of the T.
|
225
|
-
* @param {SV} schemaValidator - The schema provider.
|
226
|
-
* @param {T['_Entity']} entity - The entity to serialize.
|
227
|
-
* @returns {T['_dto']} - The JSON object.
|
228
|
-
* @throws {Error} - Throws an error if the DTO is invalid.
|
229
|
-
*/
|
230
|
-
static serializeEntityToDto(schemaValidator, ...[entity, ...additionalArgs]) {
|
231
|
-
const result = construct(this, schemaValidator).fromEntity(
|
232
|
-
entity,
|
233
|
-
...additionalArgs
|
234
|
-
);
|
235
|
-
return result.then((r) => r.toDto());
|
236
|
-
}
|
237
|
-
};
|
57
|
+
};
|
58
|
+
}
|
238
59
|
// Annotate the CommonJS export names for ESM import in node:
|
239
60
|
0 && (module.exports = {
|
240
|
-
|
241
|
-
ResponseMapper
|
61
|
+
mapper
|
242
62
|
});
|
243
63
|
//# sourceMappingURL=index.js.map
|
package/lib/mappers/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/mappers/index.ts","../../src/mappers/models/requestMapper.model.ts","../../src/mappers/models/baseMapper.model.ts","../../src/mappers/models/responseMapper.model.ts"],"sourcesContent":["export * from './models/requestMapper.model';\nexport * from './models/responseMapper.model';\n","import {\n AnySchemaValidator,\n prettyPrintParseErrors\n} from '@forklaunch/validator';\nimport { MapperConstructor } from '../interfaces/mappers.interface';\nimport { BaseMapper, construct } from './baseMapper.model';\n\n/**\n * Abstract class representing a request entity mapper.\n *\n * @template Entity - A type that extends SqlBaseEntity.\n * @template SV - A type that extends AnySchemaValidator.\n * @extends {BaseMapper<SV>}\n */\nexport abstract class RequestMapper<\n Entity,\n SV extends AnySchemaValidator\n> extends BaseMapper<SV> {\n /**\n * The entity type.\n * @type {Entity}\n * @protected\n */\n _Entity!: Entity;\n\n /**\n * Converts the underlying DTO to an entity.\n *\n * @abstract\n * @param {...unknown[]} additionalArgs - Additional arguments.\n * @returns {Promise<Entity>} - The entity.\n */\n abstract toEntity(...additionalArgs: unknown[]): Promise<Entity>;\n\n /**\n * Populates the DTO with data from a JSON object.\n *\n * @param {this['_dto']} json - The JSON object.\n * @returns {Promise<this>} - The instance of the RequestMapper.\n */\n fromDto(json: this['_dto']): Promise<this> {\n const parsedSchema = this.schemaValidator.parse(\n this.schemaValidator.schemify(this.schema),\n json\n );\n if (!parsedSchema.ok) {\n throw new Error(prettyPrintParseErrors(parsedSchema.errors, 'DTO'));\n }\n this.dto = json;\n return Promise.resolve(this);\n }\n\n /**\n * Deserializes a JSON object to an entity.\n *\n * @param {this['_dto']} json - The JSON object.\n * @param {...unknown[]} additionalArgs - Additional arguments.\n * @returns {Entity} - The entity.\n */\n deserializeDtoToEntity(\n json: this['_dto'],\n ...additionalArgs: Parameters<this['toEntity']>\n ): Promise<Entity> {\n const result = this.fromDto(json);\n return result.then((r) => r.toEntity(...additionalArgs));\n }\n\n /**\n * Creates an instance of a RequestMapper from a JSON object.\n *\n * @template T - A type that extends RequestMapper.\n * @template SV - A type that extends AnySchemaValidator.\n * @template JsonType - The type of the JSON object.\n * @param {MapperConstructor<T, SV>} this - The constructor of the T.\n * @param {SV} schemaValidator - The schema provider.\n * @param {JsonType} json - The JSON object.\n * @returns {T} - An instance of the T.\n */\n static fromDto<\n T extends RequestMapper<unknown, SV>,\n SV extends AnySchemaValidator,\n JsonType extends T['_dto']\n >(\n this: MapperConstructor<T, SV>,\n schemaValidator: SV,\n json: JsonType\n ): Promise<T> {\n return construct(this, schemaValidator).fromDto(json);\n }\n\n /**\n * Deserializes a JSON object to an entity.\n *\n * @template T - A type that extends RequestMapper.\n * @template SV - A type that extends AnySchemaValidator.\n * @template JsonType - The type of the JSON object.\n * @param {MapperConstructor<T, SV>} this - The constructor of the T.\n * @param {SV} schemaValidator - The schema provider.\n * @param {JsonType} json - The JSON object.\n * @param {...unknown[]} additionalArgs - Additional arguments.\n * @returns {T['_Entity']} - The entity.\n */\n static deserializeDtoToEntity<\n T extends RequestMapper<unknown, SV>,\n SV extends AnySchemaValidator,\n JsonType extends T['_dto']\n >(\n this: MapperConstructor<T, SV>,\n schemaValidator: SV,\n json: JsonType,\n ...additionalArgs: Parameters<T['toEntity']>\n ): Promise<T['_Entity']> {\n const result = construct(this, schemaValidator).fromDto(json);\n return result.then((r) => r.toEntity(...additionalArgs));\n }\n}\n","import {\n AnySchemaValidator,\n prettyPrintParseErrors,\n Schema,\n SchemaValidator\n} from '@forklaunch/validator';\nimport { MapperSchemaValidatorObject } from '../../../../internal/src/types/mappers.types';\nimport { MapperConstructor } from '../interfaces/mappers.interface';\n\n/**\n * Constructs an instance of a T.\n *\n * @template T - A type that extends BaseMapper.\n * @template SV - A type that extends AnySchemaValidator.\n * @param {MapperConstructor<T, SV>} self - The constructor of the T.\n * @param {SV} [schemaValidator] - The optional schema validator.\n * @returns {T} - An instance of the T.\n */\nexport function construct<T, SV extends AnySchemaValidator>(\n self: MapperConstructor<T, SV>,\n schemaValidator?: SV\n): T {\n return new self(schemaValidator || ({} as SV));\n}\n\n/**\n * Abstract class representing a base entity mapper.\n *\n * @template SV - A type that extends AnySchemaValidator.\n */\nexport abstract class BaseMapper<SV extends AnySchemaValidator> {\n /**\n * The schema validator exact type.\n * @type {SV}\n * @protected\n */\n _SV!: SV;\n\n /**\n * The schema validator as a general type.\n * @type {SchemaValidator}\n * @protected\n */\n protected schemaValidator: SchemaValidator;\n\n /**\n * The schema definition.\n * @type {MapperSchemaValidatorObject<SV>}\n * @abstract\n */\n abstract schema: MapperSchemaValidatorObject<SV>;\n\n /**\n * The Data Transfer Object (DTO).\n * @type {Schema<this['schema'], SV>}\n */\n _dto!: Schema<this['schema'], SV>;\n\n /**\n * Creates an instance of BaseMapper.\n *\n * @param {SV} schemaValidator - The schema provider.\n */\n constructor(schemaValidator: SV) {\n this.schemaValidator = schemaValidator as unknown as SchemaValidator;\n }\n\n /**\n * Validates and sets the Data Transfer Object (DTO).\n *\n * @param {this['_dto']} dto - The Data Transfer Object (DTO).\n * @throws {Error} - Throws an error if the DTO is invalid.\n */\n set dto(_dto: this['_dto']) {\n const parsedSchema = this.schemaValidator.parse(\n this.schemaValidator.schemify(this.schema),\n _dto\n );\n if (!parsedSchema.ok) {\n throw new Error(prettyPrintParseErrors(parsedSchema.errors, 'DTO'));\n }\n this._dto = _dto as unknown as Schema<this['schema'], SV>;\n }\n\n /**\n * Validates and gets the Data Transfer Object (DTO).\n *\n * @returns {this['_dto']} - The Data Transfer Object (DTO).\n */\n get dto(): this['_dto'] {\n return this._dto as unknown as this['_dto'];\n }\n\n /**\n * Gets the schema of a T.\n *\n * @template T - A type that extends BaseMapper.\n * @template SV - A type that extends AnySchemaValidator.\n * @param {MapperConstructor<T, SV>} this - The constructor of the T.\n * @returns {T['schema']} - The schema of the T.\n */\n static schema<T extends BaseMapper<SV>, SV extends AnySchemaValidator>(\n this: MapperConstructor<T, SV>\n ): T['schema'] {\n return construct(this).schema;\n }\n}\n","import {\n AnySchemaValidator,\n prettyPrintParseErrors\n} from '@forklaunch/validator';\nimport { MapperConstructor } from '../interfaces/mappers.interface';\nimport { BaseMapper, construct } from './baseMapper.model';\n\n/**\n * Abstract class representing a response entity mapper.\n *\n * @template Entity - A type that extends SqlBaseEntity.\n * @template SV - A type that extends AnySchemaValidator.\n * @extends {BaseMapper<SV>}\n */\nexport abstract class ResponseMapper<\n Entity,\n SV extends AnySchemaValidator\n> extends BaseMapper<SV> {\n /**\n * The entity type.\n * @type {Entity}\n * @protected\n */\n _Entity!: Entity;\n\n /**\n * Populates entity mapper with DTO from an entity.\n *\n * @abstract\n * @param {Entity} entity - The entity to convert.\n * @param {...unknown[]} additionalArgs - Additional arguments.\n * @returns {this} - The instance of the ResponseMapper.\n */\n abstract fromEntity(\n entity: Entity,\n ...additionalArgs: unknown[]\n ): Promise<this>;\n\n /**\n * Converts the underlying DTO to a JSON object.\n *\n * @returns {this['_dto']} - The JSON object.\n * @throws {Error} - Throws an error if the DTO is invalid.\n */\n toDto(): Promise<this['_dto']> {\n const parsedSchema = this.schemaValidator.parse(\n this.schemaValidator.schemify(this.schema),\n this.dto\n );\n if (!parsedSchema.ok) {\n throw new Error(prettyPrintParseErrors(parsedSchema.errors, 'DTO'));\n }\n return Promise.resolve(this.dto) as unknown as Promise<this['_dto']>;\n }\n\n /**\n * Serializes an entity to a JSON object.\n *\n * @param {Entity} entity - The entity to serialize.\n * @returns {this['_dto']} - The JSON object.\n * @throws {Error} - Throws an error if the DTO is invalid.\n */\n serializeEntityToDto(\n ...[entity, ...additionalArgs]: Parameters<this['fromEntity']>\n ): Promise<this['_dto']> {\n const result = this.fromEntity(entity, ...additionalArgs);\n return result.then((r) => r.toDto());\n }\n\n /**\n * Populates entity mapper with DTO from an entity.\n *\n * @template T - A type that extends ResponseMapper.\n * @template SV - A type that extends AnySchemaValidator.\n * @param {MapperConstructor<T, SV>} this - The constructor of the T.\n * @param {SV} schemaValidator - The schema provider.\n * @param {T['_Entity']} entity - The entity to convert.\n * @returns {T} - An instance of the T.\n */\n static fromEntity<\n T extends ResponseMapper<unknown, SV>,\n SV extends AnySchemaValidator\n >(\n this: MapperConstructor<T, SV>,\n schemaValidator: SV,\n ...[entity, ...additionalArgs]: Parameters<T['fromEntity']>\n ): Promise<T> {\n return construct(this, schemaValidator).fromEntity(\n entity,\n ...additionalArgs\n );\n }\n\n /**\n * Serializes an entity to a JSON object.\n *\n * @template T - A type that extends ResponseMapper.\n * @template SV - A type that extends AnySchemaValidator.\n * @param {MapperConstructor<T, SV>} this - The constructor of the T.\n * @param {SV} schemaValidator - The schema provider.\n * @param {T['_Entity']} entity - The entity to serialize.\n * @returns {T['_dto']} - The JSON object.\n * @throws {Error} - Throws an error if the DTO is invalid.\n */\n static serializeEntityToDto<\n T extends ResponseMapper<unknown, SV>,\n SV extends AnySchemaValidator,\n DtoType extends T['_dto']\n >(\n this: MapperConstructor<T, SV>,\n schemaValidator: SV,\n ...[entity, ...additionalArgs]: Parameters<T['fromEntity']>\n ): Promise<DtoType> {\n const result = construct(this, schemaValidator).fromEntity(\n entity,\n ...additionalArgs\n );\n return result.then((r) => r.toDto()) as Promise<DtoType>;\n }\n}\n"],"mappings":"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","names":["import_validator","import_validator"]}
|
1
|
+
{"version":3,"sources":["../../src/mappers/index.ts","../../src/mappers/mapper.ts"],"sourcesContent":["export * from './mapper';\n","import {\n AnySchemaValidator,\n IdiomaticSchema,\n prettyPrintParseErrors,\n Schema,\n SchemaValidator\n} from '@forklaunch/validator';\nimport { Constructor } from '@mikro-orm/core';\n\nexport function mapper<\n SV extends AnySchemaValidator,\n DtoSchema extends IdiomaticSchema<SV>,\n Entity,\n AdditionalArgs extends unknown[] = []\n>(\n schemaValidator: SV,\n dtoSchema: DtoSchema,\n _entityConstructor: Constructor<Entity>,\n mapperDefinition:\n | {\n toEntity: (\n dto: Schema<DtoSchema, SV>,\n ...args: AdditionalArgs\n ) => Promise<Entity>;\n }\n | {\n toDto: (\n entity: Entity,\n ...args: AdditionalArgs\n ) => Promise<Schema<DtoSchema, SV>>;\n }\n | {\n toEntity: (\n dto: Schema<DtoSchema, SV>,\n ...args: AdditionalArgs\n ) => Promise<Entity>;\n toDto: (\n entity: Entity,\n ...args: AdditionalArgs\n ) => Promise<Schema<DtoSchema, SV>>;\n }\n) {\n const sv = schemaValidator as SchemaValidator;\n return {\n schema: dtoSchema,\n toEntity: async (dto: Schema<DtoSchema, SV>, ...args: AdditionalArgs) => {\n if (!('toEntity' in mapperDefinition)) {\n throw new Error('toEntity not found in mapperDefinition');\n }\n const parsedSchema = sv.parse(sv.schemify(dtoSchema), dto);\n if (!parsedSchema.ok) {\n throw new Error(prettyPrintParseErrors(parsedSchema.errors, 'DTO'));\n }\n return mapperDefinition.toEntity(\n dto as Schema<DtoSchema, SV>,\n ...(args as AdditionalArgs)\n );\n },\n toDto: async (entity: Entity, ...args: AdditionalArgs) => {\n if (!('toDto' in mapperDefinition)) {\n throw new Error('toDto not found in mapperDefinition');\n }\n const dto = await mapperDefinition.toDto(entity, ...args);\n\n const parsedSchema = sv.parse(sv.schemify(dtoSchema), dto);\n if (!parsedSchema.ok) {\n throw new Error(prettyPrintParseErrors(parsedSchema.errors, 'DTO'));\n }\n return dto;\n }\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,uBAMO;AAGA,SAAS,OAMd,iBACA,WACA,oBACA,kBAuBA;AACA,QAAM,KAAK;AACX,SAAO;AAAA,IACL,QAAQ;AAAA,IACR,UAAU,OAAO,QAA+B,SAAyB;AACvE,UAAI,EAAE,cAAc,mBAAmB;AACrC,cAAM,IAAI,MAAM,wCAAwC;AAAA,MAC1D;AACA,YAAM,eAAe,GAAG,MAAM,GAAG,SAAS,SAAS,GAAG,GAAG;AACzD,UAAI,CAAC,aAAa,IAAI;AACpB,cAAM,IAAI,UAAM,yCAAuB,aAAa,QAAQ,KAAK,CAAC;AAAA,MACpE;AACA,aAAO,iBAAiB;AAAA,QACtB;AAAA,QACA,GAAI;AAAA,MACN;AAAA,IACF;AAAA,IACA,OAAO,OAAO,WAAmB,SAAyB;AACxD,UAAI,EAAE,WAAW,mBAAmB;AAClC,cAAM,IAAI,MAAM,qCAAqC;AAAA,MACvD;AACA,YAAM,MAAM,MAAM,iBAAiB,MAAM,QAAQ,GAAG,IAAI;AAExD,YAAM,eAAe,GAAG,MAAM,GAAG,SAAS,SAAS,GAAG,GAAG;AACzD,UAAI,CAAC,aAAa,IAAI;AACpB,cAAM,IAAI,UAAM,yCAAuB,aAAa,QAAQ,KAAK,CAAC;AAAA,MACpE;AACA,aAAO;AAAA,IACT;AAAA,EACF;AACF;","names":[]}
|
package/lib/mappers/index.mjs
CHANGED
@@ -1,221 +1,38 @@
|
|
1
|
-
// src/mappers/
|
2
|
-
import {
|
3
|
-
prettyPrintParseErrors as prettyPrintParseErrors2
|
4
|
-
} from "@forklaunch/validator";
|
5
|
-
|
6
|
-
// src/mappers/models/baseMapper.model.ts
|
1
|
+
// src/mappers/mapper.ts
|
7
2
|
import {
|
8
3
|
prettyPrintParseErrors
|
9
4
|
} from "@forklaunch/validator";
|
10
|
-
function
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
this.schemaValidator = schemaValidator;
|
38
|
-
}
|
39
|
-
/**
|
40
|
-
* Validates and sets the Data Transfer Object (DTO).
|
41
|
-
*
|
42
|
-
* @param {this['_dto']} dto - The Data Transfer Object (DTO).
|
43
|
-
* @throws {Error} - Throws an error if the DTO is invalid.
|
44
|
-
*/
|
45
|
-
set dto(_dto) {
|
46
|
-
const parsedSchema = this.schemaValidator.parse(
|
47
|
-
this.schemaValidator.schemify(this.schema),
|
48
|
-
_dto
|
49
|
-
);
|
50
|
-
if (!parsedSchema.ok) {
|
51
|
-
throw new Error(prettyPrintParseErrors(parsedSchema.errors, "DTO"));
|
5
|
+
function mapper(schemaValidator, dtoSchema, _entityConstructor, mapperDefinition) {
|
6
|
+
const sv = schemaValidator;
|
7
|
+
return {
|
8
|
+
schema: dtoSchema,
|
9
|
+
toEntity: async (dto, ...args) => {
|
10
|
+
if (!("toEntity" in mapperDefinition)) {
|
11
|
+
throw new Error("toEntity not found in mapperDefinition");
|
12
|
+
}
|
13
|
+
const parsedSchema = sv.parse(sv.schemify(dtoSchema), dto);
|
14
|
+
if (!parsedSchema.ok) {
|
15
|
+
throw new Error(prettyPrintParseErrors(parsedSchema.errors, "DTO"));
|
16
|
+
}
|
17
|
+
return mapperDefinition.toEntity(
|
18
|
+
dto,
|
19
|
+
...args
|
20
|
+
);
|
21
|
+
},
|
22
|
+
toDto: async (entity, ...args) => {
|
23
|
+
if (!("toDto" in mapperDefinition)) {
|
24
|
+
throw new Error("toDto not found in mapperDefinition");
|
25
|
+
}
|
26
|
+
const dto = await mapperDefinition.toDto(entity, ...args);
|
27
|
+
const parsedSchema = sv.parse(sv.schemify(dtoSchema), dto);
|
28
|
+
if (!parsedSchema.ok) {
|
29
|
+
throw new Error(prettyPrintParseErrors(parsedSchema.errors, "DTO"));
|
30
|
+
}
|
31
|
+
return dto;
|
52
32
|
}
|
53
|
-
|
54
|
-
|
55
|
-
/**
|
56
|
-
* Validates and gets the Data Transfer Object (DTO).
|
57
|
-
*
|
58
|
-
* @returns {this['_dto']} - The Data Transfer Object (DTO).
|
59
|
-
*/
|
60
|
-
get dto() {
|
61
|
-
return this._dto;
|
62
|
-
}
|
63
|
-
/**
|
64
|
-
* Gets the schema of a T.
|
65
|
-
*
|
66
|
-
* @template T - A type that extends BaseMapper.
|
67
|
-
* @template SV - A type that extends AnySchemaValidator.
|
68
|
-
* @param {MapperConstructor<T, SV>} this - The constructor of the T.
|
69
|
-
* @returns {T['schema']} - The schema of the T.
|
70
|
-
*/
|
71
|
-
static schema() {
|
72
|
-
return construct(this).schema;
|
73
|
-
}
|
74
|
-
};
|
75
|
-
|
76
|
-
// src/mappers/models/requestMapper.model.ts
|
77
|
-
var RequestMapper = class extends BaseMapper {
|
78
|
-
/**
|
79
|
-
* The entity type.
|
80
|
-
* @type {Entity}
|
81
|
-
* @protected
|
82
|
-
*/
|
83
|
-
_Entity;
|
84
|
-
/**
|
85
|
-
* Populates the DTO with data from a JSON object.
|
86
|
-
*
|
87
|
-
* @param {this['_dto']} json - The JSON object.
|
88
|
-
* @returns {Promise<this>} - The instance of the RequestMapper.
|
89
|
-
*/
|
90
|
-
fromDto(json) {
|
91
|
-
const parsedSchema = this.schemaValidator.parse(
|
92
|
-
this.schemaValidator.schemify(this.schema),
|
93
|
-
json
|
94
|
-
);
|
95
|
-
if (!parsedSchema.ok) {
|
96
|
-
throw new Error(prettyPrintParseErrors2(parsedSchema.errors, "DTO"));
|
97
|
-
}
|
98
|
-
this.dto = json;
|
99
|
-
return Promise.resolve(this);
|
100
|
-
}
|
101
|
-
/**
|
102
|
-
* Deserializes a JSON object to an entity.
|
103
|
-
*
|
104
|
-
* @param {this['_dto']} json - The JSON object.
|
105
|
-
* @param {...unknown[]} additionalArgs - Additional arguments.
|
106
|
-
* @returns {Entity} - The entity.
|
107
|
-
*/
|
108
|
-
deserializeDtoToEntity(json, ...additionalArgs) {
|
109
|
-
const result = this.fromDto(json);
|
110
|
-
return result.then((r) => r.toEntity(...additionalArgs));
|
111
|
-
}
|
112
|
-
/**
|
113
|
-
* Creates an instance of a RequestMapper from a JSON object.
|
114
|
-
*
|
115
|
-
* @template T - A type that extends RequestMapper.
|
116
|
-
* @template SV - A type that extends AnySchemaValidator.
|
117
|
-
* @template JsonType - The type of the JSON object.
|
118
|
-
* @param {MapperConstructor<T, SV>} this - The constructor of the T.
|
119
|
-
* @param {SV} schemaValidator - The schema provider.
|
120
|
-
* @param {JsonType} json - The JSON object.
|
121
|
-
* @returns {T} - An instance of the T.
|
122
|
-
*/
|
123
|
-
static fromDto(schemaValidator, json) {
|
124
|
-
return construct(this, schemaValidator).fromDto(json);
|
125
|
-
}
|
126
|
-
/**
|
127
|
-
* Deserializes a JSON object to an entity.
|
128
|
-
*
|
129
|
-
* @template T - A type that extends RequestMapper.
|
130
|
-
* @template SV - A type that extends AnySchemaValidator.
|
131
|
-
* @template JsonType - The type of the JSON object.
|
132
|
-
* @param {MapperConstructor<T, SV>} this - The constructor of the T.
|
133
|
-
* @param {SV} schemaValidator - The schema provider.
|
134
|
-
* @param {JsonType} json - The JSON object.
|
135
|
-
* @param {...unknown[]} additionalArgs - Additional arguments.
|
136
|
-
* @returns {T['_Entity']} - The entity.
|
137
|
-
*/
|
138
|
-
static deserializeDtoToEntity(schemaValidator, json, ...additionalArgs) {
|
139
|
-
const result = construct(this, schemaValidator).fromDto(json);
|
140
|
-
return result.then((r) => r.toEntity(...additionalArgs));
|
141
|
-
}
|
142
|
-
};
|
143
|
-
|
144
|
-
// src/mappers/models/responseMapper.model.ts
|
145
|
-
import {
|
146
|
-
prettyPrintParseErrors as prettyPrintParseErrors3
|
147
|
-
} from "@forklaunch/validator";
|
148
|
-
var ResponseMapper = class extends BaseMapper {
|
149
|
-
/**
|
150
|
-
* The entity type.
|
151
|
-
* @type {Entity}
|
152
|
-
* @protected
|
153
|
-
*/
|
154
|
-
_Entity;
|
155
|
-
/**
|
156
|
-
* Converts the underlying DTO to a JSON object.
|
157
|
-
*
|
158
|
-
* @returns {this['_dto']} - The JSON object.
|
159
|
-
* @throws {Error} - Throws an error if the DTO is invalid.
|
160
|
-
*/
|
161
|
-
toDto() {
|
162
|
-
const parsedSchema = this.schemaValidator.parse(
|
163
|
-
this.schemaValidator.schemify(this.schema),
|
164
|
-
this.dto
|
165
|
-
);
|
166
|
-
if (!parsedSchema.ok) {
|
167
|
-
throw new Error(prettyPrintParseErrors3(parsedSchema.errors, "DTO"));
|
168
|
-
}
|
169
|
-
return Promise.resolve(this.dto);
|
170
|
-
}
|
171
|
-
/**
|
172
|
-
* Serializes an entity to a JSON object.
|
173
|
-
*
|
174
|
-
* @param {Entity} entity - The entity to serialize.
|
175
|
-
* @returns {this['_dto']} - The JSON object.
|
176
|
-
* @throws {Error} - Throws an error if the DTO is invalid.
|
177
|
-
*/
|
178
|
-
serializeEntityToDto(...[entity, ...additionalArgs]) {
|
179
|
-
const result = this.fromEntity(entity, ...additionalArgs);
|
180
|
-
return result.then((r) => r.toDto());
|
181
|
-
}
|
182
|
-
/**
|
183
|
-
* Populates entity mapper with DTO from an entity.
|
184
|
-
*
|
185
|
-
* @template T - A type that extends ResponseMapper.
|
186
|
-
* @template SV - A type that extends AnySchemaValidator.
|
187
|
-
* @param {MapperConstructor<T, SV>} this - The constructor of the T.
|
188
|
-
* @param {SV} schemaValidator - The schema provider.
|
189
|
-
* @param {T['_Entity']} entity - The entity to convert.
|
190
|
-
* @returns {T} - An instance of the T.
|
191
|
-
*/
|
192
|
-
static fromEntity(schemaValidator, ...[entity, ...additionalArgs]) {
|
193
|
-
return construct(this, schemaValidator).fromEntity(
|
194
|
-
entity,
|
195
|
-
...additionalArgs
|
196
|
-
);
|
197
|
-
}
|
198
|
-
/**
|
199
|
-
* Serializes an entity to a JSON object.
|
200
|
-
*
|
201
|
-
* @template T - A type that extends ResponseMapper.
|
202
|
-
* @template SV - A type that extends AnySchemaValidator.
|
203
|
-
* @param {MapperConstructor<T, SV>} this - The constructor of the T.
|
204
|
-
* @param {SV} schemaValidator - The schema provider.
|
205
|
-
* @param {T['_Entity']} entity - The entity to serialize.
|
206
|
-
* @returns {T['_dto']} - The JSON object.
|
207
|
-
* @throws {Error} - Throws an error if the DTO is invalid.
|
208
|
-
*/
|
209
|
-
static serializeEntityToDto(schemaValidator, ...[entity, ...additionalArgs]) {
|
210
|
-
const result = construct(this, schemaValidator).fromEntity(
|
211
|
-
entity,
|
212
|
-
...additionalArgs
|
213
|
-
);
|
214
|
-
return result.then((r) => r.toDto());
|
215
|
-
}
|
216
|
-
};
|
33
|
+
};
|
34
|
+
}
|
217
35
|
export {
|
218
|
-
|
219
|
-
ResponseMapper
|
36
|
+
mapper
|
220
37
|
};
|
221
38
|
//# sourceMappingURL=index.mjs.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/mappers/models/requestMapper.model.ts","../../src/mappers/models/baseMapper.model.ts","../../src/mappers/models/responseMapper.model.ts"],"sourcesContent":["import {\n AnySchemaValidator,\n prettyPrintParseErrors\n} from '@forklaunch/validator';\nimport { MapperConstructor } from '../interfaces/mappers.interface';\nimport { BaseMapper, construct } from './baseMapper.model';\n\n/**\n * Abstract class representing a request entity mapper.\n *\n * @template Entity - A type that extends SqlBaseEntity.\n * @template SV - A type that extends AnySchemaValidator.\n * @extends {BaseMapper<SV>}\n */\nexport abstract class RequestMapper<\n Entity,\n SV extends AnySchemaValidator\n> extends BaseMapper<SV> {\n /**\n * The entity type.\n * @type {Entity}\n * @protected\n */\n _Entity!: Entity;\n\n /**\n * Converts the underlying DTO to an entity.\n *\n * @abstract\n * @param {...unknown[]} additionalArgs - Additional arguments.\n * @returns {Promise<Entity>} - The entity.\n */\n abstract toEntity(...additionalArgs: unknown[]): Promise<Entity>;\n\n /**\n * Populates the DTO with data from a JSON object.\n *\n * @param {this['_dto']} json - The JSON object.\n * @returns {Promise<this>} - The instance of the RequestMapper.\n */\n fromDto(json: this['_dto']): Promise<this> {\n const parsedSchema = this.schemaValidator.parse(\n this.schemaValidator.schemify(this.schema),\n json\n );\n if (!parsedSchema.ok) {\n throw new Error(prettyPrintParseErrors(parsedSchema.errors, 'DTO'));\n }\n this.dto = json;\n return Promise.resolve(this);\n }\n\n /**\n * Deserializes a JSON object to an entity.\n *\n * @param {this['_dto']} json - The JSON object.\n * @param {...unknown[]} additionalArgs - Additional arguments.\n * @returns {Entity} - The entity.\n */\n deserializeDtoToEntity(\n json: this['_dto'],\n ...additionalArgs: Parameters<this['toEntity']>\n ): Promise<Entity> {\n const result = this.fromDto(json);\n return result.then((r) => r.toEntity(...additionalArgs));\n }\n\n /**\n * Creates an instance of a RequestMapper from a JSON object.\n *\n * @template T - A type that extends RequestMapper.\n * @template SV - A type that extends AnySchemaValidator.\n * @template JsonType - The type of the JSON object.\n * @param {MapperConstructor<T, SV>} this - The constructor of the T.\n * @param {SV} schemaValidator - The schema provider.\n * @param {JsonType} json - The JSON object.\n * @returns {T} - An instance of the T.\n */\n static fromDto<\n T extends RequestMapper<unknown, SV>,\n SV extends AnySchemaValidator,\n JsonType extends T['_dto']\n >(\n this: MapperConstructor<T, SV>,\n schemaValidator: SV,\n json: JsonType\n ): Promise<T> {\n return construct(this, schemaValidator).fromDto(json);\n }\n\n /**\n * Deserializes a JSON object to an entity.\n *\n * @template T - A type that extends RequestMapper.\n * @template SV - A type that extends AnySchemaValidator.\n * @template JsonType - The type of the JSON object.\n * @param {MapperConstructor<T, SV>} this - The constructor of the T.\n * @param {SV} schemaValidator - The schema provider.\n * @param {JsonType} json - The JSON object.\n * @param {...unknown[]} additionalArgs - Additional arguments.\n * @returns {T['_Entity']} - The entity.\n */\n static deserializeDtoToEntity<\n T extends RequestMapper<unknown, SV>,\n SV extends AnySchemaValidator,\n JsonType extends T['_dto']\n >(\n this: MapperConstructor<T, SV>,\n schemaValidator: SV,\n json: JsonType,\n ...additionalArgs: Parameters<T['toEntity']>\n ): Promise<T['_Entity']> {\n const result = construct(this, schemaValidator).fromDto(json);\n return result.then((r) => r.toEntity(...additionalArgs));\n }\n}\n","import {\n AnySchemaValidator,\n prettyPrintParseErrors,\n Schema,\n SchemaValidator\n} from '@forklaunch/validator';\nimport { MapperSchemaValidatorObject } from '../../../../internal/src/types/mappers.types';\nimport { MapperConstructor } from '../interfaces/mappers.interface';\n\n/**\n * Constructs an instance of a T.\n *\n * @template T - A type that extends BaseMapper.\n * @template SV - A type that extends AnySchemaValidator.\n * @param {MapperConstructor<T, SV>} self - The constructor of the T.\n * @param {SV} [schemaValidator] - The optional schema validator.\n * @returns {T} - An instance of the T.\n */\nexport function construct<T, SV extends AnySchemaValidator>(\n self: MapperConstructor<T, SV>,\n schemaValidator?: SV\n): T {\n return new self(schemaValidator || ({} as SV));\n}\n\n/**\n * Abstract class representing a base entity mapper.\n *\n * @template SV - A type that extends AnySchemaValidator.\n */\nexport abstract class BaseMapper<SV extends AnySchemaValidator> {\n /**\n * The schema validator exact type.\n * @type {SV}\n * @protected\n */\n _SV!: SV;\n\n /**\n * The schema validator as a general type.\n * @type {SchemaValidator}\n * @protected\n */\n protected schemaValidator: SchemaValidator;\n\n /**\n * The schema definition.\n * @type {MapperSchemaValidatorObject<SV>}\n * @abstract\n */\n abstract schema: MapperSchemaValidatorObject<SV>;\n\n /**\n * The Data Transfer Object (DTO).\n * @type {Schema<this['schema'], SV>}\n */\n _dto!: Schema<this['schema'], SV>;\n\n /**\n * Creates an instance of BaseMapper.\n *\n * @param {SV} schemaValidator - The schema provider.\n */\n constructor(schemaValidator: SV) {\n this.schemaValidator = schemaValidator as unknown as SchemaValidator;\n }\n\n /**\n * Validates and sets the Data Transfer Object (DTO).\n *\n * @param {this['_dto']} dto - The Data Transfer Object (DTO).\n * @throws {Error} - Throws an error if the DTO is invalid.\n */\n set dto(_dto: this['_dto']) {\n const parsedSchema = this.schemaValidator.parse(\n this.schemaValidator.schemify(this.schema),\n _dto\n );\n if (!parsedSchema.ok) {\n throw new Error(prettyPrintParseErrors(parsedSchema.errors, 'DTO'));\n }\n this._dto = _dto as unknown as Schema<this['schema'], SV>;\n }\n\n /**\n * Validates and gets the Data Transfer Object (DTO).\n *\n * @returns {this['_dto']} - The Data Transfer Object (DTO).\n */\n get dto(): this['_dto'] {\n return this._dto as unknown as this['_dto'];\n }\n\n /**\n * Gets the schema of a T.\n *\n * @template T - A type that extends BaseMapper.\n * @template SV - A type that extends AnySchemaValidator.\n * @param {MapperConstructor<T, SV>} this - The constructor of the T.\n * @returns {T['schema']} - The schema of the T.\n */\n static schema<T extends BaseMapper<SV>, SV extends AnySchemaValidator>(\n this: MapperConstructor<T, SV>\n ): T['schema'] {\n return construct(this).schema;\n }\n}\n","import {\n AnySchemaValidator,\n prettyPrintParseErrors\n} from '@forklaunch/validator';\nimport { MapperConstructor } from '../interfaces/mappers.interface';\nimport { BaseMapper, construct } from './baseMapper.model';\n\n/**\n * Abstract class representing a response entity mapper.\n *\n * @template Entity - A type that extends SqlBaseEntity.\n * @template SV - A type that extends AnySchemaValidator.\n * @extends {BaseMapper<SV>}\n */\nexport abstract class ResponseMapper<\n Entity,\n SV extends AnySchemaValidator\n> extends BaseMapper<SV> {\n /**\n * The entity type.\n * @type {Entity}\n * @protected\n */\n _Entity!: Entity;\n\n /**\n * Populates entity mapper with DTO from an entity.\n *\n * @abstract\n * @param {Entity} entity - The entity to convert.\n * @param {...unknown[]} additionalArgs - Additional arguments.\n * @returns {this} - The instance of the ResponseMapper.\n */\n abstract fromEntity(\n entity: Entity,\n ...additionalArgs: unknown[]\n ): Promise<this>;\n\n /**\n * Converts the underlying DTO to a JSON object.\n *\n * @returns {this['_dto']} - The JSON object.\n * @throws {Error} - Throws an error if the DTO is invalid.\n */\n toDto(): Promise<this['_dto']> {\n const parsedSchema = this.schemaValidator.parse(\n this.schemaValidator.schemify(this.schema),\n this.dto\n );\n if (!parsedSchema.ok) {\n throw new Error(prettyPrintParseErrors(parsedSchema.errors, 'DTO'));\n }\n return Promise.resolve(this.dto) as unknown as Promise<this['_dto']>;\n }\n\n /**\n * Serializes an entity to a JSON object.\n *\n * @param {Entity} entity - The entity to serialize.\n * @returns {this['_dto']} - The JSON object.\n * @throws {Error} - Throws an error if the DTO is invalid.\n */\n serializeEntityToDto(\n ...[entity, ...additionalArgs]: Parameters<this['fromEntity']>\n ): Promise<this['_dto']> {\n const result = this.fromEntity(entity, ...additionalArgs);\n return result.then((r) => r.toDto());\n }\n\n /**\n * Populates entity mapper with DTO from an entity.\n *\n * @template T - A type that extends ResponseMapper.\n * @template SV - A type that extends AnySchemaValidator.\n * @param {MapperConstructor<T, SV>} this - The constructor of the T.\n * @param {SV} schemaValidator - The schema provider.\n * @param {T['_Entity']} entity - The entity to convert.\n * @returns {T} - An instance of the T.\n */\n static fromEntity<\n T extends ResponseMapper<unknown, SV>,\n SV extends AnySchemaValidator\n >(\n this: MapperConstructor<T, SV>,\n schemaValidator: SV,\n ...[entity, ...additionalArgs]: Parameters<T['fromEntity']>\n ): Promise<T> {\n return construct(this, schemaValidator).fromEntity(\n entity,\n ...additionalArgs\n );\n }\n\n /**\n * Serializes an entity to a JSON object.\n *\n * @template T - A type that extends ResponseMapper.\n * @template SV - A type that extends AnySchemaValidator.\n * @param {MapperConstructor<T, SV>} this - The constructor of the T.\n * @param {SV} schemaValidator - The schema provider.\n * @param {T['_Entity']} entity - The entity to serialize.\n * @returns {T['_dto']} - The JSON object.\n * @throws {Error} - Throws an error if the DTO is invalid.\n */\n static serializeEntityToDto<\n T extends ResponseMapper<unknown, SV>,\n SV extends AnySchemaValidator,\n DtoType extends T['_dto']\n >(\n this: MapperConstructor<T, SV>,\n schemaValidator: SV,\n ...[entity, ...additionalArgs]: Parameters<T['fromEntity']>\n ): Promise<DtoType> {\n const result = construct(this, schemaValidator).fromEntity(\n entity,\n ...additionalArgs\n );\n return result.then((r) => r.toDto()) as Promise<DtoType>;\n }\n}\n"],"mappings":"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","names":["prettyPrintParseErrors","prettyPrintParseErrors","prettyPrintParseErrors","prettyPrintParseErrors"]}
|
1
|
+
{"version":3,"sources":["../../src/mappers/mapper.ts"],"sourcesContent":["import {\n AnySchemaValidator,\n IdiomaticSchema,\n prettyPrintParseErrors,\n Schema,\n SchemaValidator\n} from '@forklaunch/validator';\nimport { Constructor } from '@mikro-orm/core';\n\nexport function mapper<\n SV extends AnySchemaValidator,\n DtoSchema extends IdiomaticSchema<SV>,\n Entity,\n AdditionalArgs extends unknown[] = []\n>(\n schemaValidator: SV,\n dtoSchema: DtoSchema,\n _entityConstructor: Constructor<Entity>,\n mapperDefinition:\n | {\n toEntity: (\n dto: Schema<DtoSchema, SV>,\n ...args: AdditionalArgs\n ) => Promise<Entity>;\n }\n | {\n toDto: (\n entity: Entity,\n ...args: AdditionalArgs\n ) => Promise<Schema<DtoSchema, SV>>;\n }\n | {\n toEntity: (\n dto: Schema<DtoSchema, SV>,\n ...args: AdditionalArgs\n ) => Promise<Entity>;\n toDto: (\n entity: Entity,\n ...args: AdditionalArgs\n ) => Promise<Schema<DtoSchema, SV>>;\n }\n) {\n const sv = schemaValidator as SchemaValidator;\n return {\n schema: dtoSchema,\n toEntity: async (dto: Schema<DtoSchema, SV>, ...args: AdditionalArgs) => {\n if (!('toEntity' in mapperDefinition)) {\n throw new Error('toEntity not found in mapperDefinition');\n }\n const parsedSchema = sv.parse(sv.schemify(dtoSchema), dto);\n if (!parsedSchema.ok) {\n throw new Error(prettyPrintParseErrors(parsedSchema.errors, 'DTO'));\n }\n return mapperDefinition.toEntity(\n dto as Schema<DtoSchema, SV>,\n ...(args as AdditionalArgs)\n );\n },\n toDto: async (entity: Entity, ...args: AdditionalArgs) => {\n if (!('toDto' in mapperDefinition)) {\n throw new Error('toDto not found in mapperDefinition');\n }\n const dto = await mapperDefinition.toDto(entity, ...args);\n\n const parsedSchema = sv.parse(sv.schemify(dtoSchema), dto);\n if (!parsedSchema.ok) {\n throw new Error(prettyPrintParseErrors(parsedSchema.errors, 'DTO'));\n }\n return dto;\n }\n };\n}\n"],"mappings":";AAAA;AAAA,EAGE;AAAA,OAGK;AAGA,SAAS,OAMd,iBACA,WACA,oBACA,kBAuBA;AACA,QAAM,KAAK;AACX,SAAO;AAAA,IACL,QAAQ;AAAA,IACR,UAAU,OAAO,QAA+B,SAAyB;AACvE,UAAI,EAAE,cAAc,mBAAmB;AACrC,cAAM,IAAI,MAAM,wCAAwC;AAAA,MAC1D;AACA,YAAM,eAAe,GAAG,MAAM,GAAG,SAAS,SAAS,GAAG,GAAG;AACzD,UAAI,CAAC,aAAa,IAAI;AACpB,cAAM,IAAI,MAAM,uBAAuB,aAAa,QAAQ,KAAK,CAAC;AAAA,MACpE;AACA,aAAO,iBAAiB;AAAA,QACtB;AAAA,QACA,GAAI;AAAA,MACN;AAAA,IACF;AAAA,IACA,OAAO,OAAO,WAAmB,SAAyB;AACxD,UAAI,EAAE,WAAW,mBAAmB;AAClC,cAAM,IAAI,MAAM,qCAAqC;AAAA,MACvD;AACA,YAAM,MAAM,MAAM,iBAAiB,MAAM,QAAQ,GAAG,IAAI;AAExD,YAAM,eAAe,GAAG,MAAM,GAAG,SAAS,SAAS,GAAG,GAAG;AACzD,UAAI,CAAC,aAAa,IAAI;AACpB,cAAM,IAAI,MAAM,uBAAuB,aAAa,QAAQ,KAAK,CAAC;AAAA,MACpE;AACA,aAAO;AAAA,IACT;AAAA,EACF;AACF;","names":[]}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@forklaunch/core",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.14.0",
|
4
4
|
"description": "forklaunch-js core package. Contains useful building blocks.",
|
5
5
|
"homepage": "https://github.com/forklaunch/forklaunch-js#readme",
|
6
6
|
"bugs": {
|
@@ -66,8 +66,8 @@
|
|
66
66
|
"dependencies": {
|
67
67
|
"@forklaunch/fastmcp-fork": "^1.0.5",
|
68
68
|
"@forklaunch/opentelemetry-instrumentation-hyper-express": "0.0.5",
|
69
|
-
"@mikro-orm/core": "^6.
|
70
|
-
"@mikro-orm/mongodb": "^6.
|
69
|
+
"@mikro-orm/core": "^6.5.1",
|
70
|
+
"@mikro-orm/mongodb": "^6.5.1",
|
71
71
|
"@opentelemetry/api": "^1.9.0",
|
72
72
|
"@opentelemetry/api-logs": "^0.203.0",
|
73
73
|
"@opentelemetry/exporter-logs-otlp-http": "^0.203.0",
|
@@ -91,19 +91,19 @@
|
|
91
91
|
"pino-pretty": "^13.1.1",
|
92
92
|
"redis": "^5.8.2",
|
93
93
|
"uuid": "^11.1.0",
|
94
|
-
"@forklaunch/
|
95
|
-
"@forklaunch/
|
94
|
+
"@forklaunch/common": "0.6.0",
|
95
|
+
"@forklaunch/validator": "0.10.0"
|
96
96
|
},
|
97
97
|
"devDependencies": {
|
98
98
|
"@eslint/js": "^9.34.0",
|
99
|
-
"@scalar/express-api-reference": "^0.8.
|
99
|
+
"@scalar/express-api-reference": "^0.8.16",
|
100
100
|
"@types/cors": "^2.8.19",
|
101
101
|
"@types/jest": "^30.0.0",
|
102
102
|
"@types/qs": "^6.14.0",
|
103
103
|
"@types/uuid": "^10.0.0",
|
104
|
-
"@typescript/native-preview": "7.0.0-dev.
|
104
|
+
"@typescript/native-preview": "7.0.0-dev.20250827.1",
|
105
105
|
"globals": "^16.3.0",
|
106
|
-
"jest": "^30.
|
106
|
+
"jest": "^30.1.1",
|
107
107
|
"jose": "5.10.0",
|
108
108
|
"prettier": "^3.6.2",
|
109
109
|
"testcontainers": "^11.5.1",
|
@@ -112,7 +112,7 @@
|
|
112
112
|
"tsup": "^8.5.0",
|
113
113
|
"typedoc": "^0.28.11",
|
114
114
|
"typescript": "^5.9.2",
|
115
|
-
"typescript-eslint": "^8.
|
115
|
+
"typescript-eslint": "^8.41.0"
|
116
116
|
},
|
117
117
|
"scripts": {
|
118
118
|
"build": "tsgo --noEmit && tsup ./src/cache/index.ts ./src/controllers/index.ts ./src/mappers/index.ts ./src/objectstore/index.ts ./src/persistence/index.ts ./src/http/index.ts ./src/services/index.ts --format cjs,esm --no-splitting --dts --tsconfig tsconfig.json --out-dir lib --clean --sourcemap",
|