Below is how we defined it

Syntax

We use Proxy Class from ES6, the arguments, the target is the wrap object and handler will be the function used to do some action on target using traps.

Below is a simple example of its usage

Basic example for proxy

We made 2 objects called target and handler, target is a simple object with a message key and handler is an object that has a get key with a function associated with it. We pass 2 objects in proxy class and in return receive a proxy object by which we can access the message property of the target object.

Here is a small example of how to use it for validation of object values

Validation of object key’s

We use an empty object as a target object and a handler that takes set a trap on the target object and do the validation. Pretty easy right!

Let's look into how we can use it to make API call wrapper.

We have used axios, and then created an instance with our base URL, created a handler for a proxy that returns an object of get, post, put, delete, patch function to be used, and at last made an API object which is a proxy object to an empty object target.

We can then use it as :

Calling API proxy object

This can be extended doing validation, value correction, tracing property accesses, warning about unknown properties, negative Array indices, data binding, accessing a restful web service (method calls), revocable references, monitoring async functions, type checking and much more, Read here more