Comparing version 0.0.3 to 0.0.4
@@ -12,3 +12,3 @@ // Init ES6 environments for .require() | ||
// Mount yql iso-call middleware to the express | ||
// Mount iso-call middleware to the express | ||
isocall.setupMiddleware(app); | ||
@@ -15,0 +15,0 @@ |
@@ -12,3 +12,3 @@ // Init ES6 environments for .require() | ||
// Mount yql iso-call middleware to the express | ||
// Mount iso-call middleware to the express | ||
isocall.setupMiddleware(app); | ||
@@ -15,0 +15,0 @@ |
Release History | ||
=============== | ||
0.0.4 | ||
https://github.com/zordius/iso-call/releases/tag/v0.0.3 | ||
**2015-05-25 add alias of request-core** | ||
* Add new shortcut `iso-call/request` alias to `iso-call/lib/iso-request-core` | ||
0.0.3 | ||
@@ -5,0 +12,0 @@ ----- |
@@ -5,3 +5,4 @@ var isoconfig = require('./iso-config'); | ||
module.exports = { | ||
execute: function (name, cfg) { | ||
execute: function () { | ||
var name = arguments[0]; | ||
if (!name) { | ||
@@ -12,3 +13,3 @@ return Promise.reject(new Error('iso-execute-client without name!')); | ||
method: 'PUT', | ||
body: JSON.stringify(cfg), | ||
body: Array.prototype.slice.call(arguments).slice(1), | ||
url: isoconfig.getBaseURL() + name, | ||
@@ -15,0 +16,0 @@ headers: { |
@@ -1,2 +0,4 @@ | ||
var execute = function (name, cfg) { | ||
var execute = function () { | ||
var name = arguments[0]; | ||
var exec = require('./iso-config').getConfigs()[name]; | ||
@@ -17,3 +19,3 @@ | ||
try { | ||
return Promise.resolve(exec.apply(this, [cfg])); | ||
return Promise.resolve(exec.apply(this, Array.prototype.slice.call(arguments).slice(1))); | ||
} catch (E) { | ||
@@ -25,3 +27,3 @@ return Promise.reject(E); | ||
var middleware = function (req, res) { | ||
execute.apply(req, [req.params.name, req.body]).then(function (R) { | ||
execute.apply(req, [req.params.name].concat(req.body)).then(function (R) { | ||
res.send({rpc: R}); | ||
@@ -28,0 +30,0 @@ })['catch'](function (E) { |
{ | ||
"name": "iso-call", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Isomorphic api call for any nodejs/express application", | ||
@@ -5,0 +5,0 @@ "author": "Zordius <zordius@yahoo-inc.com>", |
@@ -85,3 +85,3 @@ iso-call | ||
You should setup all your API or RPC list only for server, the best place is your server.js. | ||
You should setup all your API or RPC list only for server, the best place is do it inside your server.js. | ||
@@ -98,5 +98,11 @@ ```javascript | ||
// RPC as {name: function} list | ||
connectdb: function (params) { | ||
getSqlData: function (params) { | ||
return mysqlPromise(params.host, params.port); | ||
} | ||
// Also support RPC function with multiple parameters | ||
getSQL: function (host, port, sql) { | ||
return mysqlPromise(host, port, sql); | ||
} | ||
}); | ||
@@ -118,3 +124,3 @@ ``` | ||
Now you can do isomprphic RPC!! | ||
Now you can call RPC isomorphically!! | ||
@@ -128,2 +134,10 @@ ```javascript | ||
}); | ||
// Support with multiple parameters rpc function | ||
isocall.execute('rpcName', rpcParam1, rpcParam2, ...).then(function (R) { | ||
// Success, R = result | ||
}).catch(function (E) { | ||
// Failed , E = error | ||
}); | ||
``` | ||
@@ -185,4 +199,15 @@ | ||
* Access <a href="http://expressjs.com/4x/api.html#req">express request<a/> by `this` inside the RPC. | ||
* Access <a href="http://expressjs.com/4x/api.html#req">express request</a> by `this` inside the RPC. | ||
* Do request based logic inside a RPC. | ||
* Get required cookie or headers from the request then pass to an API. | ||
Use Case: prevent CSRF | ||
---------------------- | ||
Checkout our <a href="examples/05-csrf">CSRF example</a> to know more about how to prevent <a href="http://en.wikipedia.org/wiki/Cross-site_request_forgery">Cross-Site Request Forgery</a>. | ||
NOTICE | ||
------ | ||
* We use `JSON.stringify()` to transfer `isocall.execute()` result from server side to client side, so you can not receive data other than <a href="http://www.tutorialspoint.com/json/json_data_types.htm">standard JSON data types</a>. (TODO: support customized JSON serializer) | ||
* The `result.response.body` object from `isocall.request()` will be removed from `result.response` to reduce transmission size; in most case it is same with `result.body`. |
var assert = require('chai').assert; | ||
var isocfg = require('../lib/iso-config'); | ||
var sinon = require('sinon'); | ||
var isoexe = require('../lib/iso-execute-server'); | ||
@@ -45,6 +47,16 @@ describe('iso-config', function () { | ||
it('.setBaseURL() should return baseURL', function () { | ||
it('.setBaseURL() should set baseURL correctly', function () { | ||
isocfg.setBaseURL('/_lalala_/'); | ||
assert.deepEqual(isocfg.getBaseURL(), '/_lalala_/'); | ||
}); | ||
it('.setBaseURL() should warn when after .setupMiddleware', function () { | ||
sinon.spy(console, 'warn'); | ||
sinon.stub(isoexe, 'middlewareMounted').returns(true); | ||
isocfg.setBaseURL(); | ||
assert(console.warn.getCall(0).args[0], '.setBaseURL() after .setupMiddleware() , this may cause client side call to wrong endpoint.'); | ||
console.warn.restore(); | ||
isoexe.middlewareMounted.restore(); | ||
}); | ||
}); |
@@ -37,2 +37,17 @@ var assert = require('chai').assert; | ||
}); | ||
it('will put arguments into request body', function (done) { | ||
var body = null; | ||
nock(baseHOST).persist() | ||
.filteringRequestBody(function (B) { | ||
body = B; | ||
return true; | ||
}) | ||
.put('/test') | ||
.reply(200, {rpc: {msg: 'OK!'}}); | ||
isoexe.execute('test', 123, 456, 'abc').then(function () { | ||
assert.deepEqual(body, '[123,456,"abc"]'); | ||
}).then(done.bind(), done); | ||
}); | ||
}); |
@@ -59,2 +59,13 @@ var assert = require('chai').assert; | ||
}); | ||
it('will pass arguments into RPC', function (done) { | ||
isocall.addConfigs({ | ||
testRpc: function (a, b, c) { | ||
return [c, b, a].join(','); | ||
} | ||
}); | ||
isoexe.execute('testRpc', 1, 2, 3).then(function (R) { | ||
assert.equal(R, '3,2,1'); | ||
}).then(done.bind(), done); | ||
}); | ||
it('will return rejected Promise when execute error', function (done) { | ||
@@ -85,2 +96,14 @@ isocall.addConfigs({ | ||
}); | ||
it('will throw when already mounted', function () { | ||
var app = { | ||
put: sinon.spy(), | ||
enable: sinon.spy(), | ||
enabled: sinon.stub().returns(true) | ||
}; | ||
assert.throws(function () { | ||
isoexe.setupMiddleware(app); | ||
}, 'Can not .setupMiddleware() to an express instance again!'); | ||
}); | ||
}); | ||
@@ -92,3 +115,3 @@ | ||
params: {name: 'test'}, | ||
body: 'OK!' | ||
body: ['OK!', 'GOOD!'] | ||
}; | ||
@@ -100,4 +123,5 @@ var res = { | ||
isocall.addConfigs({ | ||
test: function (body) { | ||
assert.equal(body, 'OK!'); | ||
test: function (a, b) { | ||
assert.equal(a, 'OK!'); | ||
assert.equal(b, 'GOOD!'); | ||
done(); | ||
@@ -104,0 +128,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
61650
63
1175
209