(
method: string,
path: string,
body: unknown,
)
| 63 | } |
| 64 | |
| 65 | private async request<T>( |
| 66 | method: string, |
| 67 | path: string, |
| 68 | body: unknown, |
| 69 | ): Promise<T> { |
| 70 | const startedAt = Date.now(); |
| 71 | const headers: Record<string, string> = { accept: 'application/json' }; |
| 72 | let init: RequestInit; |
| 73 | if (body !== undefined) { |
| 74 | headers['content-type'] = 'application/json'; |
| 75 | init = { method, headers, body: JSON.stringify(body) }; |
| 76 | } else { |
| 77 | init = { method, headers }; |
| 78 | } |
| 79 | const url = this.url(path); |
| 80 | let res: Response; |
| 81 | let text = ''; |
| 82 | try { |
| 83 | res = await this.opts.fetchImpl(url, init); |
| 84 | text = await res.text(); |
| 85 | } catch (error) { |
| 86 | recordReportEvent( |
| 87 | { |
| 88 | kind: 'http', |
| 89 | method, |
| 90 | path, |
| 91 | url, |
| 92 | durationMs: Date.now() - startedAt, |
| 93 | request: requestForReport(body), |
| 94 | error: errorForReport(error), |
| 95 | }, |
| 96 | { reportDir: this.opts.reportDir }, |
| 97 | ); |
| 98 | throw error; |
| 99 | } |
| 100 | let envelope: Envelope<T>; |
| 101 | try { |
| 102 | envelope = JSON.parse(text) as Envelope<T>; |
| 103 | } catch (error) { |
| 104 | recordReportEvent( |
| 105 | { |
| 106 | kind: 'http', |
| 107 | method, |
| 108 | path, |
| 109 | url, |
| 110 | status: res.status, |
| 111 | durationMs: Date.now() - startedAt, |
| 112 | request: requestForReport(body), |
| 113 | response: { raw: text.slice(0, 2_000) }, |
| 114 | error: errorForReport(error), |
| 115 | }, |
| 116 | { reportDir: this.opts.reportDir }, |
| 117 | ); |
| 118 | throw new Error( |
| 119 | `server ${method} ${path} returned non-JSON (HTTP ${res.status}): ${text.slice(0, 200)}`, |
| 120 | { cause: error }, |
| 121 | ); |
| 122 | } |
no test coverage detected