import { ModuleRef } from '@nestjs/core' ; import { Instantiable } from '@fc/common' ; import { FeatureHandlerEmptyException , FeatureHandlerUnregisteredException , } from '../exceptions' ; import { FeatureHandlerNoHandler } from '../handlers' ; import { IFeatureHandler } from '../interfaces' ; import { isNotValidFeatureHandlerKey } from '../utils' ; const internalMapping = new Map < string , Instantiable < IFeatureHandler > > ( ) ; param key { string } export function FeatureHandler < K extends string > ( key ) { return ( target ) => { internalMapping . set ( key , target ) ; return target ; } ; } param { string } key param { object } ctx returns { IFeatureHandler } FeatureHandler . get = function < T extends IFeatureHandler > ( key , ctx ) : T { if ( isNotValidFeatureHandlerKey ( key ) ) { throw new FeatureHandlerEmptyException ( ) ; } if ( key === null ) { return new FeatureHandlerNoHandler ( ) as T ; } const klass = internalMapping . get ( key ) ; if ( ! klass ) { throw new FeatureHandlerUnregisteredException ( ) ; } return ctx . moduleRef . get ( klass ) ; } ; returns { Array < string > } FeatureHandler . getAll = function ( ) : Array < string > { return Array . from ( internalMapping . keys ( ) ) ; } ; FeatureHandler . getInternalMappingForTestingPurposes = function ( ) : typeof internalMapping { return internalMapping ; } ;