node.js library to access the Bitbucket API v2
Note: This library is now written in TypeScript and includes full type definitions!
npm install bitbucket-v2const Bitbucket = require('bitbucket-v2').default;
const bitbucketApi = Bitbucket(options);import Bitbucket, { type BitbucketConstructorOptions } from 'bitbucket-v2';
const options: BitbucketConstructorOptions = {
// your options here
};
const bitbucketApi = Bitbucket(options);bitbucketApi.authenticateOAuth2(accessTokenString);
bitbucketApi.user.get().then(({ body }) => {
console.log(body.uuid);
});
It is not necessary to provide any options at all (Bitbucket can be constructed with no argument).
requesterFn((options: RequesterOptions) => Promise<BitbucketResponse>): If provided, requests will be made using the function you provide. This allows you to use your preferred http client. Theoptionsprovided are{ headers, hostname, method, path, query, url, body? }.bodyis only provided onPOSTmethods. In the case ofgetNextPage,getPreviousPage,getForksFromResponseandgetParentFromResponse, only{ headers, method, url }are provided in the options. Example:
import axios from 'axios';
import Bitbucket from 'bitbucket-v2';
const requesterFn = (options) => {
const { url, method, body } = options;
if (method === 'POST') {
return axios.post(url, body);
}
return axios.get(url);
};
const bitbucketApi = Bitbucket({ requesterFn });proxy(string): Defines a proxy to make requests against, instead ofapi.bitbucket.org:443. This option is ignored whenrequesterFnis provided.
Build the TypeScript code:
npm run buildRun linting:
npm run lint