import debugModule from 'debug' ; import { DecoratorFactory } from './decorator-factory' ; import { NamespacedReflect , Reflector } from './reflect' ; import { DecoratorType , DesignTimeMethodMetadata , MetadataKey , MetadataMap , } from './types' ; const debug = debugModule ( 'loopback:metadata:inspector' ) ; /**
 * TypeScript reflector without a namespace. The TypeScript compiler can be
 * configured to add design time metadata.
 *
 * See https://www.typescriptlang.org/docs/handbook/decorators.html
 */ const TSReflector = new NamespacedReflect ( ) ; /**
 * Options for inspection
 */ export interface InspectionOptions { /**
     * Only inspect own metadata of a given target. The prototype chain will not
     * be checked. The implementation uses `Reflect.getOwnMetadata()` if the flag
     * is set to `true`. Otherwise, it uses `Reflect.getMetadata()`.
     *
     * The flag is `false` by default for `MetadataInspector`.
     */ ownMetadataOnly ? : boolean ; } /**
 * Inspector for metadata applied by decorators
 */ export class MetadataInspector { /**
     * Expose Reflector, which is a wrapper of `Reflect` and it uses `loopback`
     * as the namespace prefix for all metadata keys
     */ static readonly Reflector = Reflector ; /**
     * Expose the reflector for TypeScript design-time metadata
     */ static readonly DesignTimeReflector = TSReflector ; param key param target param options static getClassMetadata < T > ( key , target , options ? ) { return options ?. ownMetadataOnly ? Reflector . getOwnMetadata ( key . toString ( ) , target ) : Reflector . getMetadata ( key . toString ( ) , target ) ; } param key param value param target param member static defineMetadata < T > ( key , value , target , member ? ) { Reflector . defineMetadata ( key . toString ( ) , value , target , member ) ; } param key param target param options static getAllMethodMetadata < T > ( key , target , options ? ) { return options ?. ownMetadataOnly ? Reflector . getOwnMetadata ( key . toString ( ) , target ) : Reflector . getMetadata ( key . toString ( ) , target ) ; } param key param target param methodName param options static getMethodMetadata < T > ( key , target , methodName ? , options ? ) { methodName = methodName ?? '' ; const meta = options ?. ownMetadataOnly ? Reflector . getOwnMetadata ( key . toString ( ) , target ) : Reflector . getMetadata ( key . toString ( ) , target ) ; return meta ?. [ methodName ] ; } param key param target param options static getAllPropertyMetadata < T > ( key , target , options ? ) { return options ?. ownMetadataOnly ? Reflector . getOwnMetadata ( key . toString ( ) , target ) : Reflector . getMetadata ( key . toString ( ) , target ) ; } param key param target param propertyName param options static getPropertyMetadata < T > ( key , target , propertyName , options ? ) { const meta = options ?. ownMetadataOnly ? Reflector . getOwnMetadata ( key . toString ( ) , target ) : Reflector . getMetadata ( key . toString ( ) , target ) ; return meta ?. [ propertyName ] ; } param key param target param methodName param options static getAllParameterMetadata < T > ( key , target , methodName ? , options ? ) { methodName = methodName ?? '' ; const meta = options ?. ownMetadataOnly ? Reflector . getOwnMetadata ( key . toString ( ) , target ) : Reflector . getMetadata ( key . toString ( ) , target ) ; return meta ?. [ methodName ] ; } param key param target param methodName param index param options static getParameterMetadata < T > ( key , target , methodName , index , options ? ) { methodName = methodName || '' ; const meta = options ?. ownMetadataOnly ? Reflector . getOwnMetadata ( key . toString ( ) , target ) : Reflector . getMetadata ( key . toString ( ) , target ) ; const params = meta ?. [ methodName ] ; return params ?. [ index ] ; } param target param propertyName returns static getDesignTypeForProperty ( target , propertyName ) { return TSReflector . getMetadata ( 'design:type' , target , propertyName ) ; } param target param methodName returns static getDesignTypeForMethod ( target , methodName ) { const type = TSReflector . getMetadata ( 'design:type' , target , methodName ) ; const parameterTypes = TSReflector . getMetadata ( 'design:paramtypes' , target , methodName ) ; const returnType = TSReflector . getMetadata ( 'design:returntype' , target , methodName ) ; if ( type === undefined && parameterTypes === undefined && returnType === undefined ) { if ( debug . enabled ) { const targetName = DecoratorFactory . getTargetName ( target , methodName ) ; debug ( 'No design-time type metadata found while inspecting %s. ' + 'Did you forget to enable TypeScript compiler option `emitDecoratorMetadata`?' , targetName ) ; } return undefined ; } return { type , parameterTypes , returnType , } ; } }