-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrpccall.js
More file actions
74 lines (45 loc) · 1.5 KB
/
rpccall.js
File metadata and controls
74 lines (45 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"use strict";
const commands = require("./commands");
const simpleClient = require("./client");
module.exports = (connection) => {
if(!connection){
throw "Must include host and port to connect."
}
let client = simpleClient(connection);
let caller = {};
for(let command in commands){
caller[command] = (args, cb) => {
let params;
if (args instanceof Object && !(Array.isArray(args)) && !(args instanceof Function)) {
params = parseParams(commands[command], args);
} else if (args instanceof Function && !cb) {
cb = args;
} else if (Array.isArray(args) || args === null || args === undefined) {
params = args;
} else {
throw `${args} is invalid input.`
}
client.call(command.toLowerCase(), params, (err, res) => {
cb(err, res);
})
}
}
return caller;
}
let parseParams = (commandParams, args) => {
let userParams = [];
for(let arg of commandParams){
if(typeof arg === "string") {
userParams.push(args[arg]);
} else if (typeof arg === "object") {
let key = Object.keys(arg)[0];
let defaultVal = arg[key];
if(typeof args[key] !== "undefined") {
userParams.push(args[key]);
} else {
userParams.push(defaultVal);
}
}
}
return userParams;
}