45 lines
1.5 KiB
JavaScript
45 lines
1.5 KiB
JavaScript
|
|
module.exports = function (RED) {
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
function ZammadApiNode(config) {
|
||
|
|
RED.nodes.createNode(this, config);
|
||
|
|
const node = this;
|
||
|
|
|
||
|
|
node.configNode = RED.nodes.getNode(config.config);
|
||
|
|
if (!node.configNode) {
|
||
|
|
node.error("No zammad-config node configured");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
node.status({ fill: "blue", shape: "ring", text: "ready" });
|
||
|
|
|
||
|
|
node.on("input", function (msg, send, done) {
|
||
|
|
const method = (msg.method || "GET").toUpperCase();
|
||
|
|
const endpoint = msg.endpoint || msg.path || "/";
|
||
|
|
const body = msg.payload !== undefined ? msg.payload : msg.body;
|
||
|
|
const extraHeaders = msg.headers || {};
|
||
|
|
|
||
|
|
node.status({ fill: "yellow", shape: "dot", text: method + " " + endpoint });
|
||
|
|
|
||
|
|
node.configNode.sendRequest(method, endpoint, body, extraHeaders, function (err, result) {
|
||
|
|
if (err) {
|
||
|
|
node.status({ fill: "red", shape: "ring", text: "error" });
|
||
|
|
if (done) done(err);
|
||
|
|
else node.error(err, msg);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
msg.payload = result.body;
|
||
|
|
msg.statusCode = result.statusCode;
|
||
|
|
msg.zammad = result;
|
||
|
|
|
||
|
|
node.status({ fill: "green", shape: "dot", text: result.statusCode });
|
||
|
|
send(msg);
|
||
|
|
if (done) done();
|
||
|
|
});
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
RED.nodes.registerType("zammad-api", ZammadApiNode);
|
||
|
|
};
|