1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00

update What3Words node to latest library

and fix credentials issue
closes #285
This commit is contained in:
Dave Conway-Jones 2017-03-09 23:17:40 +00:00
parent 20f7cb0a3d
commit 2f218740c6
4 changed files with 11 additions and 73 deletions

View File

@ -1,9 +1,9 @@
{ {
"name" : "node-red-node-what3words", "name" : "node-red-node-what3words",
"version" : "0.0.4", "version" : "0.0.5",
"description" : "A Node-RED node to convert locations to/from what3words", "description" : "A Node-RED node to convert locations to/from what3words",
"dependencies" : { "dependencies" : {
"geo.what3words" : "0.0.2" "geo.what3words" : "^2.0.0"
}, },
"repository" : { "repository" : {
"type":"git", "type":"git",

Binary file not shown.

View File

@ -1,8 +1,8 @@
<script type="text/x-red" data-template-name="what3words"> <script type="text/x-red" data-template-name="what3words">
<div class="form-row"> <div class="form-row">
<label for="node-config-input-pushkey"><i class="fa fa-key"></i> API Key</label> <label for="node-input-apikey"><i class="fa fa-key"></i> API Key</label>
<input type="password" id="node-config-input-pushkey"> <input type="password" id="node-input-apikey">
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-input-lang"><i class="fa fa-language"></i> Language</label> <label for="node-input-lang"><i class="fa fa-language"></i> Language</label>
@ -45,6 +45,9 @@
lang: {value:"en"}, lang: {value:"en"},
name: {value:""} name: {value:""}
}, },
credentials: {
apikey: {type: "password"}
},
color:"#DEBD5C", color:"#DEBD5C",
inputs:1, inputs:1,
outputs:1, outputs:1,
@ -54,35 +57,6 @@
}, },
labelStyle: function() { labelStyle: function() {
return this.name?"node_label_italic":""; return this.name?"node_label_italic":"";
},
oneditprepare: function() {
$.getJSON('nma/'+this.id,function(data) {
if (data.hasPassword) {
$('#node-config-input-pushkey').val('__PWRD__');
} else {
$('#node-config-input-pushkey').val('');
}
});
},
oneditsave: function() {
var credentials = {};
var newPass = $('#node-config-input-pushkey').val();
if (newPass != '__PWRD__') {
credentials.pushkey = newPass;
$.ajax({
url: 'nma/'+this.id,
type: 'POST',
data: credentials,
success: function(result){}
});
}
},
ondelete: function() {
$.ajax({
url: 'nma/'+this.id,
type: 'DELETE',
success: function(result) {}
});
} }
}); });
</script> </script>

View File

@ -6,11 +6,9 @@ module.exports = function(RED) {
var what3wordsNode = function(n) { var what3wordsNode = function(n) {
RED.nodes.createNode(this, n); RED.nodes.createNode(this, n);
this.lang = n.lang || "en"; this.lang = n.lang || "en";
var credentials = RED.nodes.getCredentials(n.id);
if ((credentials) && (credentials.hasOwnProperty("pushkey"))) { this.pushkey = credentials.pushkey; }
else { this.error("No what3words API key set"); }
this.w3w = new What3Words(this.pushkey);
var node = this; var node = this;
//if ( !node.credentials.apikey ) { this.error("No what3words API key set"); }
this.w3w = new What3Words(node.credentials.apikey);
var w1 = /^\*\w{6,31}$/; var w1 = /^\*\w{6,31}$/;
var w3 = /^\w+\.\w+\.\w+$/; var w3 = /^\w+\.\w+\.\w+$/;
this.on("input", function(msg) { this.on("input", function(msg) {
@ -69,41 +67,7 @@ module.exports = function(RED) {
else { node.warn("No useable data found. See info."); } else { node.warn("No useable data found. See info."); }
}); });
} }
RED.nodes.registerType("what3words", what3wordsNode); RED.nodes.registerType("what3words", what3wordsNode, {
credentials: { apikey: {type: "password"} }
var querystring = require('querystring');
RED.httpAdmin.get('/what3words/:id', RED.auth.needsPermission('what3words.read'), function(req, res) {
var credentials = RED.nodes.getCredentials(req.params.id);
if (credentials) {
res.send(JSON.stringify({hasPassword:(credentials.pushkey && credentials.pushkey !== "")}));
}
else {
res.send(JSON.stringify({}));
}
});
RED.httpAdmin.delete('/what3words/:id', RED.auth.needsPermission('what3words.write'), function(req, res) {
RED.nodes.deleteCredentials(req.params.id);
res.send(200);
});
RED.httpAdmin.post('/what3words/:id', RED.auth.needsPermission('what3words.write'), function(req, res) {
var body = "";
req.on('data', function(chunk) {
body += chunk;
});
req.on('end', function() {
var newCreds = querystring.parse(body);
var credentials = RED.nodes.getCredentials(req.params.id) || {};
if (newCreds.pushkey === "") {
delete credentials.pushkey;
}
else {
credentials.pushkey = newCreds.pushkey || credentials.pushkey;
}
RED.nodes.addCredentials(req.params.id, credentials);
res.send(200);
});
}); });
} }