| 22 | } |
| 23 | |
| 24 | export class HTTPClient implements IHTTPClient { |
| 25 | constructor(readonly baseUrl?: string | undefined) {} |
| 26 | |
| 27 | perform( |
| 28 | pathOrUrl: string, |
| 29 | method: HTTPMethod, |
| 30 | headers: StringMap<string> | undefined, |
| 31 | body: ArrayBuffer | Uint8Array | undefined, |
| 32 | ): CancelablePromise<HTTPResponse> { |
| 33 | let cancelFn: (() => void) | undefined; |
| 34 | let rejectFn: ((reason: unknown) => void) | undefined; |
| 35 | const promise = new Promise<HTTPResponse>((resolve, reject) => { |
| 36 | rejectFn = reject; |
| 37 | try { |
| 38 | const request: HTTPRequest = { |
| 39 | url: makeURL(this.baseUrl, pathOrUrl), |
| 40 | method, |
| 41 | headers, |
| 42 | body, |
| 43 | }; |
| 44 | |
| 45 | cancelFn = performRequest(request, (response, error) => { |
| 46 | if (response) { |
| 47 | resolve(response); |
| 48 | } else { |
| 49 | // A request manager reports its failure as text, so wrap it: callers should not have to |
| 50 | // ask whether the rejection they caught is a string or an Error. |
| 51 | reject(error instanceof Error ? error : new Error(String(error))); |
| 52 | } |
| 53 | }); |
| 54 | } catch (err: unknown) { |
| 55 | reject(err); |
| 56 | } |
| 57 | }); |
| 58 | |
| 59 | // No request manager reports a cancelled request, so without this the promise never settles. |
| 60 | // Rejecting an already settled one is a no-op, so a late cancel changes nothing. |
| 61 | return promiseToCancelablePromise(promise, () => { |
| 62 | cancelFn?.(); |
| 63 | rejectFn?.(new Error('Request was cancelled')); |
| 64 | }); |
| 65 | } |
| 66 | |
| 67 | get(pathOrUrl: string, headers?: StringMap<string> | undefined): CancelablePromise<HTTPResponse> { |
| 68 | return this.perform(pathOrUrl, HTTPMethod.GET, headers, undefined); |
| 69 | } |
| 70 | |
| 71 | put( |
| 72 | pathOrUrl: string, |
| 73 | body?: ArrayBuffer | Uint8Array | undefined, |
| 74 | headers?: StringMap<string> | undefined, |
| 75 | ): CancelablePromise<HTTPResponse> { |
| 76 | return this.perform(pathOrUrl, HTTPMethod.PUT, headers, body); |
| 77 | } |
| 78 | |
| 79 | post( |
| 80 | pathOrUrl: string, |
| 81 | body?: ArrayBuffer | Uint8Array | undefined, |
nothing calls this directly
no outgoing calls
no test coverage detected