renovate/lib/platform/bitbucket/bb-got-wrapper.ts
Rhys Arkins c02abf0976
feat: rename hostRules.platform to hostRules.hostType (#3768)
Refactors hostRules to use field “hostType” instead of “platform”. hostType can include datasources like npm, maven, pypi, etc.
2019-05-21 13:20:09 +02:00

58 lines
1.5 KiB
TypeScript

import got from 'got';
import URL from 'url';
import * as hostRules from '../../util/host-rules';
import { IGotApi, IGotApiOptions } from '../common';
let cache: Renovate.IDict<got.Response<any>> = {};
const endpoint = 'https://api.bitbucket.org/';
const host = 'api.bitbucket.org';
async function get(path: string, options: IGotApiOptions & got.GotJSONOptions) {
const opts: IGotApiOptions &
hostRules.IPlatformConfig &
got.GotJSONOptions = {
// TODO: Move to configurable host rules, or use utils/got
timeout: 60 * 1000,
json: true,
basic: false,
...hostRules.find({ hostType: 'bitbucket', host }),
...options,
};
const url = URL.resolve(endpoint, path);
const method = (
opts.method || /* istanbul ignore next */ 'get'
).toLowerCase();
if (method === 'get' && cache[path]) {
logger.trace({ path }, 'Returning cached result');
return cache[path];
}
opts.headers = {
'user-agent': 'https://github.com/renovatebot/renovate',
authorization: opts.token
? `Basic ${opts.token}`
: /* istanbul ignore next */ undefined,
...opts.headers,
};
const res = await got(url, opts);
if (method.toLowerCase() === 'get') {
cache[path] = res;
}
return res;
}
const helpers = ['get', 'post', 'put', 'patch', 'head', 'delete'];
export const api: IGotApi = {} as any;
for (const x of helpers) {
(api as any)[x] = (url: string, opts: any) =>
get(url, Object.assign({}, opts, { method: x.toUpperCase() }));
}
api.reset = function reset() {
cache = {};
};
export default api;