sp-rest-proxy
Version:
SharePoint REST API Proxy for Node.js and Express local serve
80 lines • 3.57 kB
JavaScript
;
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.GetRouter = void 0;
var fs = require("fs");
var path = require("path");
var BasicRouter_1 = require("../BasicRouter");
var headers_1 = require("../../utils/headers");
var GetRouter = (function (_super) {
__extends(GetRouter, _super);
function GetRouter(context, settings) {
var _this = _super.call(this, context, settings) || this;
_this.router = function (req, res) {
if (_this.serveLocalResources(req, res)) {
return;
}
var endpointUrl = _this.url.apiEndpoint(req);
_this.logger.info('\nGET (generic): ' + endpointUrl);
var headers = (0, headers_1.getHeaders)(req.headers);
if (endpointUrl.indexOf('/ScriptResource.axd') !== -1) {
var axdUrlArr = endpointUrl.split('/ScriptResource.axd');
endpointUrl = "".concat(axdUrlArr[0].split('/').splice(0, 3).join('/'), "/ScriptResource.axd").concat(axdUrlArr[1]);
_this.sp.fetch(endpointUrl, { headers: headers })
.then(_this.handlers.isOK)
.then(function (d) { return _this.handlers.response(res)(d, function (r) { return r.text(); }); })
.catch(_this.handlers.error(res));
return;
}
_this.sp.fetch(endpointUrl, { headers: headers })
.then(_this.handlers.isOK)
.then(function (r) {
return _this.handlers.response(res)(r, function (r) { return r.buffer(); });
})
.catch(_this.handlers.error(res));
};
return _this;
}
GetRouter.prototype.serveLocalResources = function (req, res) {
var staticIndexUrl = '/index.html';
if (req.url !== '/') {
staticIndexUrl = req.url.split('?')[0];
}
else {
var pageContent = String(fs.readFileSync(path.join(this.settings.staticRoot, staticIndexUrl)));
pageContent = pageContent.replace('##proxyVersion#', this.settings.metadata.version);
res.send(pageContent);
return true;
}
if (req.url === '/config') {
var response = {
siteUrl: this.ctx.siteUrl,
username: this.ctx.authOptions.username || 'Add-In'
};
res.json(response);
return true;
}
if (fs.existsSync(path.join(this.settings.staticRoot, staticIndexUrl))) {
res.sendFile(path.join(this.settings.staticRoot, staticIndexUrl));
return true;
}
return false;
};
return GetRouter;
}(BasicRouter_1.BasicRouter));
exports.GetRouter = GetRouter;
//# sourceMappingURL=genericGet.js.map