Created
March 21, 2019 15:25
-
-
Save Sangrene/f3e89ced8a5b790eadcc75bd60271d35 to your computer and use it in GitHub Desktop.
Decorator to log async method
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
export const logAction = (name: string) => { | |
return (target, key, descriptor) => { | |
if(descriptor === undefined){ | |
descriptor = Object.getOwnPropertyDescriptor(target, key); | |
} | |
const originalMethod: Function = descriptor.value; | |
descriptor.value = async function() { | |
const args: Array<any> = []; | |
for(let i = 0, iMax = arguments.length; i < iMax; i++){ | |
args[i] = arguments[i]; | |
} | |
const result = await originalMethod.apply(this, args); | |
console.log(`Call ${name}(${JSON.stringify(args)}) returns : ${JSON.stringify(result)}`); | |
return result; | |
} | |
return descriptor; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment