-
Notifications
You must be signed in to change notification settings - Fork 483
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'Diluka-intersection-type-helper'
- Loading branch information
Showing
14 changed files
with
270 additions
and
300 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './intersection-type.helper'; | ||
export * from './omit-type.helper'; | ||
export * from './partial-type.helper'; | ||
export * from './pick-type.helper'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Type } from '@nestjs/common'; | ||
import { | ||
inheritTransformationMetadata, | ||
inheritValidationMetadata | ||
} from '@nestjs/mapped-types'; | ||
import { DECORATORS } from '../constants'; | ||
import { ApiProperty } from '../decorators'; | ||
import { ModelPropertiesAccessor } from '../services/model-properties-accessor'; | ||
import { clonePluginMetadataFactory } from './mapped-types.utils'; | ||
|
||
const modelPropertiesAccessor = new ModelPropertiesAccessor(); | ||
|
||
export function IntersectionType<A, B>( | ||
classARef: Type<A>, | ||
classBRef: Type<B> | ||
): Type<A & B> { | ||
const fieldsOfA = modelPropertiesAccessor.getModelProperties( | ||
classARef.prototype | ||
); | ||
const fieldsOfB = modelPropertiesAccessor.getModelProperties( | ||
classBRef.prototype | ||
); | ||
|
||
abstract class IntersectionTypeClass {} | ||
inheritValidationMetadata(classARef, IntersectionTypeClass); | ||
inheritTransformationMetadata(classARef, IntersectionTypeClass); | ||
inheritValidationMetadata(classBRef, IntersectionTypeClass); | ||
inheritTransformationMetadata(classBRef, IntersectionTypeClass); | ||
|
||
clonePluginMetadataFactory( | ||
IntersectionTypeClass as Type<unknown>, | ||
classARef.prototype | ||
); | ||
clonePluginMetadataFactory( | ||
IntersectionTypeClass as Type<unknown>, | ||
classBRef.prototype | ||
); | ||
|
||
fieldsOfA.forEach((propertyKey) => { | ||
const metadata = Reflect.getMetadata( | ||
DECORATORS.API_MODEL_PROPERTIES, | ||
classARef.prototype, | ||
propertyKey | ||
); | ||
const decoratorFactory = ApiProperty(metadata); | ||
decoratorFactory(IntersectionTypeClass.prototype, propertyKey); | ||
}); | ||
|
||
fieldsOfB.forEach((propertyKey) => { | ||
const metadata = Reflect.getMetadata( | ||
DECORATORS.API_MODEL_PROPERTIES, | ||
classBRef.prototype, | ||
propertyKey | ||
); | ||
const decoratorFactory = ApiProperty(metadata); | ||
decoratorFactory(IntersectionTypeClass.prototype, propertyKey); | ||
}); | ||
|
||
Object.defineProperty(IntersectionTypeClass, 'name', { | ||
value: `Intersection${classARef.name}${classBRef.name}` | ||
}); | ||
return IntersectionTypeClass as Type<A & B>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Type } from '@nestjs/common'; | ||
import { identity } from 'lodash'; | ||
import { METADATA_FACTORY_NAME } from '../plugin/plugin-constants'; | ||
|
||
export function clonePluginMetadataFactory( | ||
target: Type<unknown>, | ||
parent: Type<unknown>, | ||
transformFn: (metadata: Record<string, any>) => Record<string, any> = identity | ||
) { | ||
let targetMetadata = {}; | ||
|
||
do { | ||
if (!parent.constructor) { | ||
return; | ||
} | ||
if (!parent.constructor[METADATA_FACTORY_NAME]) { | ||
continue; | ||
} | ||
const parentMetadata = parent.constructor[METADATA_FACTORY_NAME](); | ||
targetMetadata = { | ||
...parentMetadata, | ||
...targetMetadata | ||
}; | ||
} while ( | ||
(parent = Reflect.getPrototypeOf(parent) as Type<any>) && | ||
parent !== Object.prototype && | ||
parent | ||
); | ||
targetMetadata = transformFn(targetMetadata); | ||
|
||
if (target[METADATA_FACTORY_NAME]) { | ||
const originalFactory = target[METADATA_FACTORY_NAME]; | ||
target[METADATA_FACTORY_NAME] = () => { | ||
const originalMetadata = originalFactory(); | ||
return { | ||
...originalMetadata, | ||
...targetMetadata | ||
}; | ||
}; | ||
} else { | ||
target[METADATA_FACTORY_NAME] = () => targetMetadata; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.