node-red-contrib-motechat
Version:
ultranet topic and payload communication
27 lines (22 loc) • 846 B
JavaScript
const debug = require('../lib/mcClient.js').debug
module.exports = (RED) => {
function payload(config) {
RED.nodes.createNode(this, config)
const node = this
this.payload = config.payload
this.payloadType = config.payloadType
node.on('input', (msg, send, done) => {
send = send || function() { node.send.apply(node, arguments) }
let value = RED.util.evaluateNodeProperty(this.payload, this.payloadType, this)
if (!msg.payload) msg.payload = {}
if (this.payloadType == 'json' && typeof msg.payload == 'object') {
Object.assign(msg.payload, value)
} else {
msg.payload = value
}
send(msg)
if (done) done()
})
}
RED.nodes.registerType("payload", payload)
}