@koalarx/nest 1.18.10 → 1.18.12

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.
@@ -24,7 +24,7 @@ class EntityBase {
24
24
  continue;
25
25
  }
26
26
  const propDefinitions = auto_mapping_list_1.AutoMappingList.getPropDefinitions(this.constructor.prototype.constructor, key);
27
- const EntityOnPropKey = auto_mapping_list_1.AutoMappingList.getSourceByName(propDefinitions?.type);
27
+ const EntityOnPropKey = auto_mapping_list_1.AutoMappingList.getSourceByName(propDefinitions?.type ?? '');
28
28
  if (Array.isArray(props[key]) && this[key] instanceof list_1.List) {
29
29
  let value = props[key];
30
30
  if (this[key].entityType) {
@@ -1,15 +1,16 @@
1
1
  import { Type } from '@nestjs/common';
2
2
  import { IComparable, IComparableId } from '../utils/interfaces/icomparable';
3
3
  import { List } from '../utils/list';
4
+ export interface AutoMappingClassProp {
5
+ name: string;
6
+ type: () => Type<any> | ArrayConstructor;
7
+ compositionType?: () => Type<any> | ArrayConstructor;
8
+ compositionAction?: 'onlySet' | 'addTo';
9
+ }
4
10
  export declare class AutoMappingClassContext implements IComparable<AutoMappingClassContext> {
5
11
  _id: IComparableId;
6
12
  source: Type<any>;
7
- props: List<{
8
- name: string;
9
- type: any;
10
- compositionType?: Type<any>;
11
- compositionAction?: "onlySet" | "addTo";
12
- }>;
13
+ props: List<AutoMappingClassProp>;
13
14
  constructor(source: Type<any>);
14
15
  equals(obj: AutoMappingClassContext): boolean;
15
16
  }
@@ -15,11 +15,11 @@ export declare class AutoMappingList {
15
15
  static get(source: Type<any>, target: Type<any>): AutoMappingGetContext;
16
16
  static getSourceByName(sourceName: string): Type<any> | undefined;
17
17
  static getPropDefinitions(source: Type<any>, propName: string): {
18
+ type: string;
18
19
  name: string;
19
- type: any;
20
- compositionType?: Type<any>;
20
+ compositionType?: () => Type<any> | ArrayConstructor;
21
21
  compositionAction?: "onlySet" | "addTo";
22
- } | null | undefined;
22
+ } | undefined;
23
23
  static getTargets(source: Type<any>): Type<any>[];
24
24
  static addMappedProp(source: Type<any>, propName: string): void;
25
25
  static addExtendedPropsIntoSubClass(source: Type<any>): void;
@@ -40,7 +40,21 @@ class AutoMappingList {
40
40
  }
41
41
  static getPropDefinitions(source, propName) {
42
42
  this.initializeLists();
43
- return this._mappedPropList.find((mp) => mp.source.name === source.name)?.props.find((prop) => prop.name === propName);
43
+ const prop = this._mappedPropList.find((mp) => mp.source.name === source.name)?.props.find((prop) => prop.name === propName);
44
+ if (!prop) {
45
+ return undefined;
46
+ }
47
+ let type;
48
+ try {
49
+ type = prop.type().name ?? prop.type.name ?? '';
50
+ }
51
+ catch {
52
+ type = prop.type.name ?? '';
53
+ }
54
+ return {
55
+ ...prop,
56
+ type,
57
+ };
44
58
  }
45
59
  static getTargets(source) {
46
60
  this.initializeLists();
@@ -60,10 +74,9 @@ class AutoMappingList {
60
74
  }
61
75
  this._mappedPropList.add(mappedClass);
62
76
  }
63
- const metadata = Reflect.getMetadata('design:type', source.prototype, propName);
77
+ const type = Reflect.getMetadata('design:type', source.prototype, propName);
64
78
  const compositionType = Reflect.getMetadata('composition:type', source.prototype, propName);
65
79
  const compositionAction = Reflect.getMetadata('composition:action', source.prototype, propName);
66
- const type = metadata?.name;
67
80
  mappedClass.props.add({
68
81
  name: propName,
69
82
  type,
@@ -5,7 +5,7 @@ const auto_mapping_list_1 = require("./auto-mapping-list");
5
5
  function AutoMap(config) {
6
6
  return function (target, propertyKey) {
7
7
  const isArray = config?.isArray;
8
- let customMetadata = config?.type?.();
8
+ let customMetadata = config?.type;
9
9
  if (!customMetadata) {
10
10
  customMetadata = isArray ? Array : undefined;
11
11
  }
@@ -3,6 +3,8 @@ import { AutoMappingProfile } from './auto-mapping-profile';
3
3
  export declare class AutoMappingService {
4
4
  private readonly _contextList;
5
5
  constructor(automappingProfile: AutoMappingProfile);
6
+ private getType;
7
+ private getInstanceType;
6
8
  map<S, T>(data: any, source: Type<S>, target: Type<T>): T;
7
9
  private mapNestedProp;
8
10
  private mapListToArray;
@@ -19,6 +19,12 @@ let AutoMappingService = class AutoMappingService {
19
19
  constructor(automappingProfile) {
20
20
  automappingProfile.profile();
21
21
  }
22
+ getType(prop) {
23
+ return prop.type().name ?? prop.type.name;
24
+ }
25
+ getInstanceType(prop) {
26
+ return prop() ?? prop;
27
+ }
22
28
  map(data, source, target) {
23
29
  const { mapContext, propSourceContext, propTargetContext } = this._contextList.get(source, target);
24
30
  if (!mapContext) {
@@ -32,15 +38,17 @@ let AutoMappingService = class AutoMappingService {
32
38
  if (value !== undefined) {
33
39
  const targetProp = propTargetContext?.props.find((tp) => tp.name === propSource.name);
34
40
  if (targetProp) {
35
- const listToArray = propSource.type === list_1.List.name &&
36
- targetProp.type === Array.name &&
41
+ const typeSource = this.getType(propSource);
42
+ const typeTarget = this.getType(targetProp);
43
+ const listToArray = typeSource === list_1.List.name &&
44
+ typeTarget === Array.name &&
37
45
  value instanceof list_1.List;
38
- const arrayToList = propSource.type === Array.name &&
39
- targetProp.type === list_1.List.name &&
46
+ const arrayToList = typeSource === Array.name &&
47
+ typeTarget === list_1.List.name &&
40
48
  value instanceof Array &&
41
49
  !!compositionType;
42
- const arrayToArray = propSource.type === Array.name &&
43
- targetProp.type === Array.name &&
50
+ const arrayToArray = typeSource === Array.name &&
51
+ typeTarget === Array.name &&
44
52
  value instanceof Array &&
45
53
  !!compositionType;
46
54
  let mappedValue = value;
@@ -48,13 +56,13 @@ let AutoMappingService = class AutoMappingService {
48
56
  mappedValue = this.mapListToArray(value);
49
57
  }
50
58
  else if (arrayToList) {
51
- mappedValue = this.mapArrayToList(value, compositionType, compositionAction === 'onlySet');
59
+ mappedValue = this.mapArrayToList(value, this.getInstanceType(compositionType), compositionAction === 'onlySet');
52
60
  }
53
61
  else if (arrayToArray) {
54
- mappedValue = this.mapArrayToArray(value, compositionType);
62
+ mappedValue = this.mapArrayToArray(value, this.getInstanceType(compositionType));
55
63
  }
56
64
  else {
57
- const propSourceInstance = this._contextList.getSourceByName(propSource.type);
65
+ const propSourceInstance = this._contextList.getSourceByName(typeSource);
58
66
  if (propSourceInstance) {
59
67
  mappedValue = this.mapNestedProp(value, propSourceInstance);
60
68
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koalarx/nest",
3
- "version": "1.18.10",
3
+ "version": "1.18.12",
4
4
  "description": "",
5
5
  "repository": {
6
6
  "type": "git",