Skip to content

Instantly share code, notes, and snippets.

@mdegat01
Created December 16, 2021 16:36
Show Gist options
  • Save mdegat01/7c3b0e8cfff0fb995ad995375e2dc4e2 to your computer and use it in GitHub Desktop.
Save mdegat01/7c3b0e8cfff0fb995ad995375e2dc4e2 to your computer and use it in GitHub Desktop.
Get env value subflow
[
{
"id": "75c36e89506227cf",
"type": "subflow",
"name": "Get env value",
"info": "Gets the value provided for an env variable.\nIf value is prefixed with `$:` then renders it\nas a JSONata expression. Otherwise value returned\nas is.",
"category": "",
"in": [
{
"x": 40,
"y": 40,
"wires": [
{
"id": "70528e431a51b89b"
}
]
}
],
"out": [
{
"x": 400,
"y": 40,
"wires": [
{
"id": "70528e431a51b89b",
"port": 0
}
]
},
{
"x": 400,
"y": 100,
"wires": [
{
"id": "a1f7df8ca3e7d2cc",
"port": 0
}
]
}
],
"env": [
{
"name": "name",
"type": "str",
"value": "",
"ui": {
"icon": "font-awesome/fa-tag",
"label": {
"en-US": "Name"
},
"type": "input",
"opts": {
"types": [
"str"
]
}
}
},
{
"name": "value",
"type": "env",
"value": "",
"ui": {
"icon": "font-awesome/fa-code",
"label": {
"en-US": "Value"
},
"type": "input",
"opts": {
"types": [
"str",
"env"
]
}
}
}
],
"meta": {},
"color": "#F3B567",
"outputLabels": [
"Success",
"Error"
],
"icon": "font-awesome/fa-code",
"status": {
"x": 200,
"y": 180,
"wires": [
{
"id": "1432b856e8c55703",
"port": 0
}
]
}
},
{
"id": "1432b856e8c55703",
"type": "status",
"z": "75c36e89506227cf",
"name": "",
"scope": null,
"x": 80,
"y": 180,
"wires": [
[]
]
},
{
"id": "70528e431a51b89b",
"type": "function",
"z": "75c36e89506227cf",
"name": "Get value",
"func": "let value = env.get(\"value\");\n\nif(typeof value == 'string' && value.startsWith('$:')){\n value = value.substring(2);\n let template = RED.util.prepareJSONataExpression(value, node);\n value = RED.util.evaluateJSONataExpression(template, msg, null);\n}\n\nmsg.env = msg.env || {};\nmsg.env[env.get(\"name\")] = value;\n\nnode.status({ fill: \"green\", shape: \"dot\", text: String(value) });\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 240,
"y": 40,
"wires": [
[]
]
},
{
"id": "c6edc2f950bd7f10",
"type": "catch",
"z": "75c36e89506227cf",
"name": "",
"scope": null,
"uncaught": false,
"x": 80,
"y": 100,
"wires": [
[
"a1f7df8ca3e7d2cc"
]
]
},
{
"id": "a1f7df8ca3e7d2cc",
"type": "function",
"z": "75c36e89506227cf",
"name": "Set fail status",
"func": "const VALUE = env.get(\"value\");\nnode.status({fill:\"red\",shape:\"ring\",text:`JSONata fail: ${VALUE}`});\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 260,
"y": 100,
"wires": [
[]
]
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment