UNPKG

sp-rest-proxy

Version:

SharePoint REST API Proxy for Node.js and Express local serve

72 lines 3.38 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.RestBatchRouter = void 0; var BasicRouter_1 = require("../BasicRouter"); var headers_1 = require("../../utils/headers"); var RestBatchRouter = (function (_super) { __extends(RestBatchRouter, _super); function RestBatchRouter(context, settings) { var _this = _super.call(this, context, settings) || this; _this.router = function (request, response) { var endpointUrl = _this.url.apiEndpoint(request); _this.logger.info('\nPOST (batch): ' + endpointUrl); var reqBody = ''; if (request.body) { reqBody = request.body; _this.processBatchRequest(reqBody, request, response); } else { request.on('data', function (chunk) { return reqBody += chunk; }); request.on('end', function () { return _this.processBatchRequest(reqBody, request, response); }); } }; return _this; } RestBatchRouter.prototype.processBatchRequest = function (body, req, res) { var endpointUrl = this.url.apiEndpoint(req); body = req.rawBody; var transform = this.settings.processBatchMultipartBody; if (transform && typeof transform === 'function') { body = transform(body); } else { var regExp_1 = new RegExp('^(POST|GET|MERGE|DELETE) https?://localhost(:[0-9]+)?/', 'i'); var origin_1 = this.ctx.siteUrl.split('/').splice(0, 3).join('/'); body = body.split('\n').map(function (line) { if (regExp_1.test(line)) { var parts = line.split(' '); var method = parts.shift(); var version = parts.pop(); var endpoint = parts.join(' '); endpoint = "".concat(origin_1, "/").concat(endpoint.split('/').splice(3).join('/')); line = "".concat(method, " ").concat(endpoint, " ").concat(version); } return line; }).join('\n'); } this.logger.verbose('Request body:', body); var headers = (0, headers_1.getHeaders)(req.headers); this.sp.fetch(endpointUrl, { method: 'POST', headers: headers, body: body }) .then(this.handlers.isOK) .then(this.handlers.response(res)) .catch(this.handlers.error(res)); }; return RestBatchRouter; }(BasicRouter_1.BasicRouter)); exports.RestBatchRouter = RestBatchRouter; //# sourceMappingURL=restBatch.js.map