Created
May 11, 2025 05:46
-
-
Save kennarddh/61d31c6965f68fb60d211277b42a3086 to your computer and use it in GitHub Desktop.
Intercept function calls with proxy.
This file contains hidden or 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
| const CreateInterceptor = <T extends object>(target: T) => { | |
| const handler: ProxyHandler<T> = { | |
| get: (target, prop) => { | |
| const originalValue = (target as any)[prop] | |
| if (typeof originalValue === 'function') { | |
| return function (...args: any[]) { | |
| console.log( | |
| `Function "${prop as any}" called with arguments:`, | |
| args, | |
| ) | |
| const result = originalValue.apply(target, args) | |
| console.log(`Function "${prop as any}" returned:`, result) | |
| return result | |
| } | |
| } | |
| return originalValue | |
| }, | |
| } | |
| return new Proxy(target, handler) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment