Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core,nestjs,docs): Finalize the async support effort started by #617 #622

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
301 changes: 235 additions & 66 deletions packages/core/src/lib/core.ts

Large diffs are not rendered by default.

95 changes: 56 additions & 39 deletions packages/core/src/lib/mappings/map-member.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
import type {
import {
ConditionReturn,
ConvertUsingReturn,
Dictionary,
FromValueReturn,
MapDeferReturn,
MapFnClassId,
MapFromReturn,
Mapper,
MapWithArgumentsReturn,
MapWithReturn,
MemberMapReturn,
MetadataIdentifier,
Primitive,
SelectorReturn,
TransformationType
} from '../types';
import { MapFnClassId, TransformationType } from '../types';
import { isDateConstructor } from '../utils/is-date-constructor';
import { isPrimitiveConstructor } from '../utils/is-primitive-constructor';
import { asyncAware } from '../utils/async-aware';

export function mapMember<
TSource extends Dictionary<TSource>,
TDestination extends Dictionary<TDestination>
TDestination extends Dictionary<TDestination>,
IsAsync extends boolean = false,
>(
transformationMapFn: MemberMapReturn<TSource, TDestination>,
sourceObject: TSource,
Expand All @@ -27,7 +31,8 @@ export function mapMember<
extraArgs: Record<string, any> | undefined,
mapper: Mapper,
sourceMemberIdentifier?: MetadataIdentifier | Primitive | Date,
destinationMemberIdentifier?: MetadataIdentifier | Primitive | Date
destinationMemberIdentifier?: MetadataIdentifier | Primitive | Date,
isAsync?: IsAsync,
) {
let value: unknown;
const transformationType: TransformationType =
Expand Down Expand Up @@ -57,7 +62,8 @@ export function mapMember<
)(
sourceObject,
mapper,
extraArgs ? { extraArgs: () => extraArgs } : undefined
extraArgs ? { extraArgs: () => extraArgs } : undefined,
isAsync
);
break;
case TransformationType.ConvertUsing:
Expand All @@ -66,28 +72,33 @@ export function mapMember<
TSource,
TDestination
>[MapFnClassId.fn]
)(sourceObject);
)(sourceObject, isAsync as IsAsync);
break;
case TransformationType.Condition:
case TransformationType.NullSubstitution:
case TransformationType.UndefinedSubstitution:
value = (
mapFn as ConditionReturn<TSource, TDestination>[MapFnClassId.fn]
)(sourceObject, destinationMemberPath);

if (shouldRunImplicitMap && value != null) {
value = Array.isArray(value)
? mapper.mapArray(
value,
sourceMemberIdentifier as MetadataIdentifier,
destinationMemberIdentifier as MetadataIdentifier
)
: mapper.map(
value,
sourceMemberIdentifier as MetadataIdentifier,
destinationMemberIdentifier as MetadataIdentifier
);
}
value = asyncAware(
() => (
mapFn as ConditionReturn<TSource, TDestination>[MapFnClassId.fn]
)(sourceObject, destinationMemberPath, isAsync as boolean),
(value) => {
if (shouldRunImplicitMap && value != null) {
return Array.isArray(value)
? mapper.mapArray(
value,
sourceMemberIdentifier as MetadataIdentifier,
destinationMemberIdentifier as MetadataIdentifier
)
: mapper.map(
value,
sourceMemberIdentifier as MetadataIdentifier,
destinationMemberIdentifier as MetadataIdentifier
);
}
return value;
},
isAsync
);

break;
case TransformationType.MapWithArguments:
Expand All @@ -98,23 +109,29 @@ export function mapMember<
>[MapFnClassId.fn]
)(sourceObject, extraArgs || {});
break;
case TransformationType.MapDefer:
value = mapMember(
(
mapFn as MapDeferReturn<
TSource,
TDestination
>[MapFnClassId.fn]
)(sourceObject) as MemberMapReturn<TSource, TDestination>,
sourceObject,
destinationObject,
destinationMemberPath,
extraArgs,
mapper,
sourceMemberIdentifier,
destinationMemberIdentifier
case TransformationType.MapDefer: {
value = asyncAware(() => (
mapFn as MapDeferReturn<
TSource,
TDestination,
SelectorReturn<TDestination>
>[MapFnClassId.fn]
)(sourceObject), (deferFunction) => {
return mapMember(
deferFunction,
sourceObject,
destinationObject,
destinationMemberPath,
extraArgs,
mapper,
sourceMemberIdentifier,
destinationMemberIdentifier,
isAsync
);
break;
}, isAsync);

break;
}
}
return value;
}
Loading