mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Merge pull request #3690 from cow0w/env_evalulating_in_template
Add support for evalulating {{env.<var>}} within a template node
This commit is contained in:
commit
d1d3b805f6
@ -44,6 +44,14 @@ module.exports = function(RED) {
|
|||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function parseEnv(key) {
|
||||||
|
var match = /^env\.(.+)/.exec(key);
|
||||||
|
if (match) {
|
||||||
|
return match[1];
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Custom Mustache Context capable to collect message property and node
|
* Custom Mustache Context capable to collect message property and node
|
||||||
* flow and global context
|
* flow and global context
|
||||||
@ -74,6 +82,11 @@ module.exports = function(RED) {
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// try env
|
||||||
|
if (parseEnv(name)) {
|
||||||
|
return this.cachedContextTokens[name];
|
||||||
|
}
|
||||||
|
|
||||||
// try flow/global context:
|
// try flow/global context:
|
||||||
var context = parseContext(name);
|
var context = parseContext(name);
|
||||||
if (context) {
|
if (context) {
|
||||||
@ -156,6 +169,17 @@ module.exports = function(RED) {
|
|||||||
var tokens = extractTokens(mustache.parse(template));
|
var tokens = extractTokens(mustache.parse(template));
|
||||||
var resolvedTokens = {};
|
var resolvedTokens = {};
|
||||||
tokens.forEach(function(name) {
|
tokens.forEach(function(name) {
|
||||||
|
var env_name = parseEnv(name);
|
||||||
|
if (env_name) {
|
||||||
|
var promise = new Promise((resolve, reject) => {
|
||||||
|
var val = RED.util.evaluateNodeProperty(env_name, 'env', node)
|
||||||
|
resolvedTokens[name] = val;
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
promises.push(promise);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var context = parseContext(name);
|
var context = parseContext(name);
|
||||||
if (context) {
|
if (context) {
|
||||||
var type = context.type;
|
var type = context.type;
|
||||||
|
@ -144,6 +144,28 @@ describe('template node', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('env var', function() {
|
||||||
|
before(function() {
|
||||||
|
process.env.TEST = 'xyzzy';
|
||||||
|
})
|
||||||
|
after(function() {
|
||||||
|
delete process.env.TEST;
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should modify payload from env variable', function(done) {
|
||||||
|
var flow = [{id:"n1",z:"t1", type:"template", field:"payload", template:"payload={{env.TEST}}",wires:[["n2"]]},{id:"n2",z:"t1",type:"helper"}];
|
||||||
|
helper.load(templateNode, flow, function() {
|
||||||
|
var n1 = helper.getNode("n1");
|
||||||
|
var n2 = helper.getNode("n2");
|
||||||
|
n2.on("input", function(msg) {
|
||||||
|
msg.should.have.property('payload', 'payload=xyzzy');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
n1.receive({payload:"foo",topic: "bar"});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('should modify payload from flow context', function(done) {
|
it('should modify payload from flow context', function(done) {
|
||||||
var flow = [{id:"n1",z:"t1", type:"template", field:"payload", template:"payload={{flow.value}}",wires:[["n2"]]},{id:"n2",z:"t1",type:"helper"}];
|
var flow = [{id:"n1",z:"t1", type:"template", field:"payload", template:"payload={{flow.value}}",wires:[["n2"]]},{id:"n2",z:"t1",type:"helper"}];
|
||||||
helper.load(templateNode, flow, function() {
|
helper.load(templateNode, flow, function() {
|
||||||
|
Loading…
Reference in New Issue
Block a user