mirror of https://github.com/BTCPrivate/copay.git
added type coercion for command-line
All inputs from the command are strings, but sometimes an argument needs to be something other than a string. This adds basic type coercion, so you can input objects and numbers as well.
This commit is contained in:
parent
dd2e8c585e
commit
5cc25e3e59
61
API.js
61
API.js
|
@ -26,12 +26,39 @@ API.prototype._init = function(opts) {
|
||||||
//self.wallet = self.opts.wallet ? self.opts.wallet : new Wallet();
|
//self.wallet = self.opts.wallet ? self.opts.wallet : new Wallet();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
API._coerceArgTypes = function(args, argTypes) {
|
||||||
|
for (var i in args) {
|
||||||
|
var arg = args[i];
|
||||||
|
var argType = argTypes[i][1];
|
||||||
|
if (typeof arg == 'string') {
|
||||||
|
switch (argType) {
|
||||||
|
case 'object':
|
||||||
|
args[i] = JSON.parse(arg);
|
||||||
|
break;
|
||||||
|
case 'number':
|
||||||
|
args[i] = Number(arg);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return args;
|
||||||
|
};
|
||||||
|
|
||||||
API.prototype._command = function(command, args, callback) {
|
API.prototype._command = function(command, args, callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
if (!command || command[0] == "_")
|
if (!command || command[0] == "_")
|
||||||
return callback(new Error('invalid command'));
|
return callback(new Error('invalid command'));
|
||||||
|
|
||||||
|
if (!API._checkArgTypes(command, args)) {
|
||||||
|
var argTypes = API.prototype[command].argTypes;
|
||||||
|
API._coerceArgTypes(args, argTypes)
|
||||||
|
if (!API._checkArgTypes(command, args)) {
|
||||||
|
throw new Error('Invalid arguments');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (typeof self[command] == 'function') {
|
if (typeof self[command] == 'function') {
|
||||||
var f = API.prototype[command];
|
var f = API.prototype[command];
|
||||||
if (f.argTypes[f.argTypes.length-1][1] == 'function')
|
if (f.argTypes[f.argTypes.length-1][1] == 'function')
|
||||||
|
@ -52,14 +79,8 @@ API._checkArgTypes = function(command, args) {
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
function checkArgs(name, args) {
|
|
||||||
if (!API._checkArgTypes(name, args))
|
|
||||||
throw new Error('Invalid arguments');
|
|
||||||
}
|
|
||||||
|
|
||||||
API.prototype.echo = function echo(str, callback) {
|
API.prototype.echo = function echo(str, callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
checkArgs(arguments.callee.name, arguments);
|
|
||||||
|
|
||||||
return callback(null, str);
|
return callback(null, str);
|
||||||
};
|
};
|
||||||
|
@ -70,10 +91,33 @@ API.prototype.echo.argTypes =
|
||||||
['callback', 'function']
|
['callback', 'function']
|
||||||
];
|
];
|
||||||
|
|
||||||
|
API.prototype.echoNumber = function echoNumber(num, callback) {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
|
return callback(null, num);
|
||||||
|
};
|
||||||
|
|
||||||
|
API.prototype.echoNumber.argTypes =
|
||||||
|
[
|
||||||
|
['num', 'number'],
|
||||||
|
['callback', 'function']
|
||||||
|
];
|
||||||
|
|
||||||
|
API.prototype.echoObject = function echoNumber(obj, callback) {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
|
return callback(null, obj);
|
||||||
|
};
|
||||||
|
|
||||||
|
API.prototype.echoObject.argTypes =
|
||||||
|
[
|
||||||
|
['obj', 'object'],
|
||||||
|
['callback', 'function']
|
||||||
|
];
|
||||||
|
|
||||||
/*
|
/*
|
||||||
API.prototype.getBalance = function(callback) {
|
API.prototype.getBalance = function(callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
checkArgs('getBalance', arguments);
|
|
||||||
|
|
||||||
return callback(null, self.wallet.getBalance([]));
|
return callback(null, self.wallet.getBalance([]));
|
||||||
};
|
};
|
||||||
|
@ -86,7 +130,6 @@ API.prototype.getBalance.argTypes =
|
||||||
|
|
||||||
API.prototype.getArgTypes = function getArgTypes(command, callback) {
|
API.prototype.getArgTypes = function getArgTypes(command, callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
checkArgs(arguments.callee.name, arguments);
|
|
||||||
|
|
||||||
if (command[0] == '_' || typeof API.prototype[command] != 'function')
|
if (command[0] == '_' || typeof API.prototype[command] != 'function')
|
||||||
return callback(new Error('Invalid command'));
|
return callback(new Error('Invalid command'));
|
||||||
|
@ -104,7 +147,6 @@ API.prototype.getArgTypes.argTypes =
|
||||||
|
|
||||||
API.prototype.getCommands = function getCommands(callback) {
|
API.prototype.getCommands = function getCommands(callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
checkArgs(arguments.callee.name, arguments);
|
|
||||||
|
|
||||||
var fs = [];
|
var fs = [];
|
||||||
|
|
||||||
|
@ -124,7 +166,6 @@ API.prototype.getCommands.argTypes =
|
||||||
|
|
||||||
API.prototype.getPublicKeyRingId = function getPublicKeyRingId(callback) {
|
API.prototype.getPublicKeyRingId = function getPublicKeyRingId(callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
checkArgs(arguments.callee.name, arguments);
|
|
||||||
|
|
||||||
return callback(null, self.publicKeyRing.id);
|
return callback(null, self.publicKeyRing.id);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue