1
- " use strict" ;
1
+ ' use strict' ;
2
2
var lib = require('./lib.js');
3
3
4
4
module.exports = function (RED) {
@@ -8,27 +8,31 @@ module.exports = function (RED) {
8
8
this.service = RED.nodes.getNode(config.service);
9
9
{ {/hasServiceParams} }
10
10
this.method = config.method;
11
-
12
11
{ {#methods} }
13
12
{ {#parameters} }
14
13
this.{ {&methodName} }_{ {&camelCaseName} } = config.{ {&methodName} }_{ {&camelCaseName} };
15
14
this.{ {&methodName} }_{ {&camelCaseName} }Type = config.{ {&methodName} }_{ {&camelCaseName} }Type || 'str';
16
15
{ {/parameters} }
17
16
{ {/methods} }
18
-
19
17
var node = this;
20
18
21
19
node.on('input', function (msg) {
20
+ var errorFlag = false ;
22
21
{{#domain} }
23
22
var client = new lib.{ {&className} }();
24
23
{ {/domain} }
25
24
{ {^domain} }
26
- var client = new lib.{ {&className} }({ domain: this.service.host } );
25
+ var client;
26
+ if (this.service && this.service.host) {
27
+ client = new lib.{{&className} }({ domain: this.service.host } );
28
+ } else {
29
+ node.error(' Host in configuration node is not specified.' , msg);
30
+ errorFlag = true ;
31
+ }
27
32
{ {/domain} }
28
-
29
33
{ {#isSecure} }
30
34
{ {#isSecureToken} }
31
- if (this.service && this.service.credentials && this.service.credentials.secureTokenValue) {
35
+ if (!errorFlag && this.service && this.service.credentials && this.service.credentials.secureTokenValue) {
32
36
if (this.service.secureTokenIsQuery) {
33
37
client.setToken(this.service.credentials.secureTokenValue,
34
38
this.service.secureTokenHeaderOrQueryName, true );
@@ -39,7 +43,7 @@ module.exports = function (RED) {
39
43
}
40
44
{ {/isSecureToken} }
41
45
{ {#isSecureApiKey} }
42
- if (this.service && this.service.credentials && this.service.credentials.secureApiKeyValue) {
46
+ if (!errorFlag && this.service && this.service.credentials && this.service.credentials.secureApiKeyValue) {
43
47
if (this.service.secureApiKeyIsQuery) {
44
48
client.setApiKey(this.service.credentials.secureApiKeyValue,
45
49
this.service.secureApiKeyHeaderOrQueryName, true );
@@ -50,18 +54,18 @@ module.exports = function (RED) {
50
54
}
51
55
{ {/isSecureApiKey} }
52
56
{ {#isSecureBasic} }
53
- if (this.service && this.service.credentials) {
57
+ if (!errorFlag && this.service && this.service.credentials) {
54
58
client.setBasicAuth(this.service.credentials.username, this.service.credentials.password);
55
59
}
56
60
{ {/isSecureBasic} }
57
61
{ {/isSecure} }
58
-
59
- client.body = msg.payload;
62
+ if (!errorFlag) {
63
+ client.body = msg.payload;
64
+ }
60
65
61
66
var result;
62
- var errorFlag = false;
63
67
{ {#methods} }
64
- if (node.method === '{ {&methodName} }') {
68
+ if (!errorFlag && node.method === '{ {&methodName} }') {
65
69
var parameters = [], nodeParam, nodeParamType;
66
70
{{#parameters} }
67
71
{ {#isBodyParam} }
@@ -82,10 +86,14 @@ module.exports = function (RED) {
82
86
83
87
result = client.{ {&methodName} }(parameters);
84
88
}
85
-
86
89
{ {/methods} }
90
+ if (!errorFlag && result === undefined) {
91
+ node.error(' Method is not specified.' , msg);
92
+ errorFlag = true ;
93
+ }
94
+
87
95
if (!errorFlag) {
88
- node.status({ fill: " blue" , shape: " dot" , text: " {{&className}}.status.requesting" } );
96
+ node.status({ fill: ' blue' , shape: ' dot' , text: ' {{&className}}.status.requesting' } );
89
97
result.then(function (response) {
90
98
if (response.body !== null && response.body !== undefined) {
91
99
msg.payload = response.body;
@@ -94,14 +102,13 @@ module.exports = function (RED) {
94
102
node.status({ } );
95
103
}).catch(function (error) {
96
104
node.error(error, msg);
97
- node.status({ fill: " red" , shape: " ring" , text: " node-red:common.status.error" } );
105
+ node.status({ fill: ' red' , shape: ' ring' , text: ' node-red:common.status.error' } );
98
106
});
99
107
}
100
108
});
101
109
}
102
110
103
- RED.nodes.registerType("{ {&nodeName} }", { {&className} }Node);
104
-
111
+ RED.nodes.registerType('{ {&nodeName} }', { {&className} }Node);
105
112
{ {#hasServiceParams} }
106
113
function { {&className} }ServiceNode(n) {
107
114
RED.nodes.createNode(this, n);
@@ -127,21 +134,21 @@ module.exports = function (RED) {
127
134
{ {/isSecure} }
128
135
}
129
136
130
- RED.nodes.registerType(" { {&nodeName} }-service" , { {&className} }ServiceNode, {
137
+ RED.nodes.registerType(' { {&nodeName} }-service' , { {&className} }ServiceNode, {
131
138
credentials: {
132
139
{{#isSecure} }
133
140
{ {#isSecureToken} }
134
- secureTokenValue: { type: " password" } ,
141
+ secureTokenValue: { type: ' password' } ,
135
142
{ {/isSecureToken} }
136
143
{ {#isSecureApiKey} }
137
- secureApiKeyValue: { type: " password" } ,
144
+ secureApiKeyValue: { type: ' password' } ,
138
145
{ {/isSecureApiKey} }
139
146
{ {#isSecureBasic} }
140
- username: { type: " text" } ,
141
- password: { type: " password" } ,
147
+ username: { type: ' text' } ,
148
+ password: { type: ' password' } ,
142
149
{ {/isSecureBasic} }
143
150
{ {/isSecure} }
144
- temp: { type: " text" }
151
+ temp: { type: ' text' }
145
152
}
146
153
});
147
154
{ {/hasServiceParams} }
0 commit comments