Skip to content

Commit

Permalink
Headers passby enhancement #9
Browse files Browse the repository at this point in the history
  • Loading branch information
koltyakov committed Dec 2, 2016
1 parent aa1bf66 commit 0a0a9d1
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
test
typings
config/_private.conf.json
npm-debug.log
npm-debug.log
src/static/test
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "sp-rest-proxy",
"description": "SharePoint REST API Proxy for Node.js and Express local serve",
"version": "1.1.5",
"version": "1.1.6",
"main": "./src/index.js",
"scripts": {
"serve": "node ./src/server.js",
Expand Down
61 changes: 43 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ spf.restProxy = function(settings) {
fs.exists(configPath, function(exists) {
if (exists) {
_self.ctx = require(configPath);
_self.ctx.password = cpass.decode(_self.ctx.password);
if (typeof _self.ctx.password !== "undefined") {
_self.ctx.password = cpass.decode(_self.ctx.password);
}
if (callback && typeof callback === "function") {
callback();
}
Expand Down Expand Up @@ -133,19 +135,31 @@ spf.restProxy = function(settings) {
_self.routers.apiRestRouter.get("/*", function(req, res) {
_self.spr = _self.getCachedRequest(_self.spr);
console.log("GET: " + _self.ctx.siteUrl + req.originalUrl);

// var requestHeadersPass = {};
// if (req.headers["accept"]) {
// requestHeadersPass["accept"] = req.headers["accept"];
// }
// if (req.headers["content-type"]) {
// requestHeadersPass["content-type"] = req.headers["content-type"];
// }

var requestHeadersPass = {};
if (req.headers["accept"]) {
requestHeadersPass["accept"] = req.headers["accept"];
}
if (req.headers["content-type"]) {
requestHeadersPass["content-type"] = req.headers["content-type"];
var ignoreHeaders = [ "host", "referer", "if-none-match", "connection", "cache-control", "cache-control", "user-agent", "accept-encoding" ];
for (var prop in req.headers) {
if (req.headers.hasOwnProperty(prop)) {
if (ignoreHeaders.indexOf(prop.toLowerCase()) === -1) {
requestHeadersPass[prop] = req.headers[prop];
}
}
}

_self.spr.get(_self.ctx.siteUrl + req.originalUrl, {
headers: requestHeadersPass
})
.then(function (response) {
res.status(response.statusCode);
res.json(response);
res.json(response.body);
})
.catch(function (err) {
res.status(err.statusCode);
Expand All @@ -163,29 +177,40 @@ spf.restProxy = function(settings) {

_self.spr = _self.getCachedRequest(_self.spr);
console.log("POST: " + _self.ctx.siteUrl + req.originalUrl);

// var requestHeadersPass = {};
// if (req.headers["accept"]) {
// requestHeadersPass["accept"] = req.headers["accept"];
// }
// if (req.headers["content-type"]) {
// requestHeadersPass["content-type"] = req.headers["content-type"];
// }

var requestHeadersPass = {};
if (req.headers["accept"]) {
requestHeadersPass["accept"] = req.headers["accept"];
}
if (req.headers["content-type"]) {
requestHeadersPass["content-type"] = req.headers["content-type"];
var ignoreHeaders = [ "host", "referer", "if-none-match", "connection", "cache-control", "cache-control", "user-agent", "accept-encoding" ];
for (var prop in req.headers) {
if (req.headers.hasOwnProperty(prop)) {
if (ignoreHeaders.indexOf(prop.toLowerCase()) === -1) {
requestHeadersPass[prop] = req.headers[prop];
}
}
}

_self.spr.requestDigest(_self.ctx.siteUrl, {
headers: requestHeadersPass
})
.then(function (digest) {
requestHeadersPass["X-RequestDigest"] = digest;
requestHeadersPass["accept"] = requestHeadersPass["accept"] || "application/json; odata=verbose";
requestHeadersPass["content-type"] = requestHeadersPass["content-type"] || "application/json; odata=verbose";
return _self.spr.post(_self.ctx.siteUrl + req.originalUrl, {
headers: {
"X-RequestDigest": digest,
"Accept": "application/json; odata=verbose", // ToDo - pass through proxy
"Content-Type": "application/json; odata=verbose" // ToDo - pass through proxy
},
headers: requestHeadersPass,
body: reqBody
});
})
.then(function (response) {
res.status(response.statusCode);
res.json(response);
res.json(response.body);
})
.catch(function (err) {
res.status(err.statusCode);
Expand Down
33 changes: 33 additions & 0 deletions src/static/test/sitetitle.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html>
<head>
<title>Show title using REST</title>
<meta name="viewport" content="width=device-width, maximum-scale=1, user-scalable=no" />

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/es6-promise/4.0.5/es6-promise.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fetch/2.0.1/fetch.min.js"></script>
</head>
<body>
<h1 id="title">Loading...</h1>
<script>
fetch(new Request('/_api/web/title', {
credentials: 'include',
headers: new Headers({
"Accept": "application/json; odata=verbose"
})
}))
.then(function(response) {
return response.json();
})
.then(function(json) {
console.log(json);
title = json.d.Title;
var titleElement = document.getElementById('title');
titleElement.innerHTML = title;
})
.catch(function(ex) {
console.log('Error:', ex)
});
</script>
</body>
</html>

0 comments on commit 0a0a9d1

Please sign in to comment.