forward-to

Forward method calls from one object to another

Usage no npm install needed!

<script type="module">
  import forwardTo from 'https://cdn.skypack.dev/forward-to';
</script>

README

forward-to

Utility function for method call forwarding from one object to another

Build Status

Installation

via npm

$ npm install forward-to

or via git

$ git clone https://github.com/seerdomin/forward-to

Usage

var forward = require('forward-to');

var proxy = Object.create(null);

var api = {
    'prefix': {
        'foo': 'foo',
        'hello': 'hello'
    },

    'foo': function foo(str) {
        console.log(this.prefix.foo + ' ' + str);
    },

    'hello': function hello(str) {
        console.log(this.prefix.hello + ' ' + str);
    }
};

forward(proxy, api, ['foo', 'hello']);

console.log(typeof proxy.prefix);
// => "undefined"

proxy.foo('bar');
// => "foo bar"

proxy.hello('world');
// => "hello world"

Goals

Provide a safe way to access an object's api without exposing its internal state.

License

This library is distributed under the MIT License.