bitbucket-v2
Advanced tools
Comparing version 0.5.2 to 0.6.0
@@ -0,0 +0,0 @@ module.exports = { |
@@ -15,3 +15,3 @@ const constants = require('./constants'); | ||
module.exports = function Bitbucket({ proxy, useXhr } = {}) { | ||
module.exports = function Bitbucket({ proxy, requesterFn } = {}) { | ||
/** | ||
@@ -34,3 +34,7 @@ * Define HTTP proxy in format localhost:3128 | ||
apiModel.repositories = buildRepositories(apiModel); | ||
apiModel.request = buildRequest({ proxy_host: $proxy_host, proxy_port: $proxy_port, use_xhr: useXhr }); | ||
apiModel.request = buildRequest({ | ||
proxy_host: $proxy_host, | ||
proxy_port: $proxy_port, | ||
requester_fn: requesterFn | ||
}); | ||
apiModel.user = buildUser(apiModel); | ||
@@ -37,0 +41,0 @@ apiModel.workspaces = buildWorkspaces(apiModel); |
@@ -5,3 +5,2 @@ const _ = require('lodash'); | ||
const url = require('url'); | ||
const xhr = require('xhr'); | ||
@@ -27,3 +26,3 @@ /** | ||
proxy_port: null, | ||
use_xhr: false | ||
requester_fn: null | ||
}; | ||
@@ -95,11 +94,11 @@ const $options = _.defaults({}, _options, $defaults); | ||
if ($options.use_xhr) { | ||
const xhrOptions = { | ||
if ($options.requester_fn) { | ||
const requesterOptions = { | ||
headers, | ||
json: true, | ||
timeout: $options.timeout * 1000, | ||
method: 'GET', | ||
url: prebuiltURL | ||
}; | ||
return result.sendXhrRequest(xhrOptions); | ||
return $options.requester_fn(requesterOptions); | ||
} | ||
@@ -113,3 +112,3 @@ | ||
path, | ||
post: port | ||
port | ||
}; | ||
@@ -133,28 +132,27 @@ | ||
let query; | ||
let path = options.path + '/' + apiPath.replace(/\/*$/, ''); // eslint-disable-line prefer-template | ||
const path = options.path + '/' + apiPath.replace(/\/*$/, ''); // eslint-disable-line prefer-template | ||
if (method === 'POST') { | ||
query = JSON.stringify(parameters); | ||
headers['Content-Type'] = 'application/json'; | ||
if (!options.use_xhr) { | ||
headers['Content-Length'] = query.length; | ||
} | ||
headers['Content-Length'] = query.length; | ||
} | ||
else { | ||
query = querystring.stringify(parameters); | ||
path += `?${query}`; | ||
} | ||
if (options.use_xhr) { | ||
const xhrOptions = { | ||
if (options.requester_fn) { | ||
const requesterOptions = { | ||
headers, | ||
json: true, | ||
hostname, | ||
method, | ||
timeout: options.timeout * 1000, | ||
url: `https://${hostname}${path}` | ||
path, | ||
query, | ||
url: `https://${hostname}${path}?${query}` | ||
}; | ||
if (method === 'POST') { | ||
xhrOptions.json = parameters; | ||
requesterOptions.body = parameters; | ||
} | ||
return result.sendXhrRequest(xhrOptions); | ||
return options.requester_fn(requesterOptions); | ||
} | ||
@@ -166,4 +164,4 @@ | ||
method, | ||
path, | ||
post: port | ||
path: `${path}?${query}`, | ||
port | ||
}; | ||
@@ -195,18 +193,15 @@ | ||
proxy_port: proxyPort, | ||
use_xhr: useXhr | ||
requester_fn | ||
} = options; | ||
const hostname = !useXhr && proxyHost ? proxyHost : _hostname; | ||
const port = !useXhr && proxyHost ? proxyPort || 3128 : httpPort || 443; | ||
const hostname = !requester_fn && proxyHost ? proxyHost : _hostname; | ||
const port = !requester_fn && proxyHost ? proxyPort || 3128 : httpPort || 443; | ||
const headers = { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
Authorization: `Bearer ${oauthAccessToken}` | ||
Authorization: `Bearer ${oauthAccessToken}`, | ||
Host: 'api.bitbucket.org', | ||
'User-Agent': 'NodeJS HTTP Client', | ||
'Content-Length': '0' | ||
}; | ||
if (!useXhr) { | ||
headers['Host'] = 'api.bitbucket.org'; // eslint-disable-line dot-notation | ||
headers['User-Agent'] = 'NodeJS HTTP Client'; | ||
headers['Content-Length'] = '0'; | ||
} | ||
return { headers, hostname, port }; | ||
@@ -266,29 +261,4 @@ }, | ||
return resultPromise; | ||
}, | ||
sendXhrRequest(xhrOptions) { | ||
let resolve; | ||
let reject; | ||
const resultPromise = new Promise((_resolve, _reject) => { | ||
resolve = _resolve; | ||
reject = _reject; | ||
}); | ||
xhr(xhrOptions, (error, response) => { | ||
if (error) { | ||
reject(error); | ||
return; | ||
} | ||
if (response.statusCode >= 400) { | ||
reject(response); | ||
return; | ||
} | ||
resolve(response); | ||
}); | ||
return resultPromise; | ||
} | ||
}); | ||
}; |
# CHANGLOG | ||
## 0.6.0 | ||
Thanks to [@andrewyalung](https://github.com/andrewyalung/) for authoring most of this version! | ||
- `requesterFn` has been added as an option. When `requesterFn` is provided, all request will be processed by that function. | ||
- `useXhr` has been removed and replaced with `requesterFn`. | ||
- Fixed non-default `port` being ignored in default request implementation. | ||
## 0.5.2 | ||
@@ -7,3 +13,2 @@ - Functions that take an API response as an argument now properly handle taking the _entire_ response, as they did in `0.4.x`. They _also_ still accept just the response's `body` property, as in `0.5.1`. Affected functions: `hasNextPage`, `hasPreviousPage`, `getNextPage`, `getPreviousPage`, `repositories.getForksFromResponse`, `repositories.hasParent`, `repositories.getParentFromResponse`. | ||
## 0.5.1 | ||
This version is targeted at two main goals: | ||
@@ -10,0 +15,0 @@ 1. Bitbucket's _breaking_ API changes (migrating from users and teams to "workspaces"), in order to comply with GDPR. For more details, see: https://developer.atlassian.com/cloud/bitbucket/bitbucket-api-changes-workspaces/ |
{ | ||
"name": "bitbucket-v2", | ||
"version": "0.5.2", | ||
"description": "Wrapper for the BitBucket API v2, the version required to use OAuth2. Includes support for XHR requests.", | ||
"version": "0.6.0", | ||
"description": "Wrapper for the BitBucket API v2, the version required to use OAuth2. Includes support for custom HTTP clients.", | ||
"author": "Jordan Wallet <jjwallet@gmail.com>", | ||
@@ -20,4 +20,3 @@ "homepage": "https://github.com/Mr-Wallet/node-bitbucket-v2.git", | ||
"lodash": "^4.17.15", | ||
"oauth": "~0.9.14", | ||
"xhr": "^2.2.0" | ||
"oauth": "~0.9.14" | ||
}, | ||
@@ -24,0 +23,0 @@ "devDependencies": { |
@@ -19,5 +19,21 @@ # node-bitbucket-v2 | ||
It is not necessary to provide any options at all (`Bitbucket` can be constructed with no argument). | ||
- `useXhr` (`Boolean`): If `true`, requests will be made using XMLHttpRequest. This is only available in a web browser, and will fail otherwise. This can be very useful in Electron for automatically resolving proxies and custom SSL certificates. | ||
- `proxy` (`String`): Defines a proxy to make requests against, instead of `api.bitbucket.org:443`. This option is _ignored_ when `useXhr` is active. | ||
- `requesterFn` (`(options) => Promise<any>`): If provided, requests will be made using the function you provide. This is allows you to use your preferred http client. The `options` provided are `{ headers, hostname, method, path, query, url, body? }`. `body` is only provided on `POST` methods. In the case of `getNextPage`, `getPreviousPage`, `getForksFromResponse` and `getParentFromResponse`, only `{ headers, method, url }` are provided in the options. Example: | ||
``` | ||
const axios = require('axios'); | ||
const Bitbucket = require('node-bitbucket-v2'); | ||
const requesterFn = (options) => { | ||
const { url, method, body } = options; | ||
if (method === 'POST') { | ||
return axios.post(url, body); | ||
} | ||
return axios.get(url); | ||
}; | ||
const bitbucketApi = new Bitbucket({ requesterFn }); | ||
``` | ||
- `proxy` (`String`): Defines a proxy to make requests against, instead of `api.bitbucket.org:443`. This option is _ignored_ when `requesterFn` is provided. | ||
For implemented methods, check `bitbucket/repositories.js` and `bitbucket/user.js`. |
26877
2
39
586
- Removedxhr@^2.2.0
- Removeddom-walk@0.1.2(transitive)
- Removedglobal@4.4.0(transitive)
- Removedis-function@1.0.2(transitive)
- Removedmin-document@2.19.0(transitive)
- Removedparse-headers@2.0.5(transitive)
- Removedprocess@0.11.10(transitive)
- Removedxhr@2.6.0(transitive)
- Removedxtend@4.0.2(transitive)