UNPKG

@openinc/parse-server-opendash

Version:
66 lines (65 loc) 2.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.publishDataItem = publishDataItem; const __1 = require(".."); const config_1 = require("../../config"); const LOG_PREFIX = "[@openinc/parse-server-opendash][open.WARE] "; async function publishDataItem(dataItem, usermail, update) { const active = config_1.ConfigInstance.getInstance().getBoolean("OPENWARE"); if (!active) { return console.log(LOG_PREFIX + "OPENWARE = false => Sending Data to open.WARE is not available."); } const token = await (0, __1.getToken)(usermail); if (!token) { return console.log(LOG_PREFIX + "OPENWARE_JWT_TOKEN OR _JWT_SECERT/_ADMIN_EMAIL/_ISS = undefined => Sending Data to open.WARE is not available."); } const baseurl = config_1.ConfigInstance.getInstance().get("OPENWARE_BASEURL"); if (!baseurl) { return console.log(LOG_PREFIX + "OPENWARE_BASEURL = undefined => Sending Data to open.WARE is not available."); } const url = baseurl + `/api/data/${update ? "update" : "push"}`; const headers = { Authorization: "Bearer " + token, }; const response = await fetch(url, { method: "POST", headers, body: JSON.stringify(dataItem), }); // console.log( // LOG_PREFIX + // "PUBLISH DEBUG: \n" + // `STATUS=${response.status}\n` + // `URL=${url}\n` + // `TOKEN=${token}\n` + // "PAYLOAD = " + // JSON.stringify(dataItem, null, 2) + // "\n" + // (await response.text()) // ); if (!response.ok) { const text = await response.text(); console.log(LOG_PREFIX + "PUBLISH ERROR Bad Status Code: " + response.status + "\n" + text); console.log(LOG_PREFIX + "PUBLISH ERROR DEBUG: \n" + "URL = " + url + "\n" + "TOKEN = " + token + "\n" + "PAYLOAD = " + JSON.stringify(dataItem, null, 2) + "\n" + text); throw new Error(LOG_PREFIX + "Bad Status Code: " + response.status + "\n" + text); } console.log(LOG_PREFIX + "PUBLISH SUCCESS " + (await response.text())); }