ChuckJonas / ts-force

A Salesforce REST Client written in Typescript for Typescript
88 stars 21 forks source link

custom formatter? #51

Open ChuckJonas opened 5 years ago

ChuckJonas commented 5 years ago

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

ChuckJonas commented 5 years ago

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

CarsonF commented 4 years ago

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.