Comparing version 0.0.8 to 0.0.9
'use strict'; | ||
var createMethodName = require('./create-method-name'); | ||
var createPublicMethodName = require('./create-method-name'); | ||
@@ -31,19 +31,44 @@ function Client(transporter, options, cb) { | ||
Client.prototype.call = function (message, params, cb) { | ||
if (!message) { | ||
throw new TypeError('Bograch.on requires a message'); | ||
/** | ||
* @callback requestCallback | ||
* @param {Error} error - The error that happened during executing | ||
* the remote method. | ||
* @param {..*} resonseData - The data returned by the remote method. | ||
*/ | ||
/** | ||
* Call a remote method. | ||
* @param {string} methodName - The name of the method to be called. | ||
* @param {...*} arguments - The arguments to be passed to the method. | ||
* @param {requestCallback} [cb] - The callback that handles the response. | ||
*/ | ||
Client.prototype.call = function () { | ||
var methodName = arguments[0]; | ||
var args = []; | ||
var i = 0; | ||
var cb; | ||
if (!methodName) { | ||
throw new TypeError('Bograch.on requires a methodName'); | ||
} | ||
if (typeof message !== 'string') { | ||
throw new TypeError('Bograch.on requires a message of string type'); | ||
if (typeof methodName !== 'string') { | ||
throw new TypeError('Bograch.on requires a methodName of string type'); | ||
} | ||
if (typeof params === 'function') { | ||
cb = params; | ||
params = null; | ||
} else if (cb !== null && cb !== undefined && typeof cb !== 'function') { | ||
throw new TypeError('Bograch.call has an invalid callback function'); | ||
while (typeof arguments[++i] !== 'function' && i < arguments.length) { | ||
args.push(arguments[i]); | ||
} | ||
if (typeof arguments[i] === 'function') { | ||
cb = arguments[i]; | ||
} | ||
if (methodName == 'methodList') { | ||
console.log('---||---'); | ||
console.log(arguments); | ||
console.log(args); | ||
console.log(cb); | ||
} | ||
var methodName = createMethodName(this._name, message); | ||
this._transporter.call(methodName, params, cb); | ||
var publicMethodName = createPublicMethodName(this._name, methodName); | ||
this._transporter.call(publicMethodName, args, cb); | ||
}; | ||
@@ -57,4 +82,5 @@ | ||
if (typeof this.methods[method] === 'undefined') { | ||
this.methods[method] = function (params, cb) { | ||
this.call(method, params, cb); | ||
this.methods[method] = function () { | ||
var args = [method].concat(arguments); | ||
this.call.apply(this, args); | ||
}.bind(this); | ||
@@ -61,0 +87,0 @@ } |
'use strict'; | ||
var createMethodName = require('./create-method-name'); | ||
var createPublicMethodName = require('./create-method-name'); | ||
@@ -23,4 +23,4 @@ function Server(transporter, options) { | ||
Server.prototype._init = function () { | ||
var methodName = createMethodName(this._name, 'methodList'); | ||
this._transporter.on(methodName, function (params, cb) { | ||
var methodName = createPublicMethodName(this._name, 'methodList'); | ||
this._transporter.on(methodName, function (args, cb) { | ||
cb(null, this._methods); | ||
@@ -30,16 +30,19 @@ }.bind(this)); | ||
Server.prototype.on = function (message, cb) { | ||
if (!message) { | ||
throw new TypeError('Bograch.on requires a message'); | ||
Server.prototype.on = function (methodName, method) { | ||
if (!methodName) { | ||
throw new TypeError('Bograch.on requires a methodName'); | ||
} | ||
if (typeof message !== 'string') { | ||
throw new TypeError('Bograch.on requires a message of string type'); | ||
if (typeof methodName !== 'string') { | ||
throw new TypeError('Bograch.on requires a methodName of string type'); | ||
} | ||
if (cb === null || typeof cb !== 'function') { | ||
if (method === null || typeof method !== 'function') { | ||
throw new TypeError('Bograch.on requires a callback function'); | ||
} | ||
this._methods.push(message); | ||
var methodName = createMethodName(this._name, message); | ||
this._transporter.on(methodName, cb); | ||
this._methods.push(methodName); | ||
var publicMethodName = createPublicMethodName(this._name, methodName); | ||
this._transporter.on(publicMethodName, function (args, cb) { | ||
args.push(cb); | ||
method.apply({}, args); | ||
}); | ||
}; | ||
@@ -46,0 +49,0 @@ |
{ | ||
"name": "bograch", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "A communication gateway for NodeJS microservices.", | ||
@@ -5,0 +5,0 @@ "main": "./lib", |
@@ -19,9 +19,9 @@ 'use strict'; | ||
boClient.call(); | ||
}).to.throw(TypeError, 'Bograch.on requires a message'); | ||
}).to.throw(TypeError, 'Bograch.on requires a methodName'); | ||
}); | ||
it('should throw an error if the third parameter is not a function', function () { | ||
it('should not throw an error if the third parameter is not a function', function () { | ||
expect(function () { | ||
boClient.call('foo', null, 4234); | ||
}).to.throw(TypeError, 'Bograch.call has an invalid callback function'); | ||
}).not.to.throw(Error); | ||
}); | ||
@@ -28,0 +28,0 @@ |
@@ -17,3 +17,3 @@ 'use strict'; | ||
boServer.on(); | ||
}).to.throw(TypeError, 'Bograch.on requires a message'); | ||
}).to.throw(TypeError, 'Bograch.on requires a methodName'); | ||
}); | ||
@@ -20,0 +20,0 @@ |
@@ -12,5 +12,5 @@ 'use strict'; | ||
Transporter.prototype.call = function (method, params, cb) { | ||
Transporter.prototype.call = function (method, args, cb) { | ||
if (this._methods[method]) { | ||
this._methods[method](params, cb); | ||
this._methods[method](args, cb); | ||
} | ||
@@ -17,0 +17,0 @@ }; |
1482494
24
396