├── .gitignore ├── LICENSE ├── README.md ├── dist └── servify.min.js ├── package.json ├── servify-browser.js ├── servify-node.js └── test └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | servify.bundle.js 3 | 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2017 VICTOR HERNANDES SILVA MAIA 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Servify 2 | 3 | Microservices the simplest way conceivable. 4 | 5 | ## Usage 6 | 7 | ### Create a microservice: 8 | 9 | ```javascript 10 | const servify = require("servify"); 11 | 12 | // The service state 13 | let count = 0; 14 | 15 | // Starts a microservice with 3 API methods 16 | servify.api(3000, { 17 | 18 | // Squares a number 19 | square: (x) => x * x, 20 | 21 | // Concats two arrays 22 | concat: (a, b) => a.concat(b), 23 | 24 | // Increments and returns the counter 25 | count: () => count++ 26 | 27 | }).then(() => console.log("servified port 3000")) 28 | ``` 29 | 30 | ### Call a microservice from code: 31 | 32 | ```javascript 33 | const servify = require("servify"); 34 | 35 | // Builds the API interface from an URL 36 | const api = servify.at("http://localhost:3000"); 37 | 38 | // Calls API methods like normal lib functions 39 | api.square(2) 40 | .then(x => console.log(x)); 41 | 42 | api.concat([1,2], [3,4]) 43 | .then(arr => console.log(arr)); 44 | 45 | api.count() 46 | .then(i => console.log(i)); 47 | ``` 48 | 49 | ### Call a microservice from the browser / request: 50 | 51 | ```javascript 52 | Just access the url followed by a function call: 53 | 54 | http://localhost:3000/square(2) 55 | http://localhost:3000/concat([1,2], [3,4]) 56 | http://localhost:3000/count() 57 | ``` 58 | 59 | ## Support 60 | 61 | This requires ES6 Proxy support, so you need node.js 6 and up. Proxies cannot be polyfilled in earlier versions. 62 | 63 | ## Why 64 | 65 | When all you want is to create a microservice, [Express.js](http://expressjs.com) becomes annoyingly verbose. You have to worry about things like serializing/deserializing JSON, chosing how to format query/param inputs, picking a XHR lib on the client and so on. Servify is a ridiculously thin (50 LOC) lib that just standardizes that boring stuff. To create a microservice, all you need is an object of functions specifying your API. To interact with a service, all you need is its URL. You can then call its functions exactly like you would call a normal lib (except it returns a Promise, obviously). 66 | -------------------------------------------------------------------------------- /dist/servify.min.js: -------------------------------------------------------------------------------- 1 | var $jscomp=$jscomp||{};$jscomp.scope={};$jscomp.ASSUME_ES5=!1;$jscomp.ASSUME_NO_NATIVE_MAP=!1;$jscomp.ASSUME_NO_NATIVE_SET=!1;$jscomp.defineProperty=$jscomp.ASSUME_ES5||"function"==typeof Object.defineProperties?Object.defineProperty:function(a,d,g){a!=Array.prototype&&a!=Object.prototype&&(a[d]=g.value)};$jscomp.getGlobal=function(a){return"undefined"!=typeof window&&window===a?a:"undefined"!=typeof global&&null!=global?global:a};$jscomp.global=$jscomp.getGlobal(this);$jscomp.SYMBOL_PREFIX="jscomp_symbol_"; 2 | $jscomp.initSymbol=function(){$jscomp.initSymbol=function(){};$jscomp.global.Symbol||($jscomp.global.Symbol=$jscomp.Symbol)};$jscomp.symbolCounter_=0;$jscomp.Symbol=function(a){return $jscomp.SYMBOL_PREFIX+(a||"")+$jscomp.symbolCounter_++}; 3 | $jscomp.initSymbolIterator=function(){$jscomp.initSymbol();var a=$jscomp.global.Symbol.iterator;a||(a=$jscomp.global.Symbol.iterator=$jscomp.global.Symbol("iterator"));"function"!=typeof Array.prototype[a]&&$jscomp.defineProperty(Array.prototype,a,{configurable:!0,writable:!0,value:function(){return $jscomp.arrayIterator(this)}});$jscomp.initSymbolIterator=function(){}};$jscomp.arrayIterator=function(a){var d=0;return $jscomp.iteratorPrototype(function(){return d=6" 7 | }, 8 | "main": "servify-node.js", 9 | "browser": "servify-browser.js", 10 | "scripts": { 11 | "test": "echo \"Error: no test specified\" && exit 1", 12 | "build": "browserify servify.js > dist/servify.bundle.js; ccjs dist/servify.bundle.js > dist/servify.min.js" 13 | }, 14 | "author": "MaiaVictor", 15 | "license": "MIT", 16 | "dependencies": { 17 | "body-parser": "^1.16.0", 18 | "cors": "^2.8.1", 19 | "express": "^4.14.0", 20 | "xhr-request-promise": "^0.1.2" 21 | }, 22 | "repository": { 23 | "type": "git", 24 | "url": "https://github.com/maiavictor/servify" 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /servify-browser.js: -------------------------------------------------------------------------------- 1 | const request = require("xhr-request-promise"); 2 | 3 | module.exports = { 4 | at: url => new Proxy({}, { 5 | get: function (target, func) { 6 | return function() { 7 | const args = JSON.stringify([].slice.call(arguments)).slice(1,-1); 8 | return request(url + "/" + func + "(" + args + ")", {method: "POST"}) 9 | .then(body => JSON.parse(body)); 10 | } 11 | } 12 | }) 13 | }; 14 | -------------------------------------------------------------------------------- /servify-node.js: -------------------------------------------------------------------------------- 1 | const express = require("express"); 2 | const cors = require("cors"); 3 | const bodyParser = require("body-parser"); 4 | const servify = require("./servify-browser"); 5 | 6 | const api = (port, api) => new Promise((resolve, reject) => { 7 | var app = express(); 8 | app.use(bodyParser.json()); 9 | app.use(cors()); 10 | app.use((error, req, res, next) => { 11 | if (error.message === "invalid json") 12 | res.send("null"); 13 | else 14 | next(); 15 | }); 16 | function callFunc(req, res){ 17 | try { 18 | var url = req.url; 19 | var parens = url.indexOf("("); 20 | if (parens !== -1){ 21 | var name = url.slice(1, parens); 22 | var args = JSON.parse("["+decodeURIComponent(url.slice(parens+1, -1))+"]"); 23 | } else { 24 | var name = url.slice(1); 25 | var args = [req.body]; 26 | } 27 | var result = api[name].apply(null, args); 28 | if (result.then) 29 | result.then(result => res.json(result)); 30 | else 31 | res.json(result); 32 | } catch(e) { 33 | res.send("null"); 34 | }; 35 | }; 36 | app.get("*", callFunc); 37 | app.post("*", callFunc); 38 | app.listen(port, resolve).on("error", reject); 39 | }); 40 | 41 | module.exports = { 42 | at: servify.at, 43 | api: api 44 | }; 45 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | var servify = require("./.."); 2 | var assert = require("assert"); 3 | 4 | describe("Servify", function(){ 5 | it("must be able to create a service and call its functions", async () => { 6 | 7 | // Creates service 8 | var count = 0; 9 | 10 | await servify.api(7171, { 11 | square: (x) => x * x, 12 | concat: (a, b) => a.concat(b), 13 | count: () => ++count 14 | }); 15 | 16 | // Uses service 17 | var lib = servify.at("http://localhost:7171"); 18 | 19 | assert(await lib.square(3) === 9); 20 | assert(await lib.count() === 1); 21 | assert(JSON.stringify(await lib.concat([1,2],[3,4])) === "[1,2,3,4]"); 22 | 23 | }); 24 | }); 25 | --------------------------------------------------------------------------------