0.2.1 • Published 9 years ago

isoproxy v0.2.1

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

IsoProxy - General Isomorphic API Proxy

IsoProxy is a simple proxy inspired Fetchr, and the motivation is the same as in Fetchr.

Build Status npm version Dependency Status devDependency Status license

Features

  • Isolation from a web application framework by adopting JSONRPC.
  • ES6 friendly with Promise.

Install

npm install isoproxy --save

Usage

Define isomorphic API:

// proxy.js
var IsoProxy = require("isoproxy");

var proxy = new IsoProxy({
  root: "/api",
  isServer: (typeof window === "undefined")
});

proxy.setInterfaces({
  math: ["add", "sub"]
});

proxy.setImplementations({
  math: {
    add: function(x, y) { return x + y; },
    sub: function(x, y) { return x - y; }
  }
});

module.exports = proxy;

Server side (express):

// server.js
var express = require("express");
var bodyParser = require("body-parser");
var proxy = require("./proxy");

var app = express();
app.use(bodyParser.json());

Object.keys(proxy.routes).forEach(function(urlPath) {
  var processJsonrpcRequest = proxy.routes[urlPath];
  app.post(urlPath, function(req, res) {
    processJsonrpcRequest(req.body).then(function(jsonrpcResponse) {
      res.send(jsonrpcResponse);
    });
  });
});

app.get("/add/:x/:y", function(req, res) {
  proxy.api.math.add(+req.params.x, +req.params.y)
    .then(function(result) {
      res.send(""+result);
    });
});

Client side (browserify):

// client.js
var proxy = require("./proxy");

proxy.api.math.add(1, 2).then(function(r) {
  console.log(r); // => 3
});

proxy.api.math.sub(1, 2).then(function(r) {
  console.log(r); // => -1
});

Why interfaces and implementations are separated?

It is because the client side need not know implementation details. In other words, implementations can be hidden from users.

Please see this example for more information.

Examples

Links

Supported Browsers

Above Internet Explorer 9 and other modern browsers.

License

The MIT License.

0.2.1

9 years ago

0.2.0

9 years ago

0.1.3

10 years ago

0.1.2

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago