UNPKG

node-red-contrib-vocalrec

Version:

Nodos personalizados de Node-RED para la API de VocalRec

58 lines (44 loc) 2.12 kB
const axios = require('axios'); const https = require('https'); module.exports = function(RED) { function VocalrecFindTransactionsNode(config) { RED.nodes.createNode(this, config); this.server = RED.nodes.getNode(config.server); const node = this; node.status({ fill: "grey", shape: "ring", text: "inactivo" }); node.on('input', function(msg) { if (!node.server) { node.error("No se ha configurado el servidor VocalRec"); return; } const url = `${node.server.base_url}/api/findTransactions`; const payload = { expression: msg.expression || config.expression, start_date: msg.start_date || config.start_date, end_date: msg.end_date || config.end_date, limit: msg.limit || config.limit || 100 }; const headers = { 'Content-Type': 'application/json' }; if (node.server.credentials && node.server.credentials.token) { headers['Authorization'] = `Bearer ${node.server.credentials.token}`; } node.status({ fill: "blue", shape: "dot", text: "buscando transacciones..." }); const httpsAgent = new https.Agent({ rejectUnauthorized: false }); axios.post(url, payload, { headers,httpsAgent }) .then(response => { node.status({ fill: "green", shape: "dot", text: "transacciones encontradas" }); msg.payload = response.data; node.send(msg); }) .catch(error => { node.status({ fill: "red", shape: "ring", text: "error" }); node.error(`Error al buscar transacciones: ${error.message}`, msg); }); }); } RED.nodes.registerType("vocalrec-findtransactions", VocalrecFindTransactionsNode); };