Files
reacord/packages/helpers/with-logged-method-calls.ts
2021-12-16 21:12:38 -06:00

21 lines
568 B
TypeScript

import { inspect } from "node:util"
export function withLoggedMethodCalls<T extends object>(value: T) {
return new Proxy(value as Record<string | symbol, unknown>, {
get(target, property) {
const value = target[property]
if (typeof value !== "function") {
return value
}
return (...values: any[]) => {
console.log(
`${String(property)}(${values
.map((value) => inspect(value, { depth: 1 }))
.join(", ")})`,
)
return value.apply(target, values)
}
},
}) as T
}