Skip to content

Commit

Permalink
refactor(classes): clean up typings
Browse files Browse the repository at this point in the history
  • Loading branch information
Chau Tran authored and Chau Tran committed Aug 16, 2021
1 parent ddb11e5 commit 45edfda
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
17 changes: 3 additions & 14 deletions packages/classes/src/lib/classes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createInitialMapping } from '@automapper/core';
import type { Dictionary, MapPluginInitializer } from '@automapper/types';
import type { MapPluginInitializer } from '@automapper/types';
import { MappingClassId } from '@automapper/types';
import 'reflect-metadata';
import {
Expand Down Expand Up @@ -29,10 +29,7 @@ export const classes: MapPluginInitializer<Constructible> = (errorHandler) => {
const instanceStorage = new ClassInstanceStorage();

return {
instantiate<TModel extends Dictionary<TModel> = any>(
model: Constructible<TModel>,
obj?: TModel
) {
instantiate(model, obj?) {
return instantiate(instanceStorage, metadataStorage, model, obj);
},
initializeMapping(source, destination, options?) {
Expand Down Expand Up @@ -121,15 +118,7 @@ export const classes: MapPluginInitializer<Constructible> = (errorHandler) => {
// return the mapping
return mapping;
},
preMap<
TSource extends Dictionary<TSource> = any,
TDestination extends Dictionary<TDestination> = any
>(
source: Constructible<TSource>,
destination: Constructible<TDestination>,
sourceObj?: TSource,
destinationObj?: TDestination
) {
preMap(source, destination, sourceObj?, destinationObj?) {
// Prepare the sourceInstance/destinationInstance with plain object sourceObj and destinationObj
const [sourceInstance] = this.instantiate(source, sourceObj);
const [destinationInstance] = this.instantiate(
Expand Down
16 changes: 11 additions & 5 deletions packages/classes/src/lib/utils/explore-metadata.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
} from '../constants';
import type { ClassInstanceStorage, ClassMetadataStorage } from '../storages';
import type { Constructible } from '../types';
import type { PrimitiveWithDate } from '@automapper/types';

export function exploreMetadata(
metadataStorage: ClassMetadataStorage,
Expand Down Expand Up @@ -38,15 +39,20 @@ export function exploreMetadata(
}
}

export function getMetadataList(
model: Constructible
): [string, { typeFn: any; depth?: number; isGetterOnly?: boolean }][] {
export function getMetadataList(model: Constructible): [
string,
{
typeFn: () => Constructible | PrimitiveWithDate;
depth?: number;
isGetterOnly?: boolean;
}
][] {
let metadataList =
Reflect.getMetadata(AUTOMAP_PROPERTIES_METADATA_KEY, model) || [];

if ((model as any)[AUTOMAPPER_METADATA_FACTORY_KEY]) {
if (model[AUTOMAPPER_METADATA_FACTORY_KEY]) {
metadataList = metadataList.concat(
(model as any)[AUTOMAPPER_METADATA_FACTORY_KEY]() || []
model[AUTOMAPPER_METADATA_FACTORY_KEY]() || []
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export function isDerivedSourcePathOnSourceClasses(
sourceProto: Record<string, unknown>
) {
return (sourceObj: any, sourcePath: string[]) => {
return (sourceObj: unknown, sourcePath: string[]) => {
return (
sourcePath.length === 1 &&
!(
Expand Down

0 comments on commit 45edfda

Please sign in to comment.