UNPKG

@mindconnect/mindconnect-nodejs

Version:

MindConnect Library for NodeJS (community based)

99 lines 4.99 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const debug = require("debug"); const node_fetch_1 = require("node-fetch"); const utils_1 = require("./utils"); const HttpsProxyAgent = require("https-proxy-agent"); const log = debug("mindconnect"); /** * Base class for mindconnect agent (and setup) which provides headers and proxy handling. * * @export * @abstract * @class MindConnectBase */ class MindConnectBase { constructor() { this._headers = { Accept: "*/*", "X-Powered-By": "meowz", "User-Agent": "mindconnect-nodejs (3.7.0)" }; /** * Http headers used for /exchange endpoint handling. * * @protected * @memberof MindConnectBase */ this._multipartHeaders = Object.assign(Object.assign({}, this._headers), { "Content-Type": "multipart/mixed; boundary=mindspheremessage" }); this._multipartFormData = Object.assign(Object.assign({}, this._headers), { "Content-Type": "multipart/form-data; boundary=--mindsphere" }); /** * Http headers used for onboarding message. * * @protected * @memberof MindConnectBase */ this._apiHeaders = Object.assign(Object.assign({}, this._headers), { "Content-Type": "application/json" }); this._octetStreamHeaders = Object.assign(Object.assign({}, this._headers), { "Content-Type": "application/octet-stream" }); /** * Http headers used to register the client assertion and acquire the /exchange token. * * @protected * @memberof MindConnectBase */ this._urlEncodedHeaders = Object.assign(Object.assign({}, this._headers), { "Content-Type": "application/x-www-form-urlencoded" }); const proxy = process.env.http_proxy || process.env.HTTP_PROXY; log(`Proxy: ${proxy}`); this._proxyHttpAgent = proxy ? new HttpsProxyAgent(proxy) : null; } HttpAction({ verb, gateway, baseUrl, authorization, body, message, octetStream, multiPartFormData, additionalHeaders, noResponse, rawResponse, returnHeaders }) { return __awaiter(this, void 0, void 0, function* () { additionalHeaders = additionalHeaders || {}; let apiheaders = octetStream ? this._octetStreamHeaders : this._apiHeaders; apiheaders = multiPartFormData ? this._multipartFormData : apiheaders; let headers = Object.assign(Object.assign({}, apiheaders), { Authorization: `Bearer ${authorization}` }); if (verb === "GET" || verb === "DELETE") { delete headers["Content-Type"]; } headers = utils_1.removeUndefined(Object.assign(Object.assign({}, headers), additionalHeaders)); const url = `${gateway}${baseUrl}`; log(`${message} Headers ${JSON.stringify(headers)} Url ${url}`); try { const request = { method: verb, headers: headers, agent: this._proxyHttpAgent }; if (verb !== "GET" && verb !== "DELETE") { request.body = octetStream || multiPartFormData ? body : JSON.stringify(body); } const response = yield node_fetch_1.default(url, request); !response.ok && utils_1.throwError(`${response.statusText} ${yield response.text()}`); (response.status < 200 || response.status > 299) && utils_1.throwError(`invalid response ${JSON.stringify(response)}`); if (rawResponse) return response; if (noResponse) { if (returnHeaders) { return response.headers; } return {}; } const json = yield response.json(); log(`${message} Response ${JSON.stringify(json)}`); return json; } catch (err) { log(err); throw new Error(`Network error occured ${err.message}`); } }); } } exports.MindConnectBase = MindConnectBase; //# sourceMappingURL=mindconnect-base.js.map