0.1.0 • Published 5 years ago

byproxy-serve v0.1.0

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

ByProxy

ByProxy allows the client to access an object or module on the server as if it were a local object. The object's API can then be invoked directly on the client.

For example, a calculator server:

On the server:

class Calculator {
  constructor() {
    this.opCount = 0;
  }
  add(a, b) {
    this.opCount++;
    return a + b;
  }
  subtract(a, b) {
    this.opCount++;
    return a - b;
  }
}
// Serve an instance of Calculator via Express
byproxy.serve(app, '/calculator', new Calculator()); 

On the client:

const calculator = byproxy.link('/calculator');

const sum = await calculator.add(2, 6);       // 8
const diff = await calculator.subtract(6, 2); // 4
console.log(await calculator.opCount);        // 2

Advantages

  • Tiny in size. The client lib is only 506 bytes gzipped.
  • No need to configure different rest methods for each function call. It is handled automatically.
  • When you want to add a new feature, just add a new method. Since the client is interacting with a proxy of the object on the server, no other setup is needed.
  • If using types, both the server and client will have the same interfaces - avoids bugs and duplication of definitions.

Other features

Proxy a whole module

const mymod = require('my-awesome-mod');
byproxy.serve(app, '/updater', mymod);

Proxy just a function

Server:

byproxy.serve(app, '/hello', function (name) {
  return 'Hello ' + name + '!';
});

Client:

const hello = byproxy.link('/hello');
console.log(await hello('John'));     // Hello John!

Seamlessly deal with async functions

Server:

byproxy.serve(app, '/updater', {
  async delayedUpdate() {
    return new Promise(........);
  }
}); 

Client:

const result = await updater.delayedUpdate();

Error handling

Since everything is a promise, the promise is rejected when the server returns and error code. The body of the response is in the error message and error.code will give you the status code of the rest call.

For most cases, when using async-await, simply catch the error

const calculator = byproxy.link('/calculator');
try {
  const x = await calculator.divide(10, 0); // div by 0 error
} catch (err) {
  console.error(err);
}

Setup

ByProxy is composed of two modules byproxy-serve for the server and byproxy-link for the client. They are both available on NPM.

npm install --save byproxy-serve
npm install --save byproxy-link

Note: ByProxy is not a rest library. It integrates with Express on the server. There is no dependency required on the client.

Examples

ByProxy demo project is a simple example.

License

MIT License (c) Preet Shihn

0.1.0

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago