|
1 | | -// DO NOT EDIT — generated from clients/node/commons/src/ (source digest: c39093e4bc410efcbe528a7b462142c8c4d7f0a6). |
| 1 | +// DO NOT EDIT — generated from clients/node/commons/src/ (source digest: 2ff43e12b36dac153c791f7bdb78eb7fe55c4e34). |
2 | 2 | // Regenerate via clients/node/bin/sync-commons.ts |
3 | 3 |
|
4 | 4 | import { Configuration, type Logger } from './configuration.js'; |
@@ -175,23 +175,7 @@ export abstract class ClientBase { |
175 | 175 | const responseHeaders = headersToRecord(fetchResponse.headers); |
176 | 176 | const rateLimit = RateLimit.fromHeaders(fetchResponse.headers); |
177 | 177 |
|
178 | | - let body: unknown; |
179 | | - const text = await fetchResponse.text(); |
180 | | - if (text) { |
181 | | - try { |
182 | | - body = JSON.parse(text); |
183 | | - } catch { |
184 | | - if (fetchResponse.ok) { |
185 | | - throw new TransportError( |
186 | | - `invalid JSON body: ${text.slice(0, 200)}`, |
187 | | - { method, url }, |
188 | | - ); |
189 | | - } |
190 | | - body = {}; |
191 | | - } |
192 | | - } else { |
193 | | - body = {}; |
194 | | - } |
| 178 | + const body: unknown = await this.parseBody(fetchResponse, method, url); |
195 | 179 |
|
196 | 180 | this.logRequest(method, url, fetchResponse.status, durationMs, rateLimit); |
197 | 181 |
|
@@ -256,23 +240,7 @@ export abstract class ClientBase { |
256 | 240 | const durationMs = Date.now() - started; |
257 | 241 | const responseHeaders = headersToRecord(fetchResponse.headers); |
258 | 242 |
|
259 | | - let body: unknown; |
260 | | - const text = await fetchResponse.text(); |
261 | | - if (text) { |
262 | | - try { |
263 | | - body = JSON.parse(text); |
264 | | - } catch { |
265 | | - if (fetchResponse.ok) { |
266 | | - throw new TransportError( |
267 | | - `invalid JSON body: ${text.slice(0, 200)}`, |
268 | | - { method, url }, |
269 | | - ); |
270 | | - } |
271 | | - body = {}; |
272 | | - } |
273 | | - } else { |
274 | | - body = {}; |
275 | | - } |
| 243 | + const body: unknown = await this.parseBody(fetchResponse, method, url); |
276 | 244 |
|
277 | 245 | this.logRequest(method, url, fetchResponse.status, durationMs, null); |
278 | 246 |
|
@@ -351,6 +319,26 @@ export abstract class ClientBase { |
351 | 319 | return result; |
352 | 320 | } |
353 | 321 |
|
| 322 | + private async parseBody( |
| 323 | + fetchResponse: globalThis.Response, |
| 324 | + method: string, |
| 325 | + url: string, |
| 326 | + ): Promise<unknown> { |
| 327 | + const text = await fetchResponse.text(); |
| 328 | + if (!text) return {}; |
| 329 | + try { |
| 330 | + return JSON.parse(text); |
| 331 | + } catch { |
| 332 | + if (fetchResponse.ok) { |
| 333 | + throw new TransportError( |
| 334 | + `invalid JSON body: ${text.slice(0, 200)}`, |
| 335 | + { method, url }, |
| 336 | + ); |
| 337 | + } |
| 338 | + return {}; |
| 339 | + } |
| 340 | + } |
| 341 | + |
354 | 342 | private throwMappedError( |
355 | 343 | status: number, |
356 | 344 | body: unknown, |
|
0 commit comments