ts-force
ts-force copied to clipboard
custom formatter?
Since we now wrap the RestObject in proxy a custom formatter would be helpful:
https://www.mattzeunert.com/2016/02/19/custom-chrome-devtools-object-formatters.html
This seems to work ok...
const isRestObject = (obj: any) => {
let proto = obj.__proto__;
while (proto) {
if (proto === RestObject.prototype) {
return true;
}
proto = proto.__proto__;
}
return false;
};
// init();
// const proxy_set = new WeakSet();
// window.Proxy = new Proxy(Proxy, {
// construct(target, args) {
// const proxy = new target(args[0], args[1]);
// proxy_set.add(proxy);
// return proxy;
// },
// });
window.devtoolsFormatters = [{
header(obj: any) {
try {
if (!isRestObject(obj)) {
// if (!proxy_set.has(obj)) {
return null; // ['object', {object: 'no proxy'}];
}
const loggedObj: any = JSON.parse(JSON.stringify(obj));
// loggedObj.constructor = {};
// loggedObj.constructor.name = obj.constructor.name;
// obj[i] = rawObj[i];
// return obj;
// loggedObj['TESTING'] = obj.constructor.name;
return ['object', {object: loggedObj}]; //
} catch (e) {
return null;
}
},
hasBody() {
return false;
},
}];
One downside is the objects lose their SObject type (so it just say (object instead of Account)... Seems like there should be some way to fix that
Node also has a symbol to customize output. https://nodejs.org/api/util.html#util_util_inspect_custom
I believe there are one or more libraries that do this in a way that's compatible with browsers too.