-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIFTTTNodeAction.js
106 lines (84 loc) · 3.78 KB
/
IFTTTNodeAction.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
'use strict';
var ifttt = require('ifttt');
var iftttNodeActionField = require('./IFTTTNodeActionField');
var util = require('util');
var utility = require('./utility');
module.exports = {
class: function(node) {
function action() {
action.super_.call(this, node.endpoint);
}
util.inherits(action, ifttt.Action);
action.prototype.node = node;
action.prototype._getResponseData = function(request, requestPayload, callback) {
var fields = node.fields;
if (!Array.isArray(fields)) {
return action.generateErrorResponse('fields is unexpected type \'' + typeof fields + '\'', true, callback);
}
const createResult = function() {
return {
'id': node.endpoint + '_' + new Date()
};
};
const handleTest = function(action, callback) {
var results = [];
var result = createResult();
for(var index = 0; index < fields.length; index ++) {
var field = fields[index];
if (typeof field === 'object' && typeof field.name === 'string') {
const fieldValue = requestPayload.getField(field.name);
if (field.sampleDataInvalid && fieldValue === field.sampleDataInvalid) {
return action.generateErrorResponse('Invalid value \'' + fieldValue + '\' for action field \'' + field.name + '\'', true, callback);
} else if (field.sampleDataValid && fieldValue === field.sampleDataValid) {
} else {
// TODO: should report unexpected
}
}
}
results.push(result);
return callback(null, results);
};
if (utility.isTestMode(request)) {
return handleTest(this, callback);
}
var nodePayload = {
timestamp: new Date().toISOString(),
fields: {}
};
// TODO: error if required field isn't provided
// TODO: ensure we have all fields configured for response requirements
for(var index = 0; index < fields.length; index ++) {
const field = fields[index];
if (typeof field === 'object' && typeof field.name === 'string') {
const fieldValue = requestPayload.getField(field.name);
if (typeof fieldValue === 'undefined' && field.required) {
return action.generateErrorResponse('Value required for action field \'' + field.name + '\'', true, callback);
}
nodePayload.fields[field.name] = fieldValue;
}
}
this.node.emit("input", {
payload: nodePayload
});
// TODO: double check if we need to respond with the processed fields
var results = [
createResult()
];
return callback(null, results);
}
return action;
},
createDefault: function(node) {
const result = new (this.class(node))();
if (Array.isArray(node.fields)) {
for(var index = 0; index < node.fields.length; index ++) {
const field = node.fields[index];
const actionFieldInstance = iftttNodeActionField.createDefault(field);
result.registerField(actionFieldInstance);
}
} else {
node.error("fields of unexpected type '" + typeof node.fields + "'");
}
return result;
}
};