Skip to content

Instantly share code, notes, and snippets.

@kennarddh
Created May 11, 2025 05:46
Show Gist options
  • Select an option

  • Save kennarddh/61d31c6965f68fb60d211277b42a3086 to your computer and use it in GitHub Desktop.

Select an option

Save kennarddh/61d31c6965f68fb60d211277b42a3086 to your computer and use it in GitHub Desktop.
Intercept function calls with proxy.
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