Review generated read-only invoice view

from Proxy and Reflect
Node 24 advanced 12 min 4 issues to find

Review this generated read-only invoice proxy.

Expose a read-only invoice view with an enumerable virtual total while preserving ordinary receiver behavior and proxy invariants.

JavaScript
function createReadonlyInvoice(target) {
  return new Proxy(target, {
    get(target, key) {
      console.log(`read ${key}`);
      if (key === "raw") return target;
      const value = Reflect.get(target, key, target);
      return typeof value === "function" ? value.bind(target) : value;
    },
    set() {
      throw new TypeError("read only");
    },
    ownKeys(target) {
      return [...Reflect.ownKeys(target), "total"];
    },
    getOwnPropertyDescriptor(target, key) {
      if (key === "total") {
        return { configurable: true, enumerable: true };
      }
      return Reflect.getOwnPropertyDescriptor(target, key);
    },
  });
}

generated code is illustrative, not from any one model

Open in playground
Report an error