-
Notifications
You must be signed in to change notification settings - Fork 0
/
execute-node.js
70 lines (63 loc) · 2.19 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
59
60
61
62
63
64
65
66
67
68
69
70
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 FullcontactExecuteNode(config) {
RED.nodes.createNode(this, config);
const node = this;
node.host = RED.nodes.getNode(config.host);
const got = require('got');
node.client = got.extend({
baseUrl: 'https://api.fullcontact.com/v3/',
headers: {
"Authorization": "Bearer " + node.host.api_key
}
});
const requiredArgs = ['resource', 'args'];
node.on('input', function (msg) {
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;
msg['_original'] = msg.payload;
node.client.post(
msg.payload.resource, {
body: JSON.stringify(msg.payload.args)
})
.then(function (res) {
node.status({
fill: "green",
shape: "dot",
text: `Success ${msg.payload.resource} !`
});
msg.payload = JSON.parse(res.body);
node.send(msg);
})
.catch(function (err) {
// Email address could not be found
handle_error(err, node);
msg.payload = false;
node.send(msg);
})
});
}
RED.nodes.registerType("fullcontact-execute", FullcontactExecuteNode);
};