@forklaunch/core 0.13.9 → 0.14.1

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.
@@ -1,225 +1,16 @@
1
- import { AnySchemaValidator, UnboxedObjectSchema, SchemaValidator, Schema, IdiomaticSchema } from '@forklaunch/validator';
1
+ import { Prettify } from '@forklaunch/common';
2
+ import { AnySchemaValidator, IdiomaticSchema, Schema } from '@forklaunch/validator';
2
3
  import { Constructor } from '@mikro-orm/core';
3
- import { BaseEntity } from '../persistence/index.mjs';
4
4
 
5
- /**
6
- * Interface representing a constructor for an entity mapper.
7
- *
8
- * @template T - The type of the entity mapper.
9
- * @template SV - The type of the schema validator.
10
- * @interface MapperConstructor
11
- */
12
- interface MapperConstructor<T, SV extends AnySchemaValidator> {
13
- /**
14
- * Creates a new instance of the entity mapper.
15
- *
16
- * @param {AnySchemaValidator} schemaValidator - The arguments to pass to the constructor.
17
- * @returns {T} - A new instance of the entity mapper.
18
- */
19
- new (schemaValidator: SV): T;
20
- }
21
-
22
- /**
23
- * Type representing a schema validator object for an entity mapper.
24
- *
25
- * @template SV - A type that extends AnySchemaValidator.
26
- * @typedef {SV['_ValidSchemaObject'] | UnboxedObjectSchema<SV>} MapperSchemaValidatorObject
27
- */
28
- type MapperSchemaValidatorObject<SV extends AnySchemaValidator> = SV['_ValidSchemaObject'] | UnboxedObjectSchema<SV>;
29
-
30
- /**
31
- * Abstract class representing a base entity mapper.
32
- *
33
- * @template SV - A type that extends AnySchemaValidator.
34
- */
35
- declare abstract class BaseMapper<SV extends AnySchemaValidator> {
36
- /**
37
- * The schema validator exact type.
38
- * @type {SV}
39
- * @protected
40
- */
41
- _SV: SV;
42
- /**
43
- * The schema validator as a general type.
44
- * @type {SchemaValidator}
45
- * @protected
46
- */
47
- protected schemaValidator: SchemaValidator;
48
- /**
49
- * The schema definition.
50
- * @type {MapperSchemaValidatorObject<SV>}
51
- * @abstract
52
- */
53
- abstract schema: MapperSchemaValidatorObject<SV>;
54
- /**
55
- * The Data Transfer Object (DTO).
56
- * @type {Schema<this['schema'], SV>}
57
- */
58
- _dto: Schema<this['schema'], SV>;
59
- /**
60
- * Creates an instance of BaseMapper.
61
- *
62
- * @param {SV} schemaValidator - The schema provider.
63
- */
64
- constructor(schemaValidator: SV);
65
- /**
66
- * Validates and sets the Data Transfer Object (DTO).
67
- *
68
- * @param {this['_dto']} dto - The Data Transfer Object (DTO).
69
- * @throws {Error} - Throws an error if the DTO is invalid.
70
- */
71
- set dto(_dto: this['_dto']);
72
- /**
73
- * Validates and gets the Data Transfer Object (DTO).
74
- *
75
- * @returns {this['_dto']} - The Data Transfer Object (DTO).
76
- */
77
- get dto(): this['_dto'];
78
- /**
79
- * Gets the schema of a T.
80
- *
81
- * @template T - A type that extends BaseMapper.
82
- * @template SV - A type that extends AnySchemaValidator.
83
- * @param {MapperConstructor<T, SV>} this - The constructor of the T.
84
- * @returns {T['schema']} - The schema of the T.
85
- */
86
- static schema<T extends BaseMapper<SV>, SV extends AnySchemaValidator>(this: MapperConstructor<T, SV>): T['schema'];
87
- }
88
-
89
- /**
90
- * Abstract class representing a request entity mapper.
91
- *
92
- * @template Entity - A type that extends SqlBaseEntity.
93
- * @template SV - A type that extends AnySchemaValidator.
94
- * @extends {BaseMapper<SV>}
95
- */
96
- declare abstract class RequestMapper<Entity, SV extends AnySchemaValidator> extends BaseMapper<SV> {
97
- /**
98
- * The entity type.
99
- * @type {Entity}
100
- * @protected
101
- */
102
- _Entity: Entity;
103
- /**
104
- * Converts the underlying DTO to an entity.
105
- *
106
- * @abstract
107
- * @param {...unknown[]} additionalArgs - Additional arguments.
108
- * @returns {Promise<Entity>} - The entity.
109
- */
110
- abstract toEntity(...additionalArgs: unknown[]): Promise<Entity>;
111
- /**
112
- * Populates the DTO with data from a JSON object.
113
- *
114
- * @param {this['_dto']} json - The JSON object.
115
- * @returns {Promise<this>} - The instance of the RequestMapper.
116
- */
117
- fromDto(json: this['_dto']): Promise<this>;
118
- /**
119
- * Deserializes a JSON object to an entity.
120
- *
121
- * @param {this['_dto']} json - The JSON object.
122
- * @param {...unknown[]} additionalArgs - Additional arguments.
123
- * @returns {Entity} - The entity.
124
- */
125
- deserializeDtoToEntity(json: this['_dto'], ...additionalArgs: Parameters<this['toEntity']>): Promise<Entity>;
126
- /**
127
- * Creates an instance of a RequestMapper from a JSON object.
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
- * @returns {T} - An instance of the T.
136
- */
137
- static fromDto<T extends RequestMapper<unknown, SV>, SV extends AnySchemaValidator, JsonType extends T['_dto']>(this: MapperConstructor<T, SV>, schemaValidator: SV, json: JsonType): Promise<T>;
138
- /**
139
- * Deserializes a JSON object to an entity.
140
- *
141
- * @template T - A type that extends RequestMapper.
142
- * @template SV - A type that extends AnySchemaValidator.
143
- * @template JsonType - The type of the JSON object.
144
- * @param {MapperConstructor<T, SV>} this - The constructor of the T.
145
- * @param {SV} schemaValidator - The schema provider.
146
- * @param {JsonType} json - The JSON object.
147
- * @param {...unknown[]} additionalArgs - Additional arguments.
148
- * @returns {T['_Entity']} - The entity.
149
- */
150
- 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']>;
151
- }
152
-
153
- /**
154
- * Abstract class representing a response entity mapper.
155
- *
156
- * @template Entity - A type that extends SqlBaseEntity.
157
- * @template SV - A type that extends AnySchemaValidator.
158
- * @extends {BaseMapper<SV>}
159
- */
160
- declare abstract class ResponseMapper<Entity, SV extends AnySchemaValidator> extends BaseMapper<SV> {
161
- /**
162
- * The entity type.
163
- * @type {Entity}
164
- * @protected
165
- */
166
- _Entity: Entity;
167
- /**
168
- * Populates entity mapper with DTO from an entity.
169
- *
170
- * @abstract
171
- * @param {Entity} entity - The entity to convert.
172
- * @param {...unknown[]} additionalArgs - Additional arguments.
173
- * @returns {this} - The instance of the ResponseMapper.
174
- */
175
- abstract fromEntity(entity: Entity, ...additionalArgs: unknown[]): Promise<this>;
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(): Promise<this['_dto']>;
183
- /**
184
- * Serializes an entity to a JSON object.
185
- *
186
- * @param {Entity} entity - The entity to serialize.
187
- * @returns {this['_dto']} - The JSON object.
188
- * @throws {Error} - Throws an error if the DTO is invalid.
189
- */
190
- serializeEntityToDto(...[entity, ...additionalArgs]: Parameters<this['fromEntity']>): Promise<this['_dto']>;
191
- /**
192
- * Populates entity mapper with DTO from an entity.
193
- *
194
- * @template T - A type that extends ResponseMapper.
195
- * @template SV - A type that extends AnySchemaValidator.
196
- * @param {MapperConstructor<T, SV>} this - The constructor of the T.
197
- * @param {SV} schemaValidator - The schema provider.
198
- * @param {T['_Entity']} entity - The entity to convert.
199
- * @returns {T} - An instance of the T.
200
- */
201
- static fromEntity<T extends ResponseMapper<unknown, SV>, SV extends AnySchemaValidator>(this: MapperConstructor<T, SV>, schemaValidator: SV, ...[entity, ...additionalArgs]: Parameters<T['fromEntity']>): Promise<T>;
202
- /**
203
- * Serializes an entity to a JSON object.
204
- *
205
- * @template T - A type that extends ResponseMapper.
206
- * @template SV - A type that extends AnySchemaValidator.
207
- * @param {MapperConstructor<T, SV>} this - The constructor of the T.
208
- * @param {SV} schemaValidator - The schema provider.
209
- * @param {T['_Entity']} entity - The entity to serialize.
210
- * @returns {T['_dto']} - The JSON object.
211
- * @throws {Error} - Throws an error if the DTO is invalid.
212
- */
213
- 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>;
214
- }
215
-
216
- declare function requestMapper<SV extends AnySchemaValidator, Dto extends IdiomaticSchema<SV>, Entity extends Constructor<BaseEntity>>(schemaValidator: SV, dtoSchema: Dto, toEntity: (dto: Schema<Dto, SV>) => Promise<Entity>): {
217
- toEntity: (dto: Dto) => Promise<Entity>;
218
- };
219
-
220
- declare function responseMapper<SV extends AnySchemaValidator, Dto extends IdiomaticSchema<SV>, Entity extends Constructor<BaseEntity>>(schemaValidator: SV, dtoSchema: Dto, toDto: (entity: Entity) => Promise<Schema<Dto, SV>>): {
221
- schema: Dto;
222
- toDto: (entity: Entity) => Promise<Schema<Dto, SV>>;
223
- };
224
-
225
- export { RequestMapper, ResponseMapper, requestMapper, responseMapper };
5
+ declare function requestMapper<SV extends AnySchemaValidator, DtoSchema extends IdiomaticSchema<SV>, Entity, AdditionalArgs extends unknown[] = []>(schemaValidator: SV, dtoSchema: DtoSchema, _entityConstructor: Constructor<Entity>, mapperDefinition: {
6
+ toEntity: (dto: Schema<DtoSchema, SV>, ...args: AdditionalArgs) => Promise<Entity>;
7
+ }): {
8
+ schema: DtoSchema;
9
+ } & typeof mapperDefinition;
10
+ declare function responseMapper<SV extends AnySchemaValidator, DtoSchema extends IdiomaticSchema<SV>, Entity, AdditionalArgs extends unknown[] = []>(schemaValidator: SV, dtoSchema: DtoSchema, _entityConstructor: Constructor<Entity>, mapperDefinition: {
11
+ toDto: (entity: Entity, ...args: AdditionalArgs) => Promise<Schema<DtoSchema, SV>>;
12
+ }): Prettify<{
13
+ schema: DtoSchema;
14
+ } & typeof mapperDefinition>;
15
+
16
+ export { requestMapper, responseMapper };
@@ -1,225 +1,16 @@
1
- import { AnySchemaValidator, UnboxedObjectSchema, SchemaValidator, Schema, IdiomaticSchema } from '@forklaunch/validator';
1
+ import { Prettify } from '@forklaunch/common';
2
+ import { AnySchemaValidator, IdiomaticSchema, Schema } from '@forklaunch/validator';
2
3
  import { Constructor } from '@mikro-orm/core';
3
- import { BaseEntity } from '../persistence/index.js';
4
4
 
5
- /**
6
- * Interface representing a constructor for an entity mapper.
7
- *
8
- * @template T - The type of the entity mapper.
9
- * @template SV - The type of the schema validator.
10
- * @interface MapperConstructor
11
- */
12
- interface MapperConstructor<T, SV extends AnySchemaValidator> {
13
- /**
14
- * Creates a new instance of the entity mapper.
15
- *
16
- * @param {AnySchemaValidator} schemaValidator - The arguments to pass to the constructor.
17
- * @returns {T} - A new instance of the entity mapper.
18
- */
19
- new (schemaValidator: SV): T;
20
- }
21
-
22
- /**
23
- * Type representing a schema validator object for an entity mapper.
24
- *
25
- * @template SV - A type that extends AnySchemaValidator.
26
- * @typedef {SV['_ValidSchemaObject'] | UnboxedObjectSchema<SV>} MapperSchemaValidatorObject
27
- */
28
- type MapperSchemaValidatorObject<SV extends AnySchemaValidator> = SV['_ValidSchemaObject'] | UnboxedObjectSchema<SV>;
29
-
30
- /**
31
- * Abstract class representing a base entity mapper.
32
- *
33
- * @template SV - A type that extends AnySchemaValidator.
34
- */
35
- declare abstract class BaseMapper<SV extends AnySchemaValidator> {
36
- /**
37
- * The schema validator exact type.
38
- * @type {SV}
39
- * @protected
40
- */
41
- _SV: SV;
42
- /**
43
- * The schema validator as a general type.
44
- * @type {SchemaValidator}
45
- * @protected
46
- */
47
- protected schemaValidator: SchemaValidator;
48
- /**
49
- * The schema definition.
50
- * @type {MapperSchemaValidatorObject<SV>}
51
- * @abstract
52
- */
53
- abstract schema: MapperSchemaValidatorObject<SV>;
54
- /**
55
- * The Data Transfer Object (DTO).
56
- * @type {Schema<this['schema'], SV>}
57
- */
58
- _dto: Schema<this['schema'], SV>;
59
- /**
60
- * Creates an instance of BaseMapper.
61
- *
62
- * @param {SV} schemaValidator - The schema provider.
63
- */
64
- constructor(schemaValidator: SV);
65
- /**
66
- * Validates and sets the Data Transfer Object (DTO).
67
- *
68
- * @param {this['_dto']} dto - The Data Transfer Object (DTO).
69
- * @throws {Error} - Throws an error if the DTO is invalid.
70
- */
71
- set dto(_dto: this['_dto']);
72
- /**
73
- * Validates and gets the Data Transfer Object (DTO).
74
- *
75
- * @returns {this['_dto']} - The Data Transfer Object (DTO).
76
- */
77
- get dto(): this['_dto'];
78
- /**
79
- * Gets the schema of a T.
80
- *
81
- * @template T - A type that extends BaseMapper.
82
- * @template SV - A type that extends AnySchemaValidator.
83
- * @param {MapperConstructor<T, SV>} this - The constructor of the T.
84
- * @returns {T['schema']} - The schema of the T.
85
- */
86
- static schema<T extends BaseMapper<SV>, SV extends AnySchemaValidator>(this: MapperConstructor<T, SV>): T['schema'];
87
- }
88
-
89
- /**
90
- * Abstract class representing a request entity mapper.
91
- *
92
- * @template Entity - A type that extends SqlBaseEntity.
93
- * @template SV - A type that extends AnySchemaValidator.
94
- * @extends {BaseMapper<SV>}
95
- */
96
- declare abstract class RequestMapper<Entity, SV extends AnySchemaValidator> extends BaseMapper<SV> {
97
- /**
98
- * The entity type.
99
- * @type {Entity}
100
- * @protected
101
- */
102
- _Entity: Entity;
103
- /**
104
- * Converts the underlying DTO to an entity.
105
- *
106
- * @abstract
107
- * @param {...unknown[]} additionalArgs - Additional arguments.
108
- * @returns {Promise<Entity>} - The entity.
109
- */
110
- abstract toEntity(...additionalArgs: unknown[]): Promise<Entity>;
111
- /**
112
- * Populates the DTO with data from a JSON object.
113
- *
114
- * @param {this['_dto']} json - The JSON object.
115
- * @returns {Promise<this>} - The instance of the RequestMapper.
116
- */
117
- fromDto(json: this['_dto']): Promise<this>;
118
- /**
119
- * Deserializes a JSON object to an entity.
120
- *
121
- * @param {this['_dto']} json - The JSON object.
122
- * @param {...unknown[]} additionalArgs - Additional arguments.
123
- * @returns {Entity} - The entity.
124
- */
125
- deserializeDtoToEntity(json: this['_dto'], ...additionalArgs: Parameters<this['toEntity']>): Promise<Entity>;
126
- /**
127
- * Creates an instance of a RequestMapper from a JSON object.
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
- * @returns {T} - An instance of the T.
136
- */
137
- static fromDto<T extends RequestMapper<unknown, SV>, SV extends AnySchemaValidator, JsonType extends T['_dto']>(this: MapperConstructor<T, SV>, schemaValidator: SV, json: JsonType): Promise<T>;
138
- /**
139
- * Deserializes a JSON object to an entity.
140
- *
141
- * @template T - A type that extends RequestMapper.
142
- * @template SV - A type that extends AnySchemaValidator.
143
- * @template JsonType - The type of the JSON object.
144
- * @param {MapperConstructor<T, SV>} this - The constructor of the T.
145
- * @param {SV} schemaValidator - The schema provider.
146
- * @param {JsonType} json - The JSON object.
147
- * @param {...unknown[]} additionalArgs - Additional arguments.
148
- * @returns {T['_Entity']} - The entity.
149
- */
150
- 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']>;
151
- }
152
-
153
- /**
154
- * Abstract class representing a response entity mapper.
155
- *
156
- * @template Entity - A type that extends SqlBaseEntity.
157
- * @template SV - A type that extends AnySchemaValidator.
158
- * @extends {BaseMapper<SV>}
159
- */
160
- declare abstract class ResponseMapper<Entity, SV extends AnySchemaValidator> extends BaseMapper<SV> {
161
- /**
162
- * The entity type.
163
- * @type {Entity}
164
- * @protected
165
- */
166
- _Entity: Entity;
167
- /**
168
- * Populates entity mapper with DTO from an entity.
169
- *
170
- * @abstract
171
- * @param {Entity} entity - The entity to convert.
172
- * @param {...unknown[]} additionalArgs - Additional arguments.
173
- * @returns {this} - The instance of the ResponseMapper.
174
- */
175
- abstract fromEntity(entity: Entity, ...additionalArgs: unknown[]): Promise<this>;
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(): Promise<this['_dto']>;
183
- /**
184
- * Serializes an entity to a JSON object.
185
- *
186
- * @param {Entity} entity - The entity to serialize.
187
- * @returns {this['_dto']} - The JSON object.
188
- * @throws {Error} - Throws an error if the DTO is invalid.
189
- */
190
- serializeEntityToDto(...[entity, ...additionalArgs]: Parameters<this['fromEntity']>): Promise<this['_dto']>;
191
- /**
192
- * Populates entity mapper with DTO from an entity.
193
- *
194
- * @template T - A type that extends ResponseMapper.
195
- * @template SV - A type that extends AnySchemaValidator.
196
- * @param {MapperConstructor<T, SV>} this - The constructor of the T.
197
- * @param {SV} schemaValidator - The schema provider.
198
- * @param {T['_Entity']} entity - The entity to convert.
199
- * @returns {T} - An instance of the T.
200
- */
201
- static fromEntity<T extends ResponseMapper<unknown, SV>, SV extends AnySchemaValidator>(this: MapperConstructor<T, SV>, schemaValidator: SV, ...[entity, ...additionalArgs]: Parameters<T['fromEntity']>): Promise<T>;
202
- /**
203
- * Serializes an entity to a JSON object.
204
- *
205
- * @template T - A type that extends ResponseMapper.
206
- * @template SV - A type that extends AnySchemaValidator.
207
- * @param {MapperConstructor<T, SV>} this - The constructor of the T.
208
- * @param {SV} schemaValidator - The schema provider.
209
- * @param {T['_Entity']} entity - The entity to serialize.
210
- * @returns {T['_dto']} - The JSON object.
211
- * @throws {Error} - Throws an error if the DTO is invalid.
212
- */
213
- 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>;
214
- }
215
-
216
- declare function requestMapper<SV extends AnySchemaValidator, Dto extends IdiomaticSchema<SV>, Entity extends Constructor<BaseEntity>>(schemaValidator: SV, dtoSchema: Dto, toEntity: (dto: Schema<Dto, SV>) => Promise<Entity>): {
217
- toEntity: (dto: Dto) => Promise<Entity>;
218
- };
219
-
220
- declare function responseMapper<SV extends AnySchemaValidator, Dto extends IdiomaticSchema<SV>, Entity extends Constructor<BaseEntity>>(schemaValidator: SV, dtoSchema: Dto, toDto: (entity: Entity) => Promise<Schema<Dto, SV>>): {
221
- schema: Dto;
222
- toDto: (entity: Entity) => Promise<Schema<Dto, SV>>;
223
- };
224
-
225
- export { RequestMapper, ResponseMapper, requestMapper, responseMapper };
5
+ declare function requestMapper<SV extends AnySchemaValidator, DtoSchema extends IdiomaticSchema<SV>, Entity, AdditionalArgs extends unknown[] = []>(schemaValidator: SV, dtoSchema: DtoSchema, _entityConstructor: Constructor<Entity>, mapperDefinition: {
6
+ toEntity: (dto: Schema<DtoSchema, SV>, ...args: AdditionalArgs) => Promise<Entity>;
7
+ }): {
8
+ schema: DtoSchema;
9
+ } & typeof mapperDefinition;
10
+ declare function responseMapper<SV extends AnySchemaValidator, DtoSchema extends IdiomaticSchema<SV>, Entity, AdditionalArgs extends unknown[] = []>(schemaValidator: SV, dtoSchema: DtoSchema, _entityConstructor: Constructor<Entity>, mapperDefinition: {
11
+ toDto: (entity: Entity, ...args: AdditionalArgs) => Promise<Schema<DtoSchema, SV>>;
12
+ }): Prettify<{
13
+ schema: DtoSchema;
14
+ } & typeof mapperDefinition>;
15
+
16
+ export { requestMapper, responseMapper };
@@ -20,250 +20,40 @@ 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
- RequestMapper: () => RequestMapper,
24
- ResponseMapper: () => ResponseMapper,
25
23
  requestMapper: () => requestMapper,
26
24
  responseMapper: () => responseMapper
27
25
  });
28
26
  module.exports = __toCommonJS(mappers_exports);
29
27
 
30
- // src/mappers/models/requestMapper.model.ts
31
- var import_validator2 = require("@forklaunch/validator");
32
-
33
- // src/mappers/models/baseMapper.model.ts
28
+ // src/mappers/mapper.ts
34
29
  var import_validator = require("@forklaunch/validator");
35
- function construct(self, schemaValidator) {
36
- return new self(schemaValidator || {});
37
- }
38
- var BaseMapper = class {
39
- /**
40
- * The schema validator exact type.
41
- * @type {SV}
42
- * @protected
43
- */
44
- _SV;
45
- /**
46
- * The schema validator as a general type.
47
- * @type {SchemaValidator}
48
- * @protected
49
- */
50
- schemaValidator;
51
- /**
52
- * The Data Transfer Object (DTO).
53
- * @type {Schema<this['schema'], SV>}
54
- */
55
- _dto;
56
- /**
57
- * Creates an instance of BaseMapper.
58
- *
59
- * @param {SV} schemaValidator - The schema provider.
60
- */
61
- constructor(schemaValidator) {
62
- this.schemaValidator = schemaValidator;
63
- }
64
- /**
65
- * Validates and sets the Data Transfer Object (DTO).
66
- *
67
- * @param {this['_dto']} dto - The Data Transfer Object (DTO).
68
- * @throws {Error} - Throws an error if the DTO is invalid.
69
- */
70
- set dto(_dto) {
71
- const parsedSchema = this.schemaValidator.parse(
72
- this.schemaValidator.schemify(this.schema),
73
- _dto
74
- );
75
- if (!parsedSchema.ok) {
76
- throw new Error((0, import_validator.prettyPrintParseErrors)(parsedSchema.errors, "DTO"));
77
- }
78
- this._dto = _dto;
79
- }
80
- /**
81
- * Validates and gets the Data Transfer Object (DTO).
82
- *
83
- * @returns {this['_dto']} - The Data Transfer Object (DTO).
84
- */
85
- get dto() {
86
- return this._dto;
87
- }
88
- /**
89
- * Gets the schema of a T.
90
- *
91
- * @template T - A type that extends BaseMapper.
92
- * @template SV - A type that extends AnySchemaValidator.
93
- * @param {MapperConstructor<T, SV>} this - The constructor of the T.
94
- * @returns {T['schema']} - The schema of the T.
95
- */
96
- static schema() {
97
- return construct(this).schema;
98
- }
99
- };
100
-
101
- // src/mappers/models/requestMapper.model.ts
102
- var RequestMapper = class extends BaseMapper {
103
- /**
104
- * The entity type.
105
- * @type {Entity}
106
- * @protected
107
- */
108
- _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) {
116
- const parsedSchema = this.schemaValidator.parse(
117
- this.schemaValidator.schemify(this.schema),
118
- json
119
- );
120
- if (!parsedSchema.ok) {
121
- throw new Error((0, import_validator2.prettyPrintParseErrors)(parsedSchema.errors, "DTO"));
122
- }
123
- this.dto = json;
124
- return Promise.resolve(this);
125
- }
126
- /**
127
- * Deserializes a JSON object to an entity.
128
- *
129
- * @param {this['_dto']} json - The JSON object.
130
- * @param {...unknown[]} additionalArgs - Additional arguments.
131
- * @returns {Entity} - The entity.
132
- */
133
- deserializeDtoToEntity(json, ...additionalArgs) {
134
- const result = this.fromDto(json);
135
- return result.then((r) => r.toEntity(...additionalArgs));
136
- }
137
- /**
138
- * Creates an instance of a RequestMapper from a JSON object.
139
- *
140
- * @template T - A type that extends RequestMapper.
141
- * @template SV - A type that extends AnySchemaValidator.
142
- * @template JsonType - The type of the JSON object.
143
- * @param {MapperConstructor<T, SV>} this - The constructor of the T.
144
- * @param {SV} schemaValidator - The schema provider.
145
- * @param {JsonType} json - The JSON object.
146
- * @returns {T} - An instance of the T.
147
- */
148
- static fromDto(schemaValidator, json) {
149
- return construct(this, schemaValidator).fromDto(json);
150
- }
151
- /**
152
- * Deserializes a JSON object to an entity.
153
- *
154
- * @template T - A type that extends RequestMapper.
155
- * @template SV - A type that extends AnySchemaValidator.
156
- * @template JsonType - The type of the JSON object.
157
- * @param {MapperConstructor<T, SV>} this - The constructor of the T.
158
- * @param {SV} schemaValidator - The schema provider.
159
- * @param {JsonType} json - The JSON object.
160
- * @param {...unknown[]} additionalArgs - Additional arguments.
161
- * @returns {T['_Entity']} - The entity.
162
- */
163
- static deserializeDtoToEntity(schemaValidator, json, ...additionalArgs) {
164
- const result = construct(this, schemaValidator).fromDto(json);
165
- return result.then((r) => r.toEntity(...additionalArgs));
166
- }
167
- };
168
-
169
- // src/mappers/models/responseMapper.model.ts
170
- var import_validator3 = require("@forklaunch/validator");
171
- var ResponseMapper = class extends BaseMapper {
172
- /**
173
- * The entity type.
174
- * @type {Entity}
175
- * @protected
176
- */
177
- _Entity;
178
- /**
179
- * Converts the underlying DTO to a JSON object.
180
- *
181
- * @returns {this['_dto']} - The JSON object.
182
- * @throws {Error} - Throws an error if the DTO is invalid.
183
- */
184
- toDto() {
185
- const parsedSchema = this.schemaValidator.parse(
186
- this.schemaValidator.schemify(this.schema),
187
- this.dto
188
- );
189
- if (!parsedSchema.ok) {
190
- throw new Error((0, import_validator3.prettyPrintParseErrors)(parsedSchema.errors, "DTO"));
191
- }
192
- return Promise.resolve(this.dto);
193
- }
194
- /**
195
- * Serializes an entity to a JSON object.
196
- *
197
- * @param {Entity} entity - The entity to serialize.
198
- * @returns {this['_dto']} - The JSON object.
199
- * @throws {Error} - Throws an error if the DTO is invalid.
200
- */
201
- serializeEntityToDto(...[entity, ...additionalArgs]) {
202
- const result = this.fromEntity(entity, ...additionalArgs);
203
- return result.then((r) => r.toDto());
204
- }
205
- /**
206
- * Populates entity mapper with DTO from an entity.
207
- *
208
- * @template T - A type that extends ResponseMapper.
209
- * @template SV - A type that extends AnySchemaValidator.
210
- * @param {MapperConstructor<T, SV>} this - The constructor of the T.
211
- * @param {SV} schemaValidator - The schema provider.
212
- * @param {T['_Entity']} entity - The entity to convert.
213
- * @returns {T} - An instance of the T.
214
- */
215
- static fromEntity(schemaValidator, ...[entity, ...additionalArgs]) {
216
- return construct(this, schemaValidator).fromEntity(
217
- entity,
218
- ...additionalArgs
219
- );
220
- }
221
- /**
222
- * Serializes an entity to a JSON object.
223
- *
224
- * @template T - A type that extends ResponseMapper.
225
- * @template SV - A type that extends AnySchemaValidator.
226
- * @param {MapperConstructor<T, SV>} this - The constructor of the T.
227
- * @param {SV} schemaValidator - The schema provider.
228
- * @param {T['_Entity']} entity - The entity to serialize.
229
- * @returns {T['_dto']} - The JSON object.
230
- * @throws {Error} - Throws an error if the DTO is invalid.
231
- */
232
- static serializeEntityToDto(schemaValidator, ...[entity, ...additionalArgs]) {
233
- const result = construct(this, schemaValidator).fromEntity(
234
- entity,
235
- ...additionalArgs
236
- );
237
- return result.then((r) => r.toDto());
238
- }
239
- };
240
-
241
- // src/mappers/requestMapper.ts
242
- var import_validator4 = require("@forklaunch/validator");
243
- function requestMapper(schemaValidator, dtoSchema, toEntity) {
30
+ function requestMapper(schemaValidator, dtoSchema, _entityConstructor, mapperDefinition) {
244
31
  const sv = schemaValidator;
245
32
  return {
246
- toEntity: async (dto) => {
33
+ ...mapperDefinition,
34
+ schema: dtoSchema,
35
+ toEntity: async (dto, ...args) => {
247
36
  const parsedSchema = sv.parse(sv.schemify(dtoSchema), dto);
248
37
  if (!parsedSchema.ok) {
249
- throw new Error((0, import_validator4.prettyPrintParseErrors)(parsedSchema.errors, "DTO"));
38
+ throw new Error((0, import_validator.prettyPrintParseErrors)(parsedSchema.errors, "DTO"));
250
39
  }
251
- return toEntity(dto);
40
+ return mapperDefinition.toEntity(
41
+ dto,
42
+ ...args
43
+ );
252
44
  }
253
45
  };
254
46
  }
255
-
256
- // src/mappers/responseMapper.ts
257
- var import_validator5 = require("@forklaunch/validator");
258
- function responseMapper(schemaValidator, dtoSchema, toDto) {
47
+ function responseMapper(schemaValidator, dtoSchema, _entityConstructor, mapperDefinition) {
259
48
  const sv = schemaValidator;
260
49
  return {
50
+ ...mapperDefinition,
261
51
  schema: dtoSchema,
262
- toDto: async (entity) => {
263
- const dto = await toDto(entity);
52
+ toDto: async (entity, ...args) => {
53
+ const dto = await mapperDefinition.toDto(entity, ...args);
264
54
  const parsedSchema = sv.parse(sv.schemify(dtoSchema), dto);
265
55
  if (!parsedSchema.ok) {
266
- throw new Error((0, import_validator5.prettyPrintParseErrors)(parsedSchema.errors, "DTO"));
56
+ throw new Error((0, import_validator.prettyPrintParseErrors)(parsedSchema.errors, "DTO"));
267
57
  }
268
58
  return dto;
269
59
  }
@@ -271,8 +61,6 @@ function responseMapper(schemaValidator, dtoSchema, toDto) {
271
61
  }
272
62
  // Annotate the CommonJS export names for ESM import in node:
273
63
  0 && (module.exports = {
274
- RequestMapper,
275
- ResponseMapper,
276
64
  requestMapper,
277
65
  responseMapper
278
66
  });
@@ -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","../../src/mappers/requestMapper.ts","../../src/mappers/responseMapper.ts"],"sourcesContent":["export * from './models/requestMapper.model';\nexport * from './models/responseMapper.model';\nexport * from './requestMapper';\nexport * from './responseMapper';\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","import {\n AnySchemaValidator,\n IdiomaticSchema,\n prettyPrintParseErrors,\n Schema,\n SchemaValidator\n} from '@forklaunch/validator';\nimport { Constructor } from '@mikro-orm/core';\nimport { BaseEntity } from '../persistence';\n\nexport function requestMapper<\n SV extends AnySchemaValidator,\n Dto extends IdiomaticSchema<SV>,\n Entity extends Constructor<BaseEntity>\n>(\n schemaValidator: SV,\n dtoSchema: Dto,\n toEntity: (dto: Schema<Dto, SV>) => Promise<Entity>\n) {\n const sv = schemaValidator as SchemaValidator;\n return {\n toEntity: async (dto: Dto) => {\n const parsedSchema = sv.parse(sv.schemify(dtoSchema), dto);\n if (!parsedSchema.ok) {\n throw new Error(prettyPrintParseErrors(parsedSchema.errors, 'DTO'));\n }\n return toEntity(dto as Schema<Dto, SV>);\n }\n };\n}\n","import {\n AnySchemaValidator,\n IdiomaticSchema,\n prettyPrintParseErrors,\n Schema,\n SchemaValidator\n} from '@forklaunch/validator';\nimport { Constructor } from '@mikro-orm/core';\nimport { BaseEntity } from '../persistence';\n\nexport function responseMapper<\n SV extends AnySchemaValidator,\n Dto extends IdiomaticSchema<SV>,\n Entity extends Constructor<BaseEntity>\n>(\n schemaValidator: SV,\n dtoSchema: Dto,\n toDto: (entity: Entity) => Promise<Schema<Dto, SV>>\n) {\n const sv = schemaValidator as SchemaValidator;\n return {\n schema: dtoSchema,\n toDto: async (entity: Entity) => {\n const dto = await toDto(entity);\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;AAAA;AAAA;AAAA;;;ACAA,IAAAA,oBAGO;;;ACHP,uBAKO;AAaA,SAAS,UACd,MACA,iBACG;AACH,SAAO,IAAI,KAAK,mBAAoB,CAAC,CAAQ;AAC/C;AAOO,IAAe,aAAf,MAAyD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAM9D;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOU;AAAA;AAAA;AAAA;AAAA;AAAA,EAaV;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,YAAY,iBAAqB;AAC/B,SAAK,kBAAkB;AAAA,EACzB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,IAAI,IAAI,MAAoB;AAC1B,UAAM,eAAe,KAAK,gBAAgB;AAAA,MACxC,KAAK,gBAAgB,SAAS,KAAK,MAAM;AAAA,MACzC;AAAA,IACF;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SAAS,cAKd,iBACA,WACA,UACA;AACA,QAAM,KAAK;AACX,SAAO;AAAA,IACL,UAAU,OAAO,QAAa;AAC5B,YAAM,eAAe,GAAG,MAAM,GAAG,SAAS,SAAS,GAAG,GAAG;AACzD,UAAI,CAAC,aAAa,IAAI;AACpB,cAAM,IAAI,UAAM,0CAAuB,aAAa,QAAQ,KAAK,CAAC;AAAA,MACpE;AACA,aAAO,SAAS,GAAsB;AAAA,IACxC;AAAA,EACF;AACF;;;AC7BA,IAAAC,oBAMO;AAIA,SAAS,eAKd,iBACA,WACA,OACA;AACA,QAAM,KAAK;AACX,SAAO;AAAA,IACL,QAAQ;AAAA,IACR,OAAO,OAAO,WAAmB;AAC/B,YAAM,MAAM,MAAM,MAAM,MAAM;AAC9B,YAAM,eAAe,GAAG,MAAM,GAAG,SAAS,SAAS,GAAG,GAAG;AACzD,UAAI,CAAC,aAAa,IAAI;AACpB,cAAM,IAAI,UAAM,0CAAuB,aAAa,QAAQ,KAAK,CAAC;AAAA,MACpE;AACA,aAAO;AAAA,IACT;AAAA,EACF;AACF;","names":["import_validator","import_validator","import_validator","import_validator"]}
1
+ {"version":3,"sources":["../../src/mappers/index.ts","../../src/mappers/mapper.ts"],"sourcesContent":["export * from './mapper';\n","import { Prettify } from '@forklaunch/common';\nimport {\n AnySchemaValidator,\n IdiomaticSchema,\n prettyPrintParseErrors,\n Schema,\n SchemaValidator\n} from '@forklaunch/validator';\nimport { Constructor } from '@mikro-orm/core';\n\nexport function requestMapper<\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 toEntity: (\n dto: Schema<DtoSchema, SV>,\n ...args: AdditionalArgs\n ) => Promise<Entity>;\n }\n): {\n schema: DtoSchema;\n} & typeof mapperDefinition {\n const sv = schemaValidator as SchemaValidator;\n return {\n ...mapperDefinition,\n schema: dtoSchema,\n\n toEntity: async (dto: Schema<DtoSchema, SV>, ...args: AdditionalArgs) => {\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 };\n}\n\nexport function responseMapper<\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 toDto: (\n entity: Entity,\n ...args: AdditionalArgs\n ) => Promise<Schema<DtoSchema, SV>>;\n }\n): Prettify<\n {\n schema: DtoSchema;\n } & typeof mapperDefinition\n> {\n const sv = schemaValidator as SchemaValidator;\n return {\n ...mapperDefinition,\n schema: dtoSchema,\n\n toDto: async (entity: Entity, ...args: AdditionalArgs) => {\n const dto = await mapperDefinition.toDto(entity, ...args);\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;AAAA;;;ACCA,uBAMO;AAGA,SAAS,cAMd,iBACA,WACA,oBACA,kBAQ0B;AAC1B,QAAM,KAAK;AACX,SAAO;AAAA,IACL,GAAG;AAAA,IACH,QAAQ;AAAA,IAER,UAAU,OAAO,QAA+B,SAAyB;AACvE,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,EACF;AACF;AAEO,SAAS,eAMd,iBACA,WACA,oBACA,kBAUA;AACA,QAAM,KAAK;AACX,SAAO;AAAA,IACL,GAAG;AAAA,IACH,QAAQ;AAAA,IAER,OAAO,OAAO,WAAmB,SAAyB;AACxD,YAAM,MAAM,MAAM,iBAAiB,MAAM,QAAQ,GAAG,IAAI;AACxD,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":[]}
@@ -1,258 +1,40 @@
1
- // src/mappers/models/requestMapper.model.ts
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 construct(self, schemaValidator) {
11
- return new self(schemaValidator || {});
12
- }
13
- var BaseMapper = class {
14
- /**
15
- * The schema validator exact type.
16
- * @type {SV}
17
- * @protected
18
- */
19
- _SV;
20
- /**
21
- * The schema validator as a general type.
22
- * @type {SchemaValidator}
23
- * @protected
24
- */
25
- schemaValidator;
26
- /**
27
- * The Data Transfer Object (DTO).
28
- * @type {Schema<this['schema'], SV>}
29
- */
30
- _dto;
31
- /**
32
- * Creates an instance of BaseMapper.
33
- *
34
- * @param {SV} schemaValidator - The schema provider.
35
- */
36
- constructor(schemaValidator) {
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"));
52
- }
53
- this._dto = _dto;
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
- };
217
-
218
- // src/mappers/requestMapper.ts
219
- import {
220
- prettyPrintParseErrors as prettyPrintParseErrors4
221
- } from "@forklaunch/validator";
222
- function requestMapper(schemaValidator, dtoSchema, toEntity) {
5
+ function requestMapper(schemaValidator, dtoSchema, _entityConstructor, mapperDefinition) {
223
6
  const sv = schemaValidator;
224
7
  return {
225
- toEntity: async (dto) => {
8
+ ...mapperDefinition,
9
+ schema: dtoSchema,
10
+ toEntity: async (dto, ...args) => {
226
11
  const parsedSchema = sv.parse(sv.schemify(dtoSchema), dto);
227
12
  if (!parsedSchema.ok) {
228
- throw new Error(prettyPrintParseErrors4(parsedSchema.errors, "DTO"));
13
+ throw new Error(prettyPrintParseErrors(parsedSchema.errors, "DTO"));
229
14
  }
230
- return toEntity(dto);
15
+ return mapperDefinition.toEntity(
16
+ dto,
17
+ ...args
18
+ );
231
19
  }
232
20
  };
233
21
  }
234
-
235
- // src/mappers/responseMapper.ts
236
- import {
237
- prettyPrintParseErrors as prettyPrintParseErrors5
238
- } from "@forklaunch/validator";
239
- function responseMapper(schemaValidator, dtoSchema, toDto) {
22
+ function responseMapper(schemaValidator, dtoSchema, _entityConstructor, mapperDefinition) {
240
23
  const sv = schemaValidator;
241
24
  return {
25
+ ...mapperDefinition,
242
26
  schema: dtoSchema,
243
- toDto: async (entity) => {
244
- const dto = await toDto(entity);
27
+ toDto: async (entity, ...args) => {
28
+ const dto = await mapperDefinition.toDto(entity, ...args);
245
29
  const parsedSchema = sv.parse(sv.schemify(dtoSchema), dto);
246
30
  if (!parsedSchema.ok) {
247
- throw new Error(prettyPrintParseErrors5(parsedSchema.errors, "DTO"));
31
+ throw new Error(prettyPrintParseErrors(parsedSchema.errors, "DTO"));
248
32
  }
249
33
  return dto;
250
34
  }
251
35
  };
252
36
  }
253
37
  export {
254
- RequestMapper,
255
- ResponseMapper,
256
38
  requestMapper,
257
39
  responseMapper
258
40
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/mappers/models/requestMapper.model.ts","../../src/mappers/models/baseMapper.model.ts","../../src/mappers/models/responseMapper.model.ts","../../src/mappers/requestMapper.ts","../../src/mappers/responseMapper.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","import {\n AnySchemaValidator,\n IdiomaticSchema,\n prettyPrintParseErrors,\n Schema,\n SchemaValidator\n} from '@forklaunch/validator';\nimport { Constructor } from '@mikro-orm/core';\nimport { BaseEntity } from '../persistence';\n\nexport function requestMapper<\n SV extends AnySchemaValidator,\n Dto extends IdiomaticSchema<SV>,\n Entity extends Constructor<BaseEntity>\n>(\n schemaValidator: SV,\n dtoSchema: Dto,\n toEntity: (dto: Schema<Dto, SV>) => Promise<Entity>\n) {\n const sv = schemaValidator as SchemaValidator;\n return {\n toEntity: async (dto: Dto) => {\n const parsedSchema = sv.parse(sv.schemify(dtoSchema), dto);\n if (!parsedSchema.ok) {\n throw new Error(prettyPrintParseErrors(parsedSchema.errors, 'DTO'));\n }\n return toEntity(dto as Schema<Dto, SV>);\n }\n };\n}\n","import {\n AnySchemaValidator,\n IdiomaticSchema,\n prettyPrintParseErrors,\n Schema,\n SchemaValidator\n} from '@forklaunch/validator';\nimport { Constructor } from '@mikro-orm/core';\nimport { BaseEntity } from '../persistence';\n\nexport function responseMapper<\n SV extends AnySchemaValidator,\n Dto extends IdiomaticSchema<SV>,\n Entity extends Constructor<BaseEntity>\n>(\n schemaValidator: SV,\n dtoSchema: Dto,\n toDto: (entity: Entity) => Promise<Schema<Dto, SV>>\n) {\n const sv = schemaValidator as SchemaValidator;\n return {\n schema: dtoSchema,\n toDto: async (entity: Entity) => {\n const dto = await toDto(entity);\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,EAEE,0BAAAA;AAAA,OACK;;;ACHP;AAAA,EAEE;AAAA,OAGK;AAaA,SAAS,UACd,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intParseErrors","prettyPrintParseErrors","prettyPrintParseErrors","prettyPrintParseErrors"]}
1
+ {"version":3,"sources":["../../src/mappers/mapper.ts"],"sourcesContent":["import { Prettify } from '@forklaunch/common';\nimport {\n AnySchemaValidator,\n IdiomaticSchema,\n prettyPrintParseErrors,\n Schema,\n SchemaValidator\n} from '@forklaunch/validator';\nimport { Constructor } from '@mikro-orm/core';\n\nexport function requestMapper<\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 toEntity: (\n dto: Schema<DtoSchema, SV>,\n ...args: AdditionalArgs\n ) => Promise<Entity>;\n }\n): {\n schema: DtoSchema;\n} & typeof mapperDefinition {\n const sv = schemaValidator as SchemaValidator;\n return {\n ...mapperDefinition,\n schema: dtoSchema,\n\n toEntity: async (dto: Schema<DtoSchema, SV>, ...args: AdditionalArgs) => {\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 };\n}\n\nexport function responseMapper<\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 toDto: (\n entity: Entity,\n ...args: AdditionalArgs\n ) => Promise<Schema<DtoSchema, SV>>;\n }\n): Prettify<\n {\n schema: DtoSchema;\n } & typeof mapperDefinition\n> {\n const sv = schemaValidator as SchemaValidator;\n return {\n ...mapperDefinition,\n schema: dtoSchema,\n\n toDto: async (entity: Entity, ...args: AdditionalArgs) => {\n const dto = await mapperDefinition.toDto(entity, ...args);\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":";AACA;AAAA,EAGE;AAAA,OAGK;AAGA,SAAS,cAMd,iBACA,WACA,oBACA,kBAQ0B;AAC1B,QAAM,KAAK;AACX,SAAO;AAAA,IACL,GAAG;AAAA,IACH,QAAQ;AAAA,IAER,UAAU,OAAO,QAA+B,SAAyB;AACvE,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,EACF;AACF;AAEO,SAAS,eAMd,iBACA,WACA,oBACA,kBAUA;AACA,QAAM,KAAK;AACX,SAAO;AAAA,IACL,GAAG;AAAA,IACH,QAAQ;AAAA,IAER,OAAO,OAAO,WAAmB,SAAyB;AACxD,YAAM,MAAM,MAAM,iBAAiB,MAAM,QAAQ,GAAG,IAAI;AACxD,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.13.9",
3
+ "version": "0.14.1",
4
4
  "description": "forklaunch-js core package. Contains useful building blocks.",
5
5
  "homepage": "https://github.com/forklaunch/forklaunch-js#readme",
6
6
  "bugs": {
@@ -91,8 +91,8 @@
91
91
  "pino-pretty": "^13.1.1",
92
92
  "redis": "^5.8.2",
93
93
  "uuid": "^11.1.0",
94
- "@forklaunch/common": "0.5.8",
95
- "@forklaunch/validator": "0.9.9"
94
+ "@forklaunch/common": "0.6.1",
95
+ "@forklaunch/validator": "0.10.1"
96
96
  },
97
97
  "devDependencies": {
98
98
  "@eslint/js": "^9.34.0",