// https://www.logicbig.com/tutorials/misc/typescript/parameter-decorator.html export function Inject(target: any, propertyKey: string, descriptor: PropertyDescriptor) { console.log("method decorator validate function invoked "); let originalMethod = descriptor.value; // # Wrapping the original method with wrapper function descriptor.value = async function (...args: any[]) { const injectedValues = await InjectDecoratorMetadata.inject(target, propertyKey, args) for(let [index, argValue] of args.entries()) { const iv : any | undefined = injectedValues.find( (iv: any) => iv[0] == index); if(iv) args[index] = iv[1] } let result = originalMethod.apply(this, args); return result; } } export class InjectDecoratorMetadata { private static map: Map> = new Map(); static register(target: any, methodName: string, paramIndex: number, handler: Function): void { let paramMap: Map | undefined= this.map.get(target); if (!paramMap) { paramMap = new Map(); this.map.set(target, paramMap); } let paramIndexes: any[] | undefined = paramMap.get(methodName); if (!paramIndexes) { paramIndexes = []; paramMap.set(methodName, paramIndexes); } paramIndexes.push({ index: paramIndex, handler: handler }); } public static async inject(target: any, methodName: string, args: any[]): Promise { let paramMap: Map | undefined = this.map.get(target); if (!paramMap) { return []; } let paramIndexes: any[] | undefined = paramMap.get(methodName); if (!paramIndexes) { return []; } let paramValues : any [] = [] for (const [index, paramValue] of paramIndexes.entries()) { const pi = paramIndexes[index]; const value : any = await pi.handler(); paramValues.push([index, value]); } return paramValues; } }