diff --git a/api/v3.0.0/index.js b/api/v3.0.0/index.js index 5ce2480f28..f6ef06fe4a 100644 --- a/api/v3.0.0/index.js +++ b/api/v3.0.0/index.js @@ -19,7 +19,7 @@ var error = require("./../../error"); var GithubHandler = module.exports = function(client) { this.client = client; - this.routes = JSON.parse(Fs.readFileSync(__dirname + "/routes.json", "utf8")); + this.routes = require("./routes.json"); }; var proto = { @@ -33,8 +33,20 @@ var proto = { } }; -["gists", "gitdata", "issues", "authorization", "orgs", "statuses", "pullRequests", "repos", "user", "events", "releases", "search", "markdown", "gitignore", "misc"].forEach(function(api) { - Util.extend(proto, require("./" + api)); -}); +Util.extend(proto, require("./gists")); +Util.extend(proto, require("./gitdata")); +Util.extend(proto, require("./issues")); +Util.extend(proto, require("./authorization")); +Util.extend(proto, require("./orgs")); +Util.extend(proto, require("./statuses")); +Util.extend(proto, require("./pullRequests")); +Util.extend(proto, require("./repos")); +Util.extend(proto, require("./user")); +Util.extend(proto, require("./events")); +Util.extend(proto, require("./releases")); +Util.extend(proto, require("./search")); +Util.extend(proto, require("./markdown")); +Util.extend(proto, require("./gitignore")); +Util.extend(proto, require("./misc")); GithubHandler.prototype = proto; diff --git a/generate.js b/generate.js index a5e553dc60..b8ba62cc15 100755 --- a/generate.js +++ b/generate.js @@ -242,11 +242,15 @@ var main = module.exports = function(versions, tests, restore) { var sectionNames = Object.keys(sections); Util.log("Writing index.js file for version " + version); + + var scripts = sectionNames.map(function(sectionName) { + return 'Util.extend(proto, require("./' + sectionName + '"));'; + }).join('\n'); Fs.writeFileSync(Path.join(dir, "index.js"), IndexTpl .replace("<%name%>", defines.constants.name) .replace("<%description%>", defines.constants.description) - .replace("<%scripts%>", "\"" + sectionNames.join("\", \"") + "\""), + .replace("<%scripts%>", scripts), "utf8"); Object.keys(sections).forEach(function(section) { diff --git a/index.js b/index.js index 0c062efcf8..09ce34124d 100644 --- a/index.js +++ b/index.js @@ -180,7 +180,15 @@ var Client = module.exports = function(config) { this.debug = Util.isTrue(config.debug); this.version = config.version; - var cls = require("./api/v" + this.version); + var cls; + if (this.version === '3.0.0') { + cls = require("./api/v3.0.0"); + } else { + if (process.browser) { + throw new Error('only version 3.0.0 is supported in the browser'); + } + cls = require("./api/v" + this.version); + } this[this.version] = new cls(this); var pathPrefix = ""; @@ -765,7 +773,13 @@ var Client = module.exports = function(config) { port: port, path: path, method: method, - headers: headers + headers: headers, + + // https://github.com/substack/https-browserify/pull/1 + scheme: protocol, + + // https://github.com/substack/http-browserify/pull/90 + withCredentials: false }; if (this.config.rejectUnauthorized !== undefined) @@ -775,12 +789,18 @@ var Client = module.exports = function(config) { console.log("REQUEST: ", options); function httpSendRequest() { - var req = require(protocol).request(options, function(res) { + var p = protocol === 'https' ? require('https') : require('http'); + var req = p.request(options, function(res) { if (self.debug) { console.log("STATUS: " + res.statusCode); console.log("HEADERS: " + JSON.stringify(res.headers)); } - res.setEncoding("utf8"); + if (res.setEncoding) { + // This method does not exist in the browser, so we just skip it for now. + // https://github.com/substack/http-browserify/issues/21 + // https://github.com/substack/http-browserify/pull/10 + res.setEncoding("utf8"); + } var data = ""; res.on("data", function(chunk) { data += chunk; diff --git a/templates/index.js.tpl b/templates/index.js.tpl index b5a585ff41..5246f99fe5 100644 --- a/templates/index.js.tpl +++ b/templates/index.js.tpl @@ -19,7 +19,7 @@ var error = require("./../../error"); var GithubHandler = module.exports = function(client) { this.client = client; - this.routes = JSON.parse(Fs.readFileSync(__dirname + "/routes.json", "utf8")); + this.routes = require("./routes.json"); }; var proto = { @@ -33,8 +33,6 @@ var proto = { } }; -[<%scripts%>].forEach(function(api) { - Util.extend(proto, require("./" + api)); -}); +<%scripts%> GithubHandler.prototype = proto;