@isaac-platform/isaac-node-red
Version:
Set of Node-RED nodes to communicate with an ISAAC system
107 lines (106 loc) • 4.16 kB
JavaScript
const axios = require("axios"), USED_CONNECTION_NODES_CONTEXT = "isaac-used-connection-nodes";
function getApiUrl(server) {
let url = server.ipAddress;
return url.match(/^[a-zA-Z]+:\/\//) || (url = `http://${url}`), `${new URL(url).origin}/api/v1`;
}
function mergeConfig({ config, msg }) {
if (msg && typeof msg == "object") {
const mergedConfig = {};
if (config && (config.action === msg.action || !msg.action || !config.action) && Object.assign(mergedConfig, config), Object.assign(mergedConfig, msg), msg.config) {
if (typeof msg.config == "object")
Object.assign(mergedConfig, msg.config);
else if (typeof msg.config == "string") {
const parsedConfig = JSON.parse(msg.config);
Object.assign(mergedConfig, parsedConfig);
}
}
return mergedConfig;
}
return config;
}
function parseStringArray(input) {
if (!input)
return [];
let array;
if (Array.isArray(input))
array = input;
else if (typeof input == "string")
array = input.split(",");
else
return [];
return array.filter((val) => val && typeof val == "string").map((str) => str.trim());
}
async function getSubsystem(server) {
const { data } = await axios.get(`${getApiUrl(server)}/subsystems?includePlayers=true`), subsystem = data.find((_subsystem) => _subsystem.externalId === server.subsystemExternalId);
if (subsystem)
return subsystem;
throw new Error("Could not find subsystem by externalId");
}
function getPayload(mergedConfig) {
if (typeof mergedConfig.payload == "object")
return mergedConfig.payload;
if (typeof mergedConfig.payload == "string")
try {
return JSON.parse(mergedConfig.payload);
} catch {
}
throw new Error("Invalid payload, must be an object or a string that can be parsed into an object");
}
function getAction({ config, msg }) {
return msg && msg.action || config.action;
}
function getNiceAxiosErrorMessage(err) {
try {
if (err.isAxiosError && err.response.status && err.response.statusText && err.response.data && (typeof err.response.data == "string" || typeof err.response.data == "object" && err.response.data.message && typeof err.response.data.message == "string")) {
const message = typeof err.response.data == "string" ? err.response.data : err.response.data.message, statusCodePart = `[${err.response.status}]`, explanationParts = [err.response.statusText, message].filter(Boolean);
if (explanationParts.length)
return `${statusCodePart} ${explanationParts.join(": ")}`.trim();
}
} catch {
}
return null;
}
function incrementConnectionUsage(nodeThatUsesConnection, connectionNode) {
try {
if (!connectionNode)
return !1;
const { id } = connectionNode, context = nodeThatUsesConnection.context().global;
let value = context.get(USED_CONNECTION_NODES_CONTEXT);
return (!value || typeof value != "object") && (value = {}), !value[id] || typeof value[id] != "number" || value[id] < 1 ? value[id] = 1 : value[id]++, context.set(USED_CONNECTION_NODES_CONTEXT, value), !0;
} catch (e) {
return console.error(e), !1;
}
}
function decrementConnectionUsage(nodeThatUsesConnection, connectionNode) {
try {
const { id } = connectionNode, context = nodeThatUsesConnection.context().global, value = context.get(USED_CONNECTION_NODES_CONTEXT);
if (!value || typeof value != "object" || value[id] === 0)
return;
!value[id] || typeof value[id] != "number" || value[id] < 0 ? value[id] = 0 : value[id]--, context.set(USED_CONNECTION_NODES_CONTEXT, value);
} catch (e) {
console.error(e);
}
}
function isConnectionUsed(connectionNode) {
try {
const value = connectionNode.context().global.get(USED_CONNECTION_NODES_CONTEXT);
if (!value || typeof value != "object")
return !1;
const count = value[connectionNode.id];
return typeof count == "number" && count > 0;
} catch (e) {
return console.error(e), !1;
}
}
module.exports = {
incrementConnectionUsage,
getAction,
getApiUrl,
getNiceAxiosErrorMessage,
getPayload,
getSubsystem,
isConnectionUsed,
mergeConfig,
parseStringArray,
decrementConnectionUsage
};