class-proxy

The Class Proxy is used to define custom behavior for fundamental operations (e.g. property lookup, assignment, enumeration, function invocation, etc).

Usage no npm install needed!

<script type="module">
  import classProxy from 'https://cdn.skypack.dev/class-proxy';
</script>

README

class-proxy

The Class Proxy is used to define custom behavior for fundamental operations (e.g. property lookup, assignment, enumeration, function invocation, etc).

npm install class-proxy

api

demo

import createClassProxy from 'class-proxy';
import { createClassProxy } from 'class-proxy';

var createClassProxy = require("class-proxy/node");
import { implementation as URLImpl } from 'whatwg-url/lib/URL-impl';

let URLImplProxy2 = createClassProxy(URLImpl, {
    get(target, name)
    {
        return target[name];
    },
    set(target, prop, value, receiver)
    {
        if (prop == '_query')
        {
            value._url = target;

            console.log(value);
        }

        target[prop] = value;
        return true;
    },
    ownKeys(target): string[]
    {
        return [
            'href',
            'origin',
            'protocol',
            'username',
            'password',
            'host',
            'hostname',
            'port',
            'pathname',
            'search',
            'hash',
            'searchParams',
        ];
    },
    getOwnPropertyDescriptor(target, prop)
    {
        return {
            value: target[prop],
            enumerable: this.ownKeys(target).includes(prop),
            configurable: true,
        };
    },
    construct(target, args)
    {
        if (typeof args[0] == 'string')
        {
            return new target([args[0]]);
        }

        return new target(...args);
    }
});