UNPKG

@hypericon/node-red-hypertable

Version:

Node-RED plugin to interact with Hypertable

68 lines (50 loc) 2.04 kB
const { readConnection } = require("./common"); const axios = require("axios").default; // https://nodered.org/docs/creating-nodes/node-js const nodeInit = (RED) => { function readRecordNode(config) { RED.nodes.createNode(this, config); // Retrieve the connection config node this.connection = RED.nodes.getNode(config.connection); // this.log("Connection: " + JSON.stringify(this.connection, null, 2)); // Called whenever a message arrives at the node this.on('input', async (msg, send, done) => { /** @type string */ const collectionId = msg.collectionId || config.collectionId; if (!collectionId) return done(new Error("No collection ID is specified")); /** @type string */ const recordId = msg.recordId || config.recordId; if (!recordId) return done(new Error("No record ID is specified")); let baseUrl, projectId, apiKey; try { const conn = readConnection(this.connection); baseUrl = conn.baseUrl; // workspaceId = conn.workspaceId; projectId = conn.projectId; apiKey = conn.apiKey; } catch (error) { return done(error); } const path = `${baseUrl}api/v1/data/projects/${projectId}/collections/${collectionId}/records/${recordId}`; try { // this.log(`GET ${path}`); const response = await axios.get(path, { responseType: "json", headers: { "Authorization": `Basic ${apiKey}`, }, }); // this.log(`Collection data: ${JSON.stringify(response.data, null, 2)}`); msg.record = response.data; } catch (error) { this.error(error); return done(new Error(`Error making HTTP request: ${error}`)); } // Sends the message to following Nodes this.send(msg); }); } // Register the configured Node wih Node-RED RED.nodes.registerType("read-record", readRecordNode); } module.exports = nodeInit;