bitbucket-v2
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -1,27 +0,22 @@ | ||
/** | ||
* Copyright 2010 Ajax.org B.V. | ||
* | ||
* This product includes software developed by | ||
* Ajax.org B.V. (http://www.ajax.org/). | ||
* | ||
* Author: Fabian Jaokbs <fabian@ajax.org> | ||
*/ | ||
var AbstractApi = exports.AbstractApi = function(api) { | ||
class AbstractApi { | ||
constructor(api) { | ||
this.$api = api; | ||
}; | ||
} | ||
(function() { | ||
$createListener(callback) { | ||
return function callbackRunner(err, response) { | ||
if (err) { | ||
if (callback) { | ||
callback(err); | ||
} | ||
return; | ||
} | ||
this.$createListener = function(callback, key) { | ||
return function(err, response) { | ||
if (err) { | ||
callback && callback(err); | ||
return; | ||
} | ||
callback && callback(err, key ? response[key] : response); | ||
}; | ||
if (callback) { | ||
callback(err, response); | ||
} | ||
}; | ||
} | ||
} | ||
}).call(AbstractApi.prototype); | ||
module.exports = AbstractApi; |
@@ -1,14 +0,5 @@ | ||
/** | ||
* Copyright 2010 Ajax.org B.V. | ||
* | ||
* This product includes software developed by | ||
* Ajax.org B.V. (http://www.ajax.org/). | ||
* | ||
* Author: Fabian Jaokbs <fabian@ajax.org> | ||
*/ | ||
const Request = require('./request').Request; | ||
var Request = require("./request").Request; | ||
/** | ||
* Simple JavaScript GitHub API | ||
* Simple JavaScript Bitbucket API v2 | ||
* | ||
@@ -18,298 +9,197 @@ * Based on the PHP GitHub API project http://github.com/ornicar/php-github-api | ||
var BitBucket = exports.BitBucket = function(debug, proxy, http) { | ||
/** | ||
* Use debug mode (prints debug messages) | ||
*/ | ||
this.$debug = debug; | ||
const BitBucket = exports.BitBucket = function BitBucket(debug, proxy, http) { | ||
/** | ||
* Use debug mode (prints debug messages) | ||
*/ | ||
this.$debug = debug; | ||
/** | ||
* Define HTTP proxy in format localhost:3128 | ||
*/ | ||
if (proxy) { | ||
this.$proxy_host = proxy.split(':')[0]; | ||
this.$proxy_port = proxy.split(':')[1]; | ||
} | ||
if (http) { | ||
this.$use_http = true; | ||
} | ||
/** | ||
* The list of loaded API instances | ||
*/ | ||
this.$apis = []; | ||
/** | ||
* Define HTTP proxy in format localhost:3128 | ||
*/ | ||
if (proxy) { | ||
this.$proxy_host = proxy.split(':')[0]; | ||
this.$proxy_port = proxy.split(':')[1]; | ||
} | ||
if (http) { | ||
this.$use_http = true; | ||
} | ||
/** | ||
* The list of loaded API instances | ||
*/ | ||
this.$apis = []; | ||
}; | ||
(function() { | ||
(function constructor() { | ||
/** | ||
* The request instance used to communicate with Bitbucket | ||
*/ | ||
this.$request = null; | ||
/** | ||
* The request instance used to communicate with GitHub | ||
*/ | ||
this.$request = null; | ||
/** | ||
* Authenticate a user for all next requests using an API token | ||
* | ||
* @param {String} login Bitbucket username | ||
* @param {String} token Bitbucket API token | ||
* @return {BitbucketApi} fluent interface | ||
*/ | ||
this.authenticateToken = function authenticateToken(login, token) { | ||
this.getRequest() | ||
.setOption('login_type', 'token') | ||
.setOption('username', login) | ||
.setOption('api_token', token); | ||
/** | ||
* Authenticate a user for all next requests | ||
* | ||
* @param {String} login GitHub username | ||
* @param {String} token GitHub private token | ||
* @return {GitHubApi} fluent interface | ||
*/ | ||
this.authenticate = function(login, token) { | ||
console.log("Deprecated: use 'authenticateToken' instead!"); | ||
return this.authenticateToken(login, token); | ||
}; | ||
return this; | ||
}; | ||
/** | ||
* Authenticate a user for all next requests using an API token | ||
* | ||
* @param {String} login GitHub username | ||
* @param {String} token GitHub API token | ||
* @return {GitHubApi} fluent interface | ||
*/ | ||
this.authenticateToken = function(login, token) | ||
{ | ||
this.getRequest() | ||
.setOption("login_type", "token") | ||
.setOption('username', login) | ||
.setOption('api_token', token); | ||
/** | ||
* Authenticate a user for all next requests using an API token | ||
* | ||
* @param {String} login Bitbucket username | ||
* @param {String} password Bitbucket password | ||
* @return {BitbucketApi} fluent interface | ||
*/ | ||
this.authenticatePassword = function authenticatePassword(login, password) { | ||
this.getRequest() | ||
.setOption('login_type', 'basic') | ||
.setOption('username', login) | ||
.setOption('password', password); | ||
return this; | ||
}; | ||
return this; | ||
}; | ||
/** | ||
* Authenticate a user for all next requests using an API token | ||
* | ||
* @param {String} login GitHub username | ||
* @param {String} password GitHub password | ||
* @return {GitHubApi} fluent interface | ||
*/ | ||
this.authenticatePassword = function(login, password) | ||
{ | ||
this.getRequest() | ||
.setOption("login_type", "basic") | ||
.setOption('username', login) | ||
.setOption('password', password); | ||
/** | ||
* Authenticate a user for all next requests using an API token | ||
* | ||
* @param {OAuth} oauth | ||
* @param {String} accessToken | ||
* @return {BitbucketApi} fluent interface | ||
*/ | ||
this.authenticateOAuth = function authenticateOAuth(oauth, accessToken, accessTokenSecret) { | ||
this.getRequest() | ||
.setOption('login_type', 'oauth') | ||
.setOption('oauth', oauth) | ||
.setOption('oauth_access_token', accessToken) | ||
.setOption('oauth_access_token_secret', accessTokenSecret); | ||
return this; | ||
}; | ||
return this; | ||
}; | ||
/** | ||
* Authenticate a user for all next requests using an API token | ||
* | ||
* @param {OAuth} oauth | ||
* @param {String} accessToken | ||
* @return {GitHubApi} fluent interface | ||
*/ | ||
this.authenticateOAuth = function(oauth, accessToken, accessTokenSecret) | ||
{ | ||
this.getRequest() | ||
.setOption("login_type", "oauth") | ||
.setOption('oauth', oauth) | ||
.setOption('oauth_access_token', accessToken) | ||
.setOption('oauth_access_token_secret', accessTokenSecret); | ||
/** | ||
* Authenticate a user for all next requests using an API token | ||
* | ||
* @param {String} accessToken | ||
* @return {BitbucketApi} fluent interface | ||
*/ | ||
this.authenticateOAuth2 = function authenticateOAuth2(accessToken) { | ||
this.getRequest() | ||
.setOption('login_type', 'oauth2') | ||
.setOption('oauth_access_token', accessToken); | ||
return this; | ||
}; | ||
return this; | ||
}; | ||
/** | ||
* Authenticate a user for all next requests using an API token | ||
* | ||
* @param {String} accessToken | ||
* @return {GitHubApi} fluent interface | ||
*/ | ||
this.authenticateOAuth2 = function(accessToken) | ||
{ | ||
this.getRequest() | ||
.setOption("login_type", "oauth2") | ||
.setOption('oauth_access_token', accessToken); | ||
/** | ||
* Deauthenticate a user for all next requests | ||
* | ||
* @return {BitbucketApi} fluent interface | ||
*/ | ||
this.deAuthenticate = function deAuthenticate() { | ||
this.getRequest() | ||
.setOption('login_type', 'none'); | ||
return this; | ||
}; | ||
return this; | ||
}; | ||
/** | ||
* Deauthenticate a user for all next requests | ||
* | ||
* @return {GitHubApi} fluent interface | ||
*/ | ||
this.deAuthenticate = function() { | ||
this.getRequest() | ||
.setOption("login_type", "none"); | ||
/** | ||
* Call any route, GET method | ||
* Ex: api.get('repos/show/my-username/my-repo') | ||
* | ||
* @param {String} route the Bitbucket route | ||
* @param {Object} parameters GET parameters | ||
* @param {Object} requestOptions reconfigure the request | ||
*/ | ||
this.get = function get(route, parameters, requestOptions, callback) { | ||
return this.getRequest().get(route, parameters || {}, requestOptions, callback); | ||
}; | ||
return this; | ||
}; | ||
/** | ||
* Call any route, DELETE method | ||
* Ex: api.delete('repos/show/my-username/my-repo') | ||
* | ||
* @param {String} route the Bitbucket route | ||
* @param {Object} parameters GET parameters | ||
* @param {Object} requestOptions reconfigure the request | ||
*/ | ||
this['delete'] = function (route, parameters, requestOptions, callback) { // eslint-disable-line | ||
return this.getRequest().send(route, parameters, 'DELETE', requestOptions, callback); | ||
}; | ||
/** | ||
* Call any route, GET method | ||
* Ex: api.get('repos/show/my-username/my-repo') | ||
* | ||
* @param {String} route the GitHub route | ||
* @param {Object} parameters GET parameters | ||
* @param {Object} requestOptions reconfigure the request | ||
*/ | ||
this.get = function(route, parameters, requestOptions, callback) { | ||
return this.getRequest().get(route, parameters || {}, requestOptions, callback); | ||
}; | ||
/** | ||
* Call any route, POST method | ||
* Ex: api.post('repos/show/my-username', {'email': 'my-new-email@provider.org'}) | ||
* | ||
* @param {String} route the Bitbucket route | ||
* @param {Object} parameters POST parameters | ||
* @param {Object} requestOptions reconfigure the request | ||
*/ | ||
this.post = function post(route, parameters, requestOptions, callback) { | ||
return this.getRequest().post(route, parameters || {}, requestOptions, callback); | ||
}; | ||
/** | ||
* Call any route, DELETE method | ||
* Ex: api.delete('repos/show/my-username/my-repo') | ||
* | ||
* @param {String} route the GitHub route | ||
* @param {Object} parameters GET parameters | ||
* @param {Object} requestOptions reconfigure the request | ||
*/ | ||
this["delete"] = function(route, parameters, requestOptions, callback) { | ||
return this.getRequest().send(route, parameters, 'DELETE', requestOptions, callback); | ||
}; | ||
/** | ||
* Get the request | ||
* | ||
* @return {Request} a request instance | ||
*/ | ||
this.getRequest = function getRequest() { | ||
if (!this.request) { | ||
this.request = new Request({ debug: this.$debug, 'proxy_host': this.$proxy_host, 'proxy_port': this.$proxy_port, 'protocol': (this.$use_http ? 'http' : 'https') }); | ||
} | ||
/** | ||
* Call any route, POST method | ||
* Ex: api.post('repos/show/my-username', {'email': 'my-new-email@provider.org'}) | ||
* | ||
* @param {String} route the GitHub route | ||
* @param {Object} parameters POST parameters | ||
* @param {Object} requestOptions reconfigure the request | ||
*/ | ||
this.post = function(route, parameters, requestOptions, callback) { | ||
return this.getRequest().post(route, parameters || {}, requestOptions, callback); | ||
}; | ||
return this.request; | ||
}; | ||
/** | ||
* Get the request | ||
* | ||
* @return {Request} a request instance | ||
*/ | ||
this.getRequest = function() | ||
{ | ||
if(!this.request) { | ||
this.request = new Request({debug: this.$debug, "proxy_host": this.$proxy_host, "proxy_port": this.$proxy_port, "protocol" : this.$use_http? "http": "https"}); | ||
} | ||
this.repositories = new (require('./repositories'))(this); | ||
return this.request; | ||
}; | ||
/** | ||
* Check for whether we can iterate to another page using this.getNextPage(response). | ||
* @param {response} A response that was received from the API. | ||
* @return {boolean} true if the response indicates more pages are available, false otherwise. | ||
*/ | ||
this.hasNextPage = function hasNextPage(response) { | ||
return !!response.next; | ||
}; | ||
/** | ||
* Get the user API | ||
* | ||
* @return {UserApi} the user API | ||
*/ | ||
this.getUserApi = function() | ||
{ | ||
if(!this.$apis.user) { | ||
this.$apis.user = new (require("./user").UserApi)(this); | ||
} | ||
/** | ||
* Check for whether we can iterate to another page using this.getPreviousPage(response). | ||
* @param {response} A response that was received from the API. | ||
* @return {boolean} true if the response indicates a previous pages is available, false otherwise. | ||
*/ | ||
this.hasPreviousPage = function hasPreviousPage(response) { | ||
return !!response.previous; | ||
}; | ||
return this.$apis.user; | ||
}; | ||
/** | ||
* Takes a response and a callback and makes an API request for the response's next page. When the next page | ||
* comes back, the param callback is run on the next-page response. | ||
* NOTE this should only be called guarded behind a check to this.hasNextPage(response) ! | ||
* | ||
* @param {response} A response that was received from the API. | ||
* @param {callback} The callback to run when the response comes back. | ||
*/ | ||
this.getNextPage = function getNextPage(response, callback) { | ||
this.getRequest().doPrebuiltSend(response.next, callback); | ||
}; | ||
/** | ||
* Get the users API | ||
* | ||
* @return {UsersApi} the users API | ||
*/ | ||
this.getUsersApi = function() | ||
{ | ||
if(!this.$apis.users) { | ||
this.$apis.users = new (require("./users").UsersApi)(this); | ||
} | ||
return this.$apis.users; | ||
}; | ||
/** | ||
* Get the repo API | ||
* | ||
* @return {RepoApi} the repo API | ||
*/ | ||
this.getRepoApi = function() | ||
{ | ||
if(!this.$apis.repo) { | ||
this.$apis.repo = new (require("./repo").RepoApi)(this); | ||
} | ||
return this.$apis.repo; | ||
}; | ||
/** | ||
* Get the ssh API | ||
* | ||
* @return {SshApi} the SSH API | ||
*/ | ||
this.getSshApi = function() | ||
{ | ||
if(!this.$apis.ssh) { | ||
this.$apis.ssh = new (require("./ssh").SshApi)(this); | ||
} | ||
return this.$apis.ssh; | ||
}; | ||
/** | ||
* Get the email API | ||
* | ||
* @return {EmailApi} the email API | ||
*/ | ||
this.getEmailApi = function() | ||
{ | ||
if(!this.$apis.email) { | ||
this.$apis.email = new (require("./email").EmailApi)(this); | ||
} | ||
return this.$apis.email; | ||
}; | ||
// /** | ||
// * Get the issue API | ||
// * | ||
// * @return {IssueApi} the issue API | ||
// */ | ||
// this.getIssueApi = function() | ||
// { | ||
// if(!this.$apis['issue']) { | ||
// this.$apis['issue'] = new (require("./github/IssueApi").IssueApi)(this); | ||
// } | ||
// | ||
// return this.$apis['issue']; | ||
// }; | ||
// | ||
// /** | ||
// * Get the pull API | ||
// * | ||
// * @return {PullApi} the pull API | ||
// */ | ||
// this.getPullApi = function() | ||
// { | ||
// if(!this.$apis['pull']) { | ||
// this.$apis['pull'] = new (require("./github/PullApi").PullApi)(this); | ||
// } | ||
// | ||
// return this.$apis['pull']; | ||
// }; | ||
// | ||
// /** | ||
// * Get the object API | ||
// * | ||
// * @return {ObjectApi} the object API | ||
// */ | ||
// this.getObjectApi = function() | ||
// { | ||
// if(!this.$apis['object']) { | ||
// this.$apis['object'] = new (require("./github/ObjectApi").ObjectApi)(this); | ||
// } | ||
// | ||
// return this.$apis['object']; | ||
// }; | ||
// | ||
// /** | ||
// * Get the commit API | ||
// * | ||
// * @return {CommitTest} the commit API | ||
// */ | ||
// this.getCommitApi = function() | ||
// { | ||
// if(!this.$apis['commit']) { | ||
// this.$apis['commit'] = new (require("./github/CommitApi").CommitApi)(this); | ||
// } | ||
// | ||
// return this.$apis['commit']; | ||
// }; | ||
/** | ||
* Takes a response and a callback and makes an API request for the response's previous page. When the previous page | ||
* comes back, the param callback is run on the previous-page response. | ||
* NOTE this should only be called guarded behind a check to this.hasPreviousPage(response) ! | ||
* | ||
* @param {response} A response that was received from the API. | ||
* @param {callback} The callback to run when the response comes back. | ||
*/ | ||
this.getPreviousPage = function getPreviousPage(response, callback) { | ||
this.getRequest().doPrebuiltSend(response.previous, callback); | ||
}; | ||
}).call(BitBucket.prototype); |
@@ -1,279 +0,376 @@ | ||
/** | ||
* Copyright 2010 Ajax.org B.V. | ||
* | ||
* This product includes software developed by | ||
* Ajax.org B.V. (http://www.ajax.org/). | ||
* | ||
* Author: Fabian Jaokbs <fabian@ajax.org> | ||
*/ | ||
const querystring = require('querystring'); | ||
const url = require('url'); | ||
var http = require("http"); | ||
var util = require('util'); | ||
var querystring = require("querystring"); | ||
var crypto = require("crypto"); | ||
/** | ||
* Performs requests on GitHub API. | ||
*/ | ||
var Request = exports.Request = function(options) { | ||
this.configure(options); | ||
const Request = exports.Request = function Request(options) { | ||
this.configure(options); | ||
}; | ||
(function() { | ||
(function construct() { | ||
this.$defaults = { | ||
protocol: 'https', | ||
path: '/2.0', | ||
hostname: 'api.bitbucket.org', | ||
format: 'json', | ||
user_agent: 'js-bitbucket-api-v2 (http://github.com/Mr-Wallet/node-bitbucket-v2)', | ||
http_port: 443, | ||
timeout: 20, | ||
login_type: 'none', | ||
username: null, | ||
password: null, | ||
api_token: null, | ||
oauth_access_token: null, | ||
proxy_host: null, | ||
proxy_port: null, | ||
debug: false | ||
}; | ||
this.$defaults = { | ||
protocol : 'https', | ||
path : '/2.0', | ||
hostname : "api.bitbucket.org", | ||
format : 'json', | ||
user_agent : 'js-bitbucket-api-v2 (http://github.com/Mr-Wallet/node-bitbucket-v2)', | ||
http_port : 443, | ||
timeout : 20, | ||
login_type : "none", | ||
username : null, | ||
password : null, | ||
api_token : null, | ||
oauth_access_token: null, | ||
proxy_host : null, | ||
proxy_port : null, | ||
debug : false | ||
}; | ||
this.configure = function configure(options = {}) { | ||
this.$options = {}; | ||
for (let key in this.$defaults) { | ||
this.$options[key] = options[key] !== undefined ? options[key]: this.$defaults[key]; | ||
} | ||
this.configure = function(options) | ||
{ | ||
options = options || {}; | ||
this.$options = {}; | ||
for (var key in this.$defaults) { | ||
this.$options[key] = options[key] !== undefined ? options[key] : this.$defaults[key]; | ||
return this; | ||
}; | ||
/** | ||
* Change an option value. | ||
* | ||
* @param {String} name The option name | ||
* @param {Object} value The value | ||
* | ||
* @return {Request} The current object instance | ||
*/ | ||
this.setOption = function setOption(name, value) | ||
{ | ||
this.$options[name] = value; | ||
return this; | ||
}; | ||
/** | ||
* Get an option value. | ||
* | ||
* @param string $name The option name | ||
* | ||
* @return mixed The option value | ||
*/ | ||
this.getOption = function getOption(name, defaultValue) { | ||
defaultValue = defaultValue === undefined ? null: defaultValue; | ||
return this.$options[name] ? this.$options[name] : defaultValue; | ||
}; | ||
/** | ||
* Send a GET request | ||
* @see send | ||
*/ | ||
this.get = function get(apiPath, parameters, options, callback) { | ||
return this.send(apiPath, parameters, 'GET', options, callback); | ||
}; | ||
/** | ||
* Send a POST request | ||
* @see send | ||
*/ | ||
this.post = function post(apiPath, parameters, options, callback) { | ||
return this.send(apiPath, parameters, 'POST', options, callback); | ||
}; | ||
/** | ||
* Send a request to the server, receive a response, | ||
* decode the response and returns an associative array | ||
* | ||
* @param {String} apiPath Request API path | ||
* @param {Object} parameters Parameters | ||
* @param {String} httpMethod HTTP method to use | ||
* @param {Object} options reconfigure the request for this call only | ||
*/ | ||
this.send = function send(apiPath, parameters, httpMethod = 'GET', options, callback) { | ||
let initialOptions; | ||
if (options) { | ||
initialOptions = this.$options; | ||
this.configure(options); | ||
} | ||
this.doSend(apiPath, parameters, httpMethod, (err, response) => { | ||
if (err) { | ||
if (callback) { | ||
callback(err); | ||
} | ||
return; | ||
} | ||
return this; | ||
}; | ||
response = this.decodeResponse(response); | ||
if (initialOptions) { | ||
this.options = initialOptions; | ||
} | ||
if (callback) { | ||
callback(null, response); | ||
} | ||
}); | ||
}; | ||
/** | ||
* Change an option value. | ||
* Send a request to the server using a URL received from the API directly, receive a response | ||
* | ||
* @param {String} name The option name | ||
* @param {Object} value The value | ||
* | ||
* @return {Request} The current object instance | ||
* @param {String} $prebuiltURL Request URL given by a previous API call | ||
*/ | ||
this.setOption = function(name, value) | ||
{ | ||
this.$options[name] = value; | ||
return this; | ||
}; | ||
this.doPrebuiltSend = function doPrebuiltSend(prebuiltURL, callback) { | ||
const port = this.$options.proxy_host ? this.$options.proxy_port || 3128 : this.$options.http_port || 443; | ||
/** | ||
* Get an option value. | ||
* | ||
* @param string $name The option name | ||
* | ||
* @return mixed The option value | ||
*/ | ||
this.getOption = function(name, defaultValue) | ||
{ | ||
defaultValue = defaultValue === undefined ? null : defaultValue; | ||
return this.$options[name] ? this.$options[name] : defaultValue; | ||
const headers = { | ||
'Host': 'api.bitbucket.org', | ||
'User-Agent': 'NodeJS HTTP Client', | ||
'Content-Length': '0', | ||
'Content-Type': 'application/x-www-form-urlencoded' | ||
}; | ||
/** | ||
* Send a GET request | ||
* @see send | ||
*/ | ||
this.get = function(apiPath, parameters, options, callback) { | ||
return this.send(apiPath, parameters, 'GET', options, callback); | ||
}; | ||
switch (this.$options.login_type) { | ||
case 'oauth2': | ||
headers.Authorization = 'Bearer ' + this.$options.oauth_access_token; | ||
break; | ||
/** | ||
* Send a POST request | ||
* @see send | ||
*/ | ||
this.post = function(apiPath, parameters, options, callback) { | ||
return this.send(apiPath, parameters, 'POST', options, callback); | ||
case 'token': { | ||
const auth = this.$options.username + '/token:' + this.$options.api_token; | ||
const basic = new Buffer(auth, 'ascii').toString('base64'); | ||
headers.Authorization = 'Basic ' + basic; | ||
break; | ||
} | ||
case 'basic': { | ||
const auth = this.$options.username + ':' + this.$options.password; | ||
const basic = new Buffer(auth, 'ascii').toString('base64'); | ||
headers.Authorization = 'Basic ' + basic; | ||
break; | ||
} | ||
default: | ||
// none | ||
} | ||
const parsedUrl = url.parse(prebuiltURL); | ||
const getOptions = { | ||
headers, | ||
hostname: parsedUrl.hostname, | ||
method: 'GET', | ||
path: parsedUrl.path, | ||
post: port | ||
}; | ||
/** | ||
* Send a request to the server, receive a response, | ||
* decode the response and returns an associative array | ||
* | ||
* @param {String} apiPath Request API path | ||
* @param {Object} parameters Parameters | ||
* @param {String} httpMethod HTTP method to use | ||
* @param {Object} options reconfigure the request for this call only | ||
*/ | ||
this.send = function(apiPath, parameters, httpMethod, options, callback) | ||
{ | ||
httpMethod = httpMethod || "GET"; | ||
if(options) | ||
{ | ||
var initialOptions = this.$options; | ||
this.configure(options); | ||
this.$debug('send prebuilt request: ' + prebuiltURL); | ||
let called = false; | ||
function done(err, body) { | ||
if (called) { | ||
return; | ||
} | ||
called = true; | ||
callback(err, body); | ||
} | ||
const request = require(this.$options.protocol).request(getOptions, (response) => { | ||
response.setEncoding('utf8'); | ||
const body = []; | ||
response.addListener('data', (chunk) => { | ||
body.push(chunk); | ||
}); | ||
response.addListener('end', () => { | ||
let msg = body.join(''); | ||
if (response.statusCode > 204) { | ||
if (response.headers['content-type'].includes('application/json')) { | ||
msg = JSON.parse(body); | ||
} | ||
else { | ||
msg = body; | ||
} | ||
done({ status: response.statusCode, msg }); | ||
return; | ||
} | ||
if (response.statusCode === 204) { | ||
msg = '{}'; | ||
} | ||
done(null, this.decodeResponse(msg)); | ||
}); | ||
var self = this; | ||
this.doSend(apiPath, parameters, httpMethod, function(err, response) { | ||
if (err) { | ||
callback && callback(err); | ||
return; | ||
} | ||
response.addListener('error', (e) => { | ||
done(e); | ||
}); | ||
response = self.decodeResponse(response); | ||
response.addListener('timeout', () => { | ||
done(new Error('Request timed out')); | ||
}); | ||
}); | ||
if (initialOptions) { | ||
self.options = initialOptions; | ||
} | ||
callback && callback(null, response); | ||
}); | ||
request.on('error', (e) => { | ||
done(e); | ||
}); | ||
request.end(); | ||
}; | ||
/** | ||
* Send a request to the server, receive a response | ||
* | ||
* @param {String} $apiPath Request API path | ||
* @param {Object} $parameters Parameters | ||
* @param {String} $httpMethod HTTP method to use | ||
*/ | ||
this.doSend = function doSend(apiPath, parameters, httpMethod, callback) { | ||
httpMethod = httpMethod.toUpperCase(); | ||
let host = this.$options.proxy_host ? this.$options.proxy_host: this.$options.hostname; | ||
let port = this.$options.proxy_host ? this.$options.proxy_port || 3128: this.$options.http_port || 443; | ||
let headers = { | ||
'Host':'api.bitbucket.org', | ||
'User-Agent': 'NodeJS HTTP Client', | ||
'Content-Length': '0', | ||
'Content-Type': 'application/x-www-form-urlencoded' | ||
}; | ||
let getParams = httpMethod != 'POST' ? parameters: {}; | ||
let postParams = httpMethod == 'POST' ? parameters: {}; | ||
/** | ||
* Send a request to the server, receive a response | ||
* | ||
* @param {String} $apiPath Request API path | ||
* @param {Object} $parameters Parameters | ||
* @param {String} $httpMethod HTTP method to use | ||
*/ | ||
this.doSend = function(apiPath, parameters, httpMethod, callback) | ||
{ | ||
httpMethod = httpMethod.toUpperCase(); | ||
var host = this.$options.proxy_host ? this.$options.proxy_host : this.$options.hostname; | ||
var port = this.$options.proxy_host ? this.$options.proxy_port || 3128 : this.$options.http_port || 443; | ||
var headers = { | ||
'Host':'api.bitbucket.org', | ||
"User-Agent": "NodeJS HTTP Client", | ||
"Content-Length": "0", | ||
"Content-Type": "application/x-www-form-urlencoded" | ||
}; | ||
var getParams = httpMethod != "POST" ? parameters : {}; | ||
var postParams = httpMethod == "POST" ? parameters : {}; | ||
let getQuery = querystring.stringify(getParams); | ||
let postQuery = querystring.stringify(postParams); | ||
this.$debug('get: '+ getQuery + ' post ' + postQuery); | ||
let path = this.$options.path + '/' + apiPath.replace(/\/*$/, ''); | ||
if (getQuery) | ||
path += '?' + getQuery; | ||
var getQuery = querystring.stringify(getParams); | ||
var postQuery = querystring.stringify(postParams); | ||
this.$debug("get: "+ getQuery + " post " + postQuery); | ||
if (postQuery) | ||
headers['Content-Length'] = postQuery.length; | ||
var path = this.$options.path + "/" + apiPath.replace(/\/*$/, ""); | ||
if (getQuery) | ||
path += "?" + getQuery; | ||
switch(this.$options.login_type) { | ||
case 'oauth': | ||
// TODO this should use oauth.authHeader once they add the missing argument | ||
let oauth = this.$options.oauth; | ||
let orderedParameters = oauth._prepareParameters( | ||
this.$options.oauth_access_token, | ||
this.$options.oauth_access_token_secret, | ||
httpMethod, | ||
'https://api.bitbucket.org' + path, | ||
postParams || {} | ||
); | ||
headers.Authorization = oauth._buildAuthorizationHeaders(orderedParameters); | ||
break; | ||
if (postQuery) | ||
headers["Content-Length"] = postQuery.length; | ||
case 'oauth2': | ||
headers.Authorization = 'Bearer ' + this.$options.oauth_access_token; | ||
break; | ||
switch(this.$options.login_type) { | ||
case "oauth": | ||
// TODO this should use oauth.authHeader once they add the missing argument | ||
var oauth = this.$options.oauth; | ||
var orderedParameters= oauth._prepareParameters( | ||
this.$options.oauth_access_token, | ||
this.$options.oauth_access_token_secret, | ||
httpMethod, | ||
"https://api.bitbucket.org" + path, | ||
postParams || {} | ||
); | ||
headers.Authorization = oauth._buildAuthorizationHeaders(orderedParameters); | ||
break; | ||
case 'token': { | ||
const auth = this.$options.username + '/token:' + this.$options.api_token; | ||
const basic = new Buffer(auth, 'ascii').toString('base64'); | ||
headers.Authorization = 'Basic ' + basic; | ||
break; | ||
} | ||
case "oauth2": | ||
headers.Authorization = 'Bearer ' + this.$options.oauth_access_token; | ||
break; | ||
case 'basic': { | ||
const auth = this.$options.username + ':' + this.$options.password; | ||
const basic = new Buffer(auth, 'ascii').toString('base64'); | ||
headers.Authorization = 'Basic ' + basic; | ||
break; | ||
} | ||
case "token": | ||
var auth = this.$options['username'] + "/token:" + this.$options['api_token']; | ||
var basic = new Buffer(auth, "ascii").toString("base64"); | ||
headers.Authorization = "Basic " + basic; | ||
break; | ||
default: | ||
// none | ||
} | ||
case "basic": | ||
var auth = this.$options['username'] + ":" + this.$options['password']; | ||
var basic = new Buffer(auth, "ascii").toString("base64"); | ||
headers.Authorization = "Basic " + basic; | ||
break; | ||
const getOptions = { | ||
host, | ||
post: port, | ||
path, | ||
method: httpMethod, | ||
headers | ||
}; | ||
default: | ||
// none | ||
} | ||
let called = false; | ||
function done(err, body) { | ||
if (called) { | ||
return; | ||
} | ||
var getOptions = { | ||
host: host, | ||
post: port, | ||
path: path, | ||
method: httpMethod, | ||
headers: headers | ||
}; | ||
called = true; | ||
callback(err, body); | ||
} | ||
this.$debug('send ' + httpMethod + ' request: ' + path); | ||
var request = require(this.$options.protocol).request(getOptions, function(response) { | ||
response.setEncoding('utf8'); | ||
this.$debug('send ' + httpMethod + ' request: ' + path); | ||
const request = require(this.$options.protocol).request(getOptions, (response) => { | ||
response.setEncoding('utf8'); | ||
var body = []; | ||
response.addListener('data', function (chunk) { | ||
body.push(chunk); | ||
}); | ||
response.addListener('end', function () { | ||
var msg; | ||
body = body.join(""); | ||
let body = []; | ||
response.addListener('data', (chunk) => { | ||
body.push(chunk); | ||
}); | ||
response.addListener('end', () => { | ||
let msg; | ||
body = body.join(''); | ||
if (response.statusCode > 204) { | ||
if (response.headers["content-type"].indexOf("application/json") === 0) { | ||
msg = JSON.parse(body); | ||
} else { | ||
msg = body; | ||
} | ||
done({status: response.statusCode, msg: msg}); | ||
return; | ||
} | ||
if (response.statusCode == 204) | ||
body = "{}"; | ||
if (response.statusCode > 204) { | ||
if (response.headers['content-type'].includes('application/json')) { | ||
msg = JSON.parse(body); | ||
} | ||
else { | ||
msg = body; | ||
} | ||
done({ status: response.statusCode, msg }); | ||
return; | ||
} | ||
if (response.statusCode === 204) { | ||
body = '{}'; | ||
} | ||
done(null, body); | ||
}); | ||
done(null, body); | ||
}); | ||
response.addListener("error", function(e) { | ||
done(e); | ||
}); | ||
response.addListener('error', (e) => { | ||
done(e); | ||
}); | ||
response.addListener("timeout", function() { | ||
done(new Error("Request timed out")); | ||
}); | ||
}); | ||
response.addListener('timeout', () => { | ||
done(new Error('Request timed out')); | ||
}); | ||
}); | ||
request.on("error", function(e) { | ||
done(e); | ||
}); | ||
request.on('error', (e) => { | ||
done(e); | ||
}); | ||
if (httpMethod == "POST") | ||
request.write(postQuery); | ||
if (httpMethod === 'POST') { | ||
request.write(postQuery); | ||
} | ||
request.end(); | ||
var called = false; | ||
function done(err, body) { | ||
if (called) | ||
return; | ||
request.end(); | ||
}; | ||
called = true; | ||
callback(err, body); | ||
} | ||
}; | ||
/** | ||
* Get a JSON response and transform to JSON | ||
*/ | ||
this.decodeResponse = function(response) | ||
this.decodeResponse = function decodeResponse(response) | ||
{ | ||
if(this.$options.format === "text") { | ||
return response; | ||
} | ||
else if(this.$options.format === "json") { | ||
return JSON.parse(response); | ||
} | ||
if (this.$options.format === 'text') { | ||
return response; | ||
} | ||
else if (this.$options.format === 'json') { | ||
return JSON.parse(response); | ||
} | ||
}; | ||
this.$debug = function(msg) { | ||
if (this.$options.debug) | ||
console.log(msg); | ||
}; | ||
this.$debug = function $debug(msg) { | ||
if (this.$options.debug) { | ||
console.log(msg); | ||
} | ||
}; | ||
}).call(Request.prototype); |
{ | ||
"name" : "bitbucket-v2", | ||
"version" : "0.0.4", | ||
"version" : "0.0.5", | ||
"description" : "Wrapper for the BitBucket API v2", | ||
"author": "Jordan Wallet <jjwallet@gmail.com>", | ||
"homepage": "https://github.com/Mr-Wallet/node-bitbucket-v2.git", | ||
"main" : "./bitbucket", | ||
"main" : "dist/index.js", | ||
"scripts": { | ||
"compile": "babel --presets es2015 -d ./dist ./bitbucket", | ||
"lint": "eslint ./bitbucket --quiet", | ||
"prepublish": "npm run compile" | ||
}, | ||
"repository" : { | ||
"type" : "git", | ||
"url" : "http://github.com/ajaxorg/node-bitbucket.git" | ||
"url" : "http://github.com/Mr-Wallet/node-bitbucket-v2.git" | ||
}, | ||
@@ -17,2 +22,9 @@ "engine" : ["node >=5.5.0"], | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.2.0", | ||
"babel-preset-es2015": "^6.1.18", | ||
"eslint": "^1.10.3", | ||
"eslint-config-airbnb": "^2.0.0", | ||
"eslint-plugin-react": "^3.11.2" | ||
}, | ||
"licenses": [{ | ||
@@ -19,0 +31,0 @@ "type": "The MIT License", |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
52363
1376
1
5
4