bitbucket-v2
Advanced tools
Comparing version 0.0.5 to 0.1.0
@@ -9,94 +9,32 @@ const Request = require('./request').Request; | ||
const BitBucket = exports.BitBucket = function BitBucket(debug, proxy, http) { | ||
/** | ||
* Use debug mode (prints debug messages) | ||
*/ | ||
this.$debug = debug; | ||
class Bitbucket { | ||
constructor(proxy) { | ||
/** | ||
* Define HTTP proxy in format localhost:3128 | ||
*/ | ||
if (proxy) { | ||
this.$proxy_host = proxy.split(':')[0]; | ||
this.$proxy_port = proxy.split(':')[1]; | ||
} | ||
/** | ||
* Define HTTP proxy in format localhost:3128 | ||
*/ | ||
if (proxy) { | ||
this.$proxy_host = proxy.split(':')[0]; | ||
this.$proxy_port = proxy.split(':')[1]; | ||
this.request = new Request({ 'proxy_host': this.$proxy_host, 'proxy_port': this.$proxy_port }); | ||
this.repositories = new (require('./repositories'))(this); | ||
} | ||
if (http) { | ||
this.$use_http = true; | ||
} | ||
/** | ||
* The list of loaded API instances | ||
*/ | ||
this.$apis = []; | ||
}; | ||
(function constructor() { | ||
/** | ||
* The request instance used to communicate with Bitbucket | ||
*/ | ||
this.$request = null; | ||
/** | ||
* Authenticate a user for all next requests using an API token | ||
* | ||
* @param {String} accessToken | ||
* @return {BitbucketApi} fluent interface | ||
*/ | ||
authenticateOAuth2(accessToken) { | ||
this.request | ||
.setOption('login_type', 'oauth2') | ||
.setOption('oauth_access_token', accessToken); | ||
/** | ||
* 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); | ||
return this; | ||
} | ||
return this; | ||
}; | ||
/** | ||
* 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; | ||
}; | ||
/** | ||
* 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; | ||
}; | ||
/** | ||
* 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; | ||
}; | ||
/** | ||
* Deauthenticate a user for all next requests | ||
@@ -106,8 +44,8 @@ * | ||
*/ | ||
this.deAuthenticate = function deAuthenticate() { | ||
this.getRequest() | ||
deAuthenticate() { | ||
this.request | ||
.setOption('login_type', 'none'); | ||
return this; | ||
}; | ||
} | ||
@@ -122,5 +60,5 @@ /** | ||
*/ | ||
this.get = function get(route, parameters, requestOptions, callback) { | ||
return this.getRequest().get(route, parameters || {}, requestOptions, callback); | ||
}; | ||
get(route, parameters, requestOptions, callback) { | ||
return this.request.get(route, parameters || {}, requestOptions, callback); | ||
} | ||
@@ -135,5 +73,5 @@ /** | ||
*/ | ||
this['delete'] = function (route, parameters, requestOptions, callback) { // eslint-disable-line | ||
return this.getRequest().send(route, parameters, 'DELETE', requestOptions, callback); | ||
}; | ||
delete(route, parameters, requestOptions, callback) { // eslint-disable-line | ||
return this.request.send(route, parameters, 'DELETE', requestOptions, callback); | ||
} | ||
@@ -148,22 +86,7 @@ /** | ||
*/ | ||
this.post = function post(route, parameters, requestOptions, callback) { | ||
return this.getRequest().post(route, parameters || {}, requestOptions, callback); | ||
}; | ||
post(route, parameters, requestOptions, callback) { | ||
return this.request.post(route, parameters || {}, 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') }); | ||
} | ||
return this.request; | ||
}; | ||
this.repositories = new (require('./repositories'))(this); | ||
/** | ||
* Check for whether we can iterate to another page using this.getNextPage(response). | ||
@@ -173,6 +96,10 @@ * @param {response} A response that was received from the API. | ||
*/ | ||
this.hasNextPage = function hasNextPage(response) { | ||
static hasNextPage(response) { | ||
return !!response.next; | ||
}; | ||
} | ||
hasNextPage(response) { | ||
return Bitbucket.hasNextPage(response); | ||
} | ||
/** | ||
@@ -183,6 +110,10 @@ * Check for whether we can iterate to another page using this.getPreviousPage(response). | ||
*/ | ||
this.hasPreviousPage = function hasPreviousPage(response) { | ||
static hasPreviousPage(response) { | ||
return !!response.previous; | ||
}; | ||
} | ||
hasPreviousPage(response) { | ||
return Bitbucket.hasPreviousPage(response); | ||
} | ||
/** | ||
@@ -196,5 +127,5 @@ * Takes a response and a callback and makes an API request for the response's next page. When the next page | ||
*/ | ||
this.getNextPage = function getNextPage(response, callback) { | ||
this.getRequest().doPrebuiltSend(response.next, callback); | ||
}; | ||
getNextPage(response, callback) { | ||
this.request.doPrebuiltSend(response.next, callback); | ||
} | ||
@@ -209,5 +140,7 @@ /** | ||
*/ | ||
this.getPreviousPage = function getPreviousPage(response, callback) { | ||
this.getRequest().doPrebuiltSend(response.previous, callback); | ||
}; | ||
}).call(BitBucket.prototype); | ||
getPreviousPage(response, callback) { | ||
this.request.doPrebuiltSend(response.previous, callback); | ||
} | ||
} | ||
module.exports = Bitbucket; |
const querystring = require('querystring'); | ||
const https = require('https'); | ||
const url = require('url'); | ||
@@ -26,4 +27,3 @@ | ||
proxy_host: null, | ||
proxy_port: null, | ||
debug: false | ||
proxy_port: null | ||
}; | ||
@@ -34,3 +34,3 @@ | ||
for (let key in this.$defaults) { | ||
this.$options[key] = options[key] !== undefined ? options[key]: this.$defaults[key]; | ||
this.$options[key] = options[key] !== undefined ? options[key] : this.$defaults[key]; | ||
} | ||
@@ -49,4 +49,3 @@ | ||
*/ | ||
this.setOption = function setOption(name, value) | ||
{ | ||
this.setOption = function setOption(name, value) { | ||
this.$options[name] = value; | ||
@@ -132,28 +131,6 @@ return this; | ||
'Content-Length': '0', | ||
'Content-Type': 'application/x-www-form-urlencoded' | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
'Authorization': 'Bearer ' + this.$options.oauth_access_token | ||
}; | ||
switch (this.$options.login_type) { | ||
case 'oauth2': | ||
headers.Authorization = 'Bearer ' + this.$options.oauth_access_token; | ||
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 '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); | ||
@@ -169,4 +146,2 @@ | ||
this.$debug('send prebuilt request: ' + prebuiltURL); | ||
let called = false; | ||
@@ -182,3 +157,3 @@ function done(err, body) { | ||
const request = require(this.$options.protocol).request(getOptions, (response) => { | ||
const request = https.request(getOptions, (response) => { | ||
response.setEncoding('utf8'); | ||
@@ -235,60 +210,26 @@ | ||
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; | ||
const host = this.$options.proxy_host ? this.$options.proxy_host: this.$options.hostname; | ||
const port = this.$options.proxy_host ? this.$options.proxy_port || 3128: this.$options.http_port || 443; | ||
let headers = { | ||
'Host':'api.bitbucket.org', | ||
const headers = { | ||
'Host': 'api.bitbucket.org', | ||
'User-Agent': 'NodeJS HTTP Client', | ||
'Content-Length': '0', | ||
'Content-Type': 'application/x-www-form-urlencoded' | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
'Authorization': 'Bearer ' + this.$options.oauth_access_token | ||
}; | ||
let getParams = httpMethod != 'POST' ? parameters: {}; | ||
let postParams = httpMethod == 'POST' ? parameters: {}; | ||
const getParams = httpMethod !== 'POST' ? parameters : {}; | ||
const postParams = httpMethod === 'POST' ? parameters : {}; | ||
let getQuery = querystring.stringify(getParams); | ||
let postQuery = querystring.stringify(postParams); | ||
this.$debug('get: '+ getQuery + ' post ' + postQuery); | ||
const getQuery = querystring.stringify(getParams); | ||
const postQuery = querystring.stringify(postParams); | ||
let path = this.$options.path + '/' + apiPath.replace(/\/*$/, ''); | ||
if (getQuery) | ||
if (getQuery) { | ||
path += '?' + getQuery; | ||
} | ||
if (postQuery) | ||
if (postQuery) { | ||
headers['Content-Length'] = postQuery.length; | ||
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; | ||
case 'oauth2': | ||
headers.Authorization = 'Bearer ' + this.$options.oauth_access_token; | ||
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 'basic': { | ||
const auth = this.$options.username + ':' + this.$options.password; | ||
const basic = new Buffer(auth, 'ascii').toString('base64'); | ||
headers.Authorization = 'Basic ' + basic; | ||
break; | ||
} | ||
default: | ||
// none | ||
} | ||
@@ -314,4 +255,3 @@ | ||
this.$debug('send ' + httpMethod + ' request: ' + path); | ||
const request = require(this.$options.protocol).request(getOptions, (response) => { | ||
const request = https.request(getOptions, (response) => { | ||
response.setEncoding('utf8'); | ||
@@ -365,21 +305,13 @@ | ||
/** | ||
* Get a JSON response and transform to JSON | ||
*/ | ||
this.decodeResponse = function decodeResponse(response) | ||
{ | ||
if (this.$options.format === 'text') { | ||
return response; | ||
} | ||
else if (this.$options.format === 'json') { | ||
return JSON.parse(response); | ||
} | ||
}; | ||
this.$debug = function $debug(msg) { | ||
if (this.$options.debug) { | ||
console.log(msg); | ||
/** | ||
* Get a JSON response and transform to JSON | ||
*/ | ||
this.decodeResponse = function decodeResponse(response) { | ||
if (this.$options.format === 'text') { | ||
return response; | ||
} | ||
else if (this.$options.format === 'json') { | ||
return JSON.parse(response); | ||
} | ||
}; | ||
}).call(Request.prototype); |
'use strict'; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var Request = require('./request').Request; | ||
@@ -11,185 +15,162 @@ | ||
var BitBucket = exports.BitBucket = function BitBucket(debug, proxy, http) { | ||
/** | ||
* Use debug mode (prints debug messages) | ||
*/ | ||
this.$debug = debug; | ||
var Bitbucket = function () { | ||
function Bitbucket(proxy) { | ||
_classCallCheck(this, Bitbucket); | ||
/** | ||
* Define HTTP proxy in format localhost:3128 | ||
*/ | ||
if (proxy) { | ||
this.$proxy_host = proxy.split(':')[0]; | ||
this.$proxy_port = proxy.split(':')[1]; | ||
/** | ||
* Define HTTP proxy in format localhost:3128 | ||
*/ | ||
if (proxy) { | ||
this.$proxy_host = proxy.split(':')[0]; | ||
this.$proxy_port = proxy.split(':')[1]; | ||
} | ||
this.request = new Request({ 'proxy_host': this.$proxy_host, 'proxy_port': this.$proxy_port }); | ||
this.repositories = new (require('./repositories'))(this); | ||
} | ||
if (http) { | ||
this.$use_http = true; | ||
} | ||
/** | ||
* The list of loaded API instances | ||
*/ | ||
this.$apis = []; | ||
}; | ||
(function constructor() { | ||
/** | ||
* The request instance used to communicate with Bitbucket | ||
*/ | ||
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 | ||
* @param {String} accessToken | ||
* @return {BitbucketApi} fluent interface | ||
*/ | ||
this.authenticateToken = function authenticateToken(login, token) { | ||
this.getRequest().setOption('login_type', 'token').setOption('username', login).setOption('api_token', token); | ||
return this; | ||
}; | ||
_createClass(Bitbucket, [{ | ||
key: 'authenticateOAuth2', | ||
value: function authenticateOAuth2(accessToken) { | ||
this.request.setOption('login_type', 'oauth2').setOption('oauth_access_token', accessToken); | ||
/** | ||
* 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; | ||
}; | ||
/** | ||
* Deauthenticate a user for all next requests | ||
* | ||
* @return {BitbucketApi} fluent interface | ||
*/ | ||
/** | ||
* 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); | ||
}, { | ||
key: 'deAuthenticate', | ||
value: function deAuthenticate() { | ||
this.request.setOption('login_type', 'none'); | ||
return this; | ||
}; | ||
return this; | ||
} | ||
/** | ||
* 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); | ||
/** | ||
* 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 | ||
*/ | ||
return this; | ||
}; | ||
}, { | ||
key: 'get', | ||
value: function get(route, parameters, requestOptions, callback) { | ||
return this.request.get(route, parameters || {}, requestOptions, callback); | ||
} | ||
/** | ||
* Deauthenticate a user for all next requests | ||
* | ||
* @return {BitbucketApi} fluent interface | ||
*/ | ||
this.deAuthenticate = function deAuthenticate() { | ||
this.getRequest().setOption('login_type', 'none'); | ||
/** | ||
* 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 | ||
*/ | ||
return this; | ||
}; | ||
}, { | ||
key: 'delete', | ||
value: function _delete(route, parameters, requestOptions, callback) { | ||
// eslint-disable-line | ||
return this.request.send(route, parameters, 'DELETE', requestOptions, callback); | ||
} | ||
/** | ||
* 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); | ||
}; | ||
/** | ||
* 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 | ||
*/ | ||
/** | ||
* 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); | ||
}; | ||
}, { | ||
key: 'post', | ||
value: function post(route, parameters, requestOptions, callback) { | ||
return this.request.post(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); | ||
}; | ||
/** | ||
* 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. | ||
*/ | ||
/** | ||
* 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' }); | ||
}, { | ||
key: 'hasNextPage', | ||
value: function hasNextPage(response) { | ||
return Bitbucket.hasNextPage(response); | ||
} | ||
return this.request; | ||
}; | ||
/** | ||
* 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.repositories = new (require('./repositories'))(this); | ||
}, { | ||
key: 'hasPreviousPage', | ||
value: function hasPreviousPage(response) { | ||
return Bitbucket.hasPreviousPage(response); | ||
} | ||
/** | ||
* 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; | ||
}; | ||
/** | ||
* 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. | ||
*/ | ||
/** | ||
* 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; | ||
}; | ||
}, { | ||
key: 'getNextPage', | ||
value: function getNextPage(response, callback) { | ||
this.request.doPrebuiltSend(response.next, callback); | ||
} | ||
/** | ||
* 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); | ||
}; | ||
/** | ||
* 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. | ||
*/ | ||
/** | ||
* 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); | ||
}, { | ||
key: 'getPreviousPage', | ||
value: function getPreviousPage(response, callback) { | ||
this.request.doPrebuiltSend(response.previous, callback); | ||
} | ||
}], [{ | ||
key: 'hasNextPage', | ||
value: function hasNextPage(response) { | ||
return !!response.next; | ||
} | ||
}, { | ||
key: 'hasPreviousPage', | ||
value: function hasPreviousPage(response) { | ||
return !!response.previous; | ||
} | ||
}]); | ||
return Bitbucket; | ||
}(); | ||
module.exports = Bitbucket; |
'use strict'; | ||
var querystring = require('querystring'); | ||
var https = require('https'); | ||
var url = require('url'); | ||
@@ -28,4 +29,3 @@ | ||
proxy_host: null, | ||
proxy_port: null, | ||
debug: false | ||
proxy_port: null | ||
}; | ||
@@ -141,30 +141,6 @@ | ||
'Content-Length': '0', | ||
'Content-Type': 'application/x-www-form-urlencoded' | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
'Authorization': 'Bearer ' + this.$options.oauth_access_token | ||
}; | ||
switch (this.$options.login_type) { | ||
case 'oauth2': | ||
headers.Authorization = 'Bearer ' + this.$options.oauth_access_token; | ||
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; | ||
} | ||
case 'basic': | ||
{ | ||
var auth = this.$options.username + ':' + this.$options.password; | ||
var basic = new Buffer(auth, 'ascii').toString('base64'); | ||
headers.Authorization = 'Basic ' + basic; | ||
break; | ||
} | ||
default: | ||
// none | ||
} | ||
var parsedUrl = url.parse(prebuiltURL); | ||
@@ -180,4 +156,2 @@ | ||
this.$debug('send prebuilt request: ' + prebuiltURL); | ||
var called = false; | ||
@@ -193,3 +167,3 @@ function done(err, body) { | ||
var request = require(this.$options.protocol).request(getOptions, function (response) { | ||
var request = https.request(getOptions, function (response) { | ||
response.setEncoding('utf8'); | ||
@@ -252,46 +226,18 @@ | ||
'Content-Length': '0', | ||
'Content-Type': 'application/x-www-form-urlencoded' | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
'Authorization': 'Bearer ' + this.$options.oauth_access_token | ||
}; | ||
var getParams = httpMethod != 'POST' ? parameters : {}; | ||
var postParams = httpMethod == 'POST' ? parameters : {}; | ||
var getParams = httpMethod !== 'POST' ? parameters : {}; | ||
var postParams = httpMethod === 'POST' ? parameters : {}; | ||
var getQuery = querystring.stringify(getParams); | ||
var postQuery = querystring.stringify(postParams); | ||
this.$debug('get: ' + getQuery + ' post ' + postQuery); | ||
var path = this.$options.path + '/' + apiPath.replace(/\/*$/, ''); | ||
if (getQuery) path += '?' + getQuery; | ||
if (getQuery) { | ||
path += '?' + getQuery; | ||
} | ||
if (postQuery) headers['Content-Length'] = postQuery.length; | ||
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 'oauth2': | ||
headers.Authorization = 'Bearer ' + this.$options.oauth_access_token; | ||
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; | ||
} | ||
case 'basic': | ||
{ | ||
var auth = this.$options.username + ':' + this.$options.password; | ||
var basic = new Buffer(auth, 'ascii').toString('base64'); | ||
headers.Authorization = 'Basic ' + basic; | ||
break; | ||
} | ||
default: | ||
// none | ||
if (postQuery) { | ||
headers['Content-Length'] = postQuery.length; | ||
} | ||
@@ -317,4 +263,3 @@ | ||
this.$debug('send ' + httpMethod + ' request: ' + path); | ||
var request = require(this.$options.protocol).request(getOptions, function (response) { | ||
var request = https.request(getOptions, function (response) { | ||
response.setEncoding('utf8'); | ||
@@ -376,8 +321,2 @@ | ||
}; | ||
this.$debug = function $debug(msg) { | ||
if (this.$options.debug) { | ||
console.log(msg); | ||
} | ||
}; | ||
}).call(Request.prototype); |
{ | ||
"name" : "bitbucket-v2", | ||
"version" : "0.0.5", | ||
"version" : "0.1.0", | ||
"description" : "Wrapper for the BitBucket API v2", | ||
@@ -5,0 +5,0 @@ "author": "Jordan Wallet <jjwallet@gmail.com>", |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
0
45345
1186
2