mirror of https://github.com/BTCPrivate/copay.git
use decorator
This commit is contained in:
parent
34f30e88ea
commit
4241287887
69
API.js
69
API.js
|
@ -58,15 +58,15 @@ API.prototype._command = function(command, args, callback) {
|
|||
var argTypes = API.prototype[command].argTypes;
|
||||
API._coerceArgTypes(args, argTypes)
|
||||
if (!API._checkArgTypes(command, args))
|
||||
throw new Error('Invalid arguments');
|
||||
throw new Error('invalid arguments');
|
||||
}
|
||||
|
||||
if (typeof self[command] == 'function') {
|
||||
if (typeof self["_cmd_" + command] == 'function') {
|
||||
var f = API.prototype[command];
|
||||
if (f.argTypes[f.argTypes.length-1][1] == 'function')
|
||||
return self[command].apply(self, args.concat([callback]));
|
||||
return self["_cmd_" + command].apply(self, args.concat([callback]));
|
||||
else
|
||||
return callback(null, self[command].apply(self, args));
|
||||
return callback(null, self["_cmd_" + command].apply(self, args));
|
||||
};
|
||||
|
||||
return callback(new Error('invalid command'));
|
||||
|
@ -89,41 +89,48 @@ API._checkArgTypes = function(command, args) {
|
|||
return true;
|
||||
};
|
||||
|
||||
API.prototype.echo = function echo(str, callback) {
|
||||
function decorate(command, argTypes) {
|
||||
var d = function() {
|
||||
API.prototype._command.call(this, command, Array.prototype.slice.call(arguments, 0));
|
||||
};
|
||||
|
||||
d.argTypes = argTypes;
|
||||
|
||||
return d;
|
||||
};
|
||||
|
||||
API.prototype._cmd_echo = function(str, callback) {
|
||||
var self = this;
|
||||
|
||||
return callback(null, str);
|
||||
};
|
||||
|
||||
API.prototype.echo.argTypes =
|
||||
[
|
||||
API.prototype.echo = decorate('echo', [
|
||||
['str', 'string'],
|
||||
['callback', 'function']
|
||||
];
|
||||
]);
|
||||
|
||||
API.prototype.echoNumber = function echoNumber(num, callback) {
|
||||
API.prototype._cmd_echoNumber = function(num, callback) {
|
||||
var self = this;
|
||||
|
||||
return callback(null, num);
|
||||
};
|
||||
|
||||
API.prototype.echoNumber.argTypes =
|
||||
[
|
||||
API.prototype.echoNumber = decorate('echoNumber', [
|
||||
['num', 'number'],
|
||||
['callback', 'function']
|
||||
];
|
||||
]);
|
||||
|
||||
API.prototype.echoObject = function echoNumber(obj, callback) {
|
||||
API.prototype._cmd_echoObject = function(obj, callback) {
|
||||
var self = this;
|
||||
|
||||
return callback(null, obj);
|
||||
};
|
||||
|
||||
API.prototype.echoObject.argTypes =
|
||||
[
|
||||
API.prototype.echoObject = decorate('echoObject', [
|
||||
['obj', 'object'],
|
||||
['callback', 'function']
|
||||
];
|
||||
]);
|
||||
|
||||
/*
|
||||
API.prototype.getBalance = function(callback) {
|
||||
|
@ -138,7 +145,7 @@ API.prototype.getBalance.argTypes =
|
|||
];
|
||||
*/
|
||||
|
||||
API.prototype.getArgTypes = function getArgTypes(command, callback) {
|
||||
API.prototype._cmd_getArgTypes = function(command, callback) {
|
||||
var self = this;
|
||||
|
||||
if (command[0] == '_' || typeof API.prototype[command] != 'function')
|
||||
|
@ -149,13 +156,12 @@ API.prototype.getArgTypes = function getArgTypes(command, callback) {
|
|||
return callback(null, argTypes);
|
||||
};
|
||||
|
||||
API.prototype.getArgTypes.argTypes =
|
||||
[
|
||||
API.prototype.getArgTypes = decorate('getArgTypes', [
|
||||
['command', 'string'],
|
||||
['callback', 'function']
|
||||
];
|
||||
]);
|
||||
|
||||
API.prototype.getCommands = function getCommands(callback) {
|
||||
API.prototype._cmd_getCommands = function(callback) {
|
||||
var self = this;
|
||||
|
||||
var fs = [];
|
||||
|
@ -169,29 +175,26 @@ API.prototype.getCommands = function getCommands(callback) {
|
|||
return callback(null, fs);
|
||||
};
|
||||
|
||||
API.prototype.getCommands.argTypes =
|
||||
[
|
||||
API.prototype.getCommands = decorate('getCommands', [
|
||||
['callback', 'function']
|
||||
];
|
||||
]);
|
||||
|
||||
API.prototype.getPublicKeyRingId = function getPublicKeyRingId(callback) {
|
||||
API.prototype._cmd_getPublicKeyRingId = function(callback) {
|
||||
var self = this;
|
||||
|
||||
return callback(null, self.wallet.publicKeyRing.walletId);
|
||||
};
|
||||
|
||||
API.prototype.getPublicKeyRingId.argTypes =
|
||||
[
|
||||
API.prototype.getPublicKeyRingId = decorate('getPublicKeyRingId', [
|
||||
['callback', 'function']
|
||||
];
|
||||
]);
|
||||
|
||||
API.prototype.help = function help(callback) {
|
||||
this.getCommands.apply(this, arguments);
|
||||
API.prototype._cmd_help = function(callback) {
|
||||
this._cmd_getCommands.apply(this, arguments);
|
||||
};
|
||||
|
||||
API.prototype.help.argTypes =
|
||||
[
|
||||
API.prototype.help = decorate('help', [
|
||||
['callback', 'function']
|
||||
];
|
||||
]);
|
||||
|
||||
module.exports = require('soop')(API);
|
||||
|
|
21
bin/copay
21
bin/copay
|
@ -20,15 +20,32 @@ var main = function() {
|
|||
var args = commander.args;
|
||||
|
||||
try {
|
||||
api._command(args[0], args.slice(1), function(err, result) {
|
||||
var command = args[0];
|
||||
var commandArgs = args.slice(1);
|
||||
|
||||
if (command[0] == '_' || typeof api[command] != 'function')
|
||||
throw new Error('invalid command');
|
||||
|
||||
api[command].apply(api, commandArgs.concat([function(err, result) {
|
||||
if (err)
|
||||
return console.log("" + err);
|
||||
|
||||
console.log(JSON.stringify(result, null, 2));
|
||||
});
|
||||
}]));
|
||||
} catch(err) {
|
||||
if (err.toString() == 'Error: invalid command') {
|
||||
console.log("" + err);
|
||||
}
|
||||
else if (err.toString() == 'Error: invalid arguments') {
|
||||
console.log("" + err);
|
||||
console.log('Arguments for ' + command + ':')
|
||||
api.getArgTypes(command, function(err, result) {
|
||||
console.log(JSON.stringify(result, null, 2));
|
||||
});
|
||||
}
|
||||
else
|
||||
throw (err);
|
||||
}
|
||||
};
|
||||
|
||||
if (require.main === module) {
|
||||
|
|
|
@ -61,7 +61,7 @@ describe('API', function() {
|
|||
it('should echo a number', function(done) {
|
||||
var api = new API();
|
||||
var num = 500;
|
||||
api.echo(num, function(err, result) {
|
||||
api.echoNumber(num, function(err, result) {
|
||||
result.should.equal(num);
|
||||
(typeof result).should.equal('number');
|
||||
done();
|
||||
|
@ -73,7 +73,7 @@ describe('API', function() {
|
|||
it('should echo an object', function(done) {
|
||||
var api = new API();
|
||||
var obj = {test:'test'};
|
||||
api.echo(obj, function(err, result) {
|
||||
api.echoObject(obj, function(err, result) {
|
||||
result.test.should.equal(obj.test);
|
||||
(typeof result).should.equal('object');
|
||||
done();
|
||||
|
@ -81,4 +81,53 @@ describe('API', function() {
|
|||
});
|
||||
});
|
||||
|
||||
describe('#getArgTypes', function() {
|
||||
it('should get the argTypes of echo', function(done) {
|
||||
var api = new API();
|
||||
api.getArgTypes('echo', function(err, result) {
|
||||
result[0][1].should.equal('string');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#getCommands', function() {
|
||||
it('should get all the commands', function(done) {
|
||||
var api = new API();
|
||||
var n = 0;
|
||||
|
||||
for (var i in api)
|
||||
if (i[0] != '_' && typeof api[i] == 'function')
|
||||
n++;
|
||||
|
||||
api.getCommands(function(err, result) {
|
||||
result.length.should.equal(n);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#getPublicKeyRingId', function() {
|
||||
it('should get a public key ring ID', function(done) {
|
||||
var api = new API();
|
||||
api.getPublicKeyRingId(function(err, result) {
|
||||
result.length.should.be.greaterThan(5);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#help', function() {
|
||||
it('should call _cmd_getCommands', function(done) {
|
||||
var api = new API();
|
||||
api._cmd_getCommands = function(callback) {
|
||||
(typeof arguments[0]).should.equal('function');
|
||||
callback(null, ['item']);
|
||||
}
|
||||
api.help(function(err, result) {
|
||||
result[0].should.equal('item');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue