Skip to content
master
Switch branches/tags
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
lib
 
 
src
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

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.

About

General Isomorphic API Proxy.

Resources

License

Packages

No packages published