UNPKG

connect-sdk-nodejs

Version:

SDK to communicate with the Worldline Global Collect platform using the Worldline Connect Server API

117 lines 4.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.sendMultipart = exports.sendJSON = void 0; const http = require("http"); const https = require("https"); const uuid = require("uuid"); const FormData = require("form-data"); const obfuscate = require("../utils/obfuscate"); const headers = require("../utils/headers"); function handleResponse(res, context, logger, uuidString) { if (context.isLoggingEnabled()) { let body = ""; if (headers.isBinaryContent(res.headers["content-type"])) { body = "<binary content>"; } else { res.setEncoding("utf8"); res.on("data", chunk => { body += chunk; }); } res.on("end", () => { if (context.isLoggingEnabled()) { // headers: case insensitive, body: case sensitive const obfuscatedHeaders = obfuscate.getObfuscated(res.headers, context, true); const obfuscatedBody = headers.isJSON(res.headers["content-type"]) ? obfuscate.getObfuscated(body, context, false) : body; logger("info", `Response from Message ID: ${uuidString}, status: ${res.statusCode}, headers: ${obfuscatedHeaders}, body: ${obfuscatedBody}`); } }); } } function handleError(error, context, logger, uuidString) { if (context.isLoggingEnabled()) { logger("error", `Error for Message ID:${uuidString}, error: ${JSON.stringify(error)}`); } } // Cannot use Promises for these two functions. // Doing so will cause logging to consume the body, making it unavailable for processing later on. // By using callbacks the two event listeners (logging + processing) will be added in the same flow and therefore will both be processed function sendJSON(options, postData, context, cb) { const logger = context.getLogger(); const uuidString = uuid.v4(); if (context.isLoggingEnabled()) { // headers: case insensitive, body: case sensitive const obfuscatedHeaders = obfuscate.getObfuscated(options.headers, context, true); const obfuscatedBody = obfuscate.getObfuscated(postData, context, false); logger("info", `Request with Message ID: ${uuidString}, ${options.method} to ${options.path}, headers: ${obfuscatedHeaders}, body: ${obfuscatedBody}`); } const h = options.protocol === "https:" ? https : http; const req = h.request(options, res => { handleResponse(res, context, logger, uuidString); cb(null, res); }); req.on("error", e => { handleError(e, context, logger, uuidString); cb(e, null); }); if (postData) { req.write(JSON.stringify(postData)); } req.end(); } exports.sendJSON = sendJSON; function sendMultipart(options, postData, boundary, context, cb) { const logger = context.getLogger(); const uuidString = uuid.v4(); if (context.isLoggingEnabled()) { // headers: case insensitive const obfuscatedHeaders = obfuscate.getObfuscated(options.headers, context, true); logger("info", `Request with Message ID: ${uuidString}, ${options.method} to ${options.path}, headers: ${obfuscatedHeaders}, body: <binary data>`); } const h = options.protocol === "https:" ? https : http; const req = h.request(options, res => { handleResponse(res, context, logger, uuidString); cb(null, res); }); req.on("error", e => { handleError(e, context, logger, uuidString); cb(e, null); }); const form = new FormData(); // Use the provided boundary instead of letting the form generate one form.setBoundary(boundary); if (postData) { for (const key in postData) { const item = postData[key]; if (typeof item === "object") { if (!item.fileName) { cb(new Error(key + ": fileName is required"), null); return; } if (!item.contentType) { cb(new Error(key + ": contentType is required"), null); return; } if (!item.content) { cb(new Error(key + ": content is required"), null); return; } const opts = { filename: item.fileName, contentType: item.contentType }; if (item.contentLength || item.contentLength === 0) { opts.knownLength = item.contentLength; } form.append(key, item.content, opts); } else if (typeof item !== "undefined") { form.append(key, item); } } } form.pipe(req); } exports.sendMultipart = sendMultipart; //# sourceMappingURL=connection.js.map