Proxying an object in Javascript: The results from the proxied function are not being applied

25 views Asked by At

What I am trying to do is intercept a function call, and prepend a header. The header is getting applied, the function itself is getting called, but the results don't have actually body.

code:


'use strict'
const load = require('require.all')
module.exports = ({xr}) => {
  const cmds = load({
    dir: __dirname,
    not: /index\.js$/i
  })(xr);
  const result = Object.entries(cmds).map(([k,v]) => {
    return {
      [k] : new Proxy(v, {
        get: function (target, prop) {
          return (...args) => {
            return {
              type: 'cmd',
              ...(target[prop])(args)
            }
          }
        },
      })
    }
  }).reduce((a,b) => Object.assign({},a,b))
  return result
}

The original function will return something like:

{
  blocks:[...]
}

and I need to prepend this header.

expected output:

{
  type: "cmd",
  blocks: [...]
}

I would give you what I am getting as JSON except, it chokes on JSON.stringify because of this proxy. However, in my debugger (and my code agrees because it blows up) the only thing there is type: "cmd"

Am I doing this right?

0

There are 0 answers