| 184 | } |
| 185 | |
| 186 | export default class API { |
| 187 | apiRoot: string; |
| 188 | branch: string; |
| 189 | repo: string; |
| 190 | requestFunction: (req: ApiRequest) => Promise<Response>; |
| 191 | repoURL: string; |
| 192 | commitAuthor?: CommitAuthor; |
| 193 | mergeStrategy: string; |
| 194 | initialWorkflowStatus: string; |
| 195 | cmsLabelPrefix: string; |
| 196 | |
| 197 | constructor(config: Config) { |
| 198 | this.apiRoot = config.apiRoot || DEFAULT_API_ROOT; |
| 199 | this.branch = config.branch || 'master'; |
| 200 | this.repo = config.repo || ''; |
| 201 | this.requestFunction = config.requestFunction || unsentRequest.performRequest; |
| 202 | // Allow overriding this.hasWriteAccess |
| 203 | this.hasWriteAccess = config.hasWriteAccess || this.hasWriteAccess; |
| 204 | this.repoURL = this.repo ? `/repositories/${this.repo}` : ''; |
| 205 | this.mergeStrategy = config.squashMerges ? 'squash' : 'merge_commit'; |
| 206 | this.initialWorkflowStatus = config.initialWorkflowStatus; |
| 207 | this.cmsLabelPrefix = config.cmsLabelPrefix; |
| 208 | } |
| 209 | |
| 210 | buildRequest = (req: ApiRequest) => { |
| 211 | const withRoot = unsentRequest.withRoot(this.apiRoot)(req); |
| 212 | if (withRoot.has('cache')) { |
| 213 | return withRoot; |
| 214 | } else { |
| 215 | const withNoCache = unsentRequest.withNoCache(withRoot); |
| 216 | return withNoCache; |
| 217 | } |
| 218 | }; |
| 219 | |
| 220 | request = (req: ApiRequest): Promise<Response> => { |
| 221 | try { |
| 222 | return requestWithBackoff(this, req); |
| 223 | } catch (err) { |
| 224 | throw new APIError(err.message, null, API_NAME); |
| 225 | } |
| 226 | }; |
| 227 | |
| 228 | responseToJSON = responseParser({ format: 'json', apiName: API_NAME }); |
| 229 | responseToBlob = responseParser({ format: 'blob', apiName: API_NAME }); |
| 230 | responseToText = responseParser({ format: 'text', apiName: API_NAME }); |
| 231 | |
| 232 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 233 | requestJSON = (req: ApiRequest) => this.request(req).then(this.responseToJSON) as Promise<any>; |
| 234 | requestText = (req: ApiRequest) => this.request(req).then(this.responseToText) as Promise<string>; |
| 235 | |
| 236 | // Bitbucket's pagination links are absolute api.bitbucket.org URLs. Make them |
| 237 | // relative so `buildRequest` sends them to `apiRoot`, the same as the first page. |
| 238 | toRelativeLink = (link: string) => |
| 239 | link.startsWith(`${DEFAULT_API_ROOT}/`) ? link.slice(DEFAULT_API_ROOT.length) : link; |
| 240 | |
| 241 | // Bitbucket returns lists one page at a time. Follow the `next` links and |
| 242 | // return the values of every page. |
| 243 | async requestAllValues<T>(req: ApiRequest): Promise<T[]> { |
nothing calls this directly
no test coverage detected