Skip to content

Commit ad1ceaf

Browse files
committed
Add support for globally configurable headers. Implementation idea from @greggman
1 parent f384f3b commit ad1ceaf

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ var github = new GitHubApi({
3737
protocol: "https",
3838
host: "github.my-GHE-enabled-company.com",
3939
pathPrefix: "/api/v3", // for some GHEs
40-
timeout: 5000
40+
timeout: 5000,
41+
headers: {
42+
"user-agent": "My-Cool-GitHub-App", // GitHub is happy with a unique user agent
43+
}
4144
});
4245
github.user.getFollowingFromUser({
4346
// optional:

index.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ var Url = require("url");
173173
* }
174174
**/
175175
var Client = module.exports = function(config) {
176+
config.headers = config.headers || {};
176177
this.config = config;
177178
this.debug = Util.isTrue(config.debug);
178179

@@ -721,19 +722,21 @@ var Client = module.exports = function(config) {
721722
}
722723
}
723724

724-
if (!msg.headers)
725-
msg.headers = {};
726-
Object.keys(msg.headers).forEach(function(header) {
727-
var headerLC = header.toLowerCase();
728-
if (self.requestHeaders.indexOf(headerLC) == -1)
729-
return;
730-
headers[headerLC] = msg.headers[header];
731-
});
725+
function addCustomHeaders(customHeaders) {
726+
Object.keys(customHeaders).forEach(function(header) {
727+
var headerLC = header.toLowerCase();
728+
if (self.requestHeaders.indexOf(headerLC) == -1)
729+
return;
730+
headers[headerLC] = customHeaders[header];
731+
});
732+
}
733+
addCustomHeaders(Util.extend(msg.headers || {}, this.config.headers));
734+
732735
if (!headers["user-agent"])
733736
headers["user-agent"] = "NodeJS HTTP Client";
734737

735-
if (!headers["accept"])
736-
headers["accept"] = this.config.requestMedia || this.constants.requestMedia;
738+
if (!("accept" in headers))
739+
headers.accept = this.config.requestMedia || this.constants.requestMedia;
737740

738741
var options = {
739742
host: host,

0 commit comments

Comments
 (0)