UNPKG

odata-batch

Version:

a simple lib for send and recieve calls odata batch

51 lines 1.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.requestsToBatch = void 0; const utils_1 = require("./utils"); const requestsToBatch = function (data, boundary, { contentType, accept }) { const changeSetNum = Math.random() * 100; const parseHeaders = (headers) => { if (!headers) { return [ `Content-Type: ${contentType}`, `Accept: ${accept}`, ]; } const _headers = Object.entries(headers).map(h => { const [header, value] = h; if (header.toLowerCase() === 'content-type' && contentType) { return `Content-Type: ${contentType}`; } if (header.toLowerCase() === 'accept' && accept) { return `Accept: ${accept}`; } return header + ': ' + value; }); return _headers; }; const aBatchByResponse = data.map(function (val) { const headers = parseHeaders(val.headers); return utils_1.flatten([ '--changeset_' + changeSetNum, 'Content-Type: application/http', 'Content-Transfer-Encoding: binary', '', val.method.toUpperCase() + ' ' + val.url + ' HTTP/1.1', headers, '', contentType === 'application/xml' ? val.data : JSON.stringify(val.data), '', ]); }); return utils_1.flatten([ '--batch_' + boundary, 'Content-Type: multipart/mixed; boundary=changeset_' + changeSetNum, '', utils_1.flatten(aBatchByResponse), '--changeset_' + changeSetNum + '--', '--batch_' + boundary + '--' ]) .join('\r\n'); }; exports.requestsToBatch = requestsToBatch; //# sourceMappingURL=request.js.map