import { Type } from '@nestjs/common' ; import { isFunction } from '@nestjs/common/utils/shared.utils' ; import { flatMap , identity } from 'lodash' ; import { DECORATORS } from '../constants' ; import { isBodyParameter } from '../utils/is-body-parameter.util' ; import { ModelPropertiesAccessor } from './model-properties-accessor' ; import { ParamsWithType , ParamWithTypeMetadata } from './parameter-metadata-accessor' ; export class ParametersMetadataMapper { constructor ( private readonly modelPropertiesAccessor ) { } transformModelToProperties ( parameters ) { const properties = flatMap ( parameters , ( param ) => { if ( ! param || param . type === Object ) { return undefined ; } if ( param . name ) { return param ; } if ( isBodyParameter ( param ) ) { const isCtor = param . type && isFunction ( param . type ) ; const name = isCtor ? param . type . name : param . type ; return { ... param , name } ; } const { prototype } = param . type ; this . modelPropertiesAccessor . applyMetadataFactory ( prototype ) ; const modelProperties = this . modelPropertiesAccessor . getModelProperties ( prototype ) ; return modelProperties . map ( ( key ) => this . mergeImplicitWithExplicit ( key , prototype , param ) ) ; } ) ; return properties . filter ( identity ) ; } mergeImplicitWithExplicit ( key , prototype , param ) { const reflectedParam = Reflect . getMetadata ( DECORATORS . API_MODEL_PROPERTIES , prototype , key ) || { } ; return { ... param , ... reflectedParam , name : reflectedParam . name || key } ; } }