-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecute-node.js
58 lines (51 loc) · 2.09 KB
/
execute-node.js
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
module.exports = function(RED) {
this.isRequesting = false;
var handle_error = function(err, node) {
node.log(err.body);
node.status({ fill: "red", shape: "dot", text: err.message });
node.error(err.message);
};
function HunterioExecuteNode(config) {
const node = this;
RED.nodes.createNode(node, config);
node.host = RED.nodes.getNode(config.host);
const Hunter = require('hunterio');
node.client = new Hunter(this.host.api_key);
const requiredArgs = ['method', 'args'];
node.on('input', function(msg) {
console.log(node);
node.status({ fill: "blue", shape: "dot", text: `Try ${msg.payload.resource}.${msg.payload.method}...` });
var validation = requiredArgs.reduce((p, v) => {
if (!msg.payload[v]) {
handle_error(new Error(`No msg.payload.${v} provided, cancel`), node);
return false;
}
return p;
}, true);
// validation
if (!validation)
return false;
if (!node.client[msg.payload.method]) {
return handle_error(new Error(`Method ${msg.payload.method} is invalid, check documentation, cancel`), node);
}
msg['_original'] = msg.payload;
node.client[msg.payload.method](
msg.payload.args,
function(err, body) {
if (err) {
// handle error
handle_error(err, node);
msg.payload = false;
node.send(msg);
}
else {
// Will contain same body as the raw API call
node.status({ fill: "green", shape: "dot", text: `Success ${msg.payload.method} !` });
msg.payload = body;
node.send(msg);
}
})
});
}
RED.nodes.registerType("hunterio-execute", HunterioExecuteNode);
};