/
bitbucket.ts
81 lines (63 loc) · 2.42 KB
/
bitbucket.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import is from '@sindresorhus/is';
import { logger } from '../../logger';
import type { PagedResult } from '../../modules/platform/bitbucket/types';
import { parseUrl, resolveBaseUrl } from '../url';
import type { HttpOptions, HttpRequestOptions, HttpResponse } from './types';
import { Http } from '.';
const MAX_PAGES = 100;
const MAX_PAGELEN = 100;
let baseUrl = 'https://api.bitbucket.org/';
export const setBaseUrl = (url: string): void => {
baseUrl = url;
};
export interface BitbucketHttpOptions extends HttpOptions {
paginate?: boolean;
pagelen?: number;
}
export class BitbucketHttp extends Http<BitbucketHttpOptions> {
constructor(type = 'bitbucket', options?: BitbucketHttpOptions) {
super(type, options);
}
protected override async request<T>(
path: string,
options?: BitbucketHttpOptions & HttpRequestOptions<T>
): Promise<HttpResponse<T>> {
const opts = { baseUrl, ...options };
const resolvedURL = parseUrl(resolveBaseUrl(baseUrl, path));
// istanbul ignore if: this should never happen
if (is.nullOrUndefined(resolvedURL)) {
logger.error(`Bitbucket: cannot parse path ${path}`);
throw new Error(`Bitbucket: cannot parse path ${path}`);
}
if (opts.paginate && !hasPagelen(resolvedURL)) {
const pagelen = opts.pagelen ?? MAX_PAGELEN;
resolvedURL.searchParams.set('pagelen', pagelen.toString());
}
const result = await super.request<T>(resolvedURL.toString(), opts);
if (opts.paginate && isPagedResult(result.body)) {
const resultBody = result.body as PagedResult<T>;
let page = 1;
let nextURL = resultBody.next;
while (is.nonEmptyString(nextURL) && page <= MAX_PAGES) {
const nextResult = await super.request<PagedResult<T>>(
nextURL,
options as BitbucketHttpOptions
);
resultBody.values.push(...nextResult.body.values);
nextURL = nextResult.body?.next;
page += 1;
}
// Override other page-related attributes
resultBody.pagelen = resultBody.values.length;
resultBody.size = page > MAX_PAGES ? undefined : resultBody.values.length;
resultBody.next = page > MAX_PAGES ? undefined : nextURL;
}
return result;
}
}
function hasPagelen(url: URL): boolean {
return !is.nullOrUndefined(url.searchParams.get('pagelen'));
}
function isPagedResult(obj: any): obj is PagedResult {
return is.nonEmptyObject(obj) && Array.isArray(obj.values);
}