(
path,
{
method = 'GET',
query,
headers = {},
body = null,
rawBody = null,
json = true
} = {}
)
| 354 | } |
| 355 | |
| 356 | async request( |
| 357 | path, |
| 358 | { |
| 359 | method = 'GET', |
| 360 | query, |
| 361 | headers = {}, |
| 362 | body = null, |
| 363 | rawBody = null, |
| 364 | json = true |
| 365 | } = {} |
| 366 | ) { |
| 367 | const start = Date.now(); |
| 368 | |
| 369 | const qs = query ? '?' + querystring.stringify(query) : ''; |
| 370 | |
| 371 | path = `${path}${qs === '?' ? '' : qs}`; |
| 372 | const response = await fetch(`${this.url}${path}`, { |
| 373 | method, |
| 374 | headers: { |
| 375 | ...headers, |
| 376 | 'user-agent': this.userAgent, |
| 377 | 'request-id': this.requestId |
| 378 | }, |
| 379 | body: rawBody ? rawBody : body ? JSON.stringify(body) : null |
| 380 | }); |
| 381 | |
| 382 | if (!response.ok) { |
| 383 | const body = await response.text(); |
| 384 | let message = body; |
| 385 | let code = 'unknown'; |
| 386 | let trace = null; |
| 387 | try { |
| 388 | const parsed = JSON.parse(body); |
| 389 | message = parsed.message || body; |
| 390 | code = parsed.code || code; |
| 391 | trace = parsed.trace || null; |
| 392 | } catch {} |
| 393 | |
| 394 | this.logger.error( |
| 395 | `caught ${ |
| 396 | response.status |
| 397 | } from ${method} ${path}: message=${message}, code=${code}` |
| 398 | ); |
| 399 | throw Object.assign(new Error(message), { |
| 400 | status: response.status, |
| 401 | headers: response.headers, |
| 402 | trace, |
| 403 | body, |
| 404 | code |
| 405 | }); |
| 406 | } |
| 407 | |
| 408 | this.logger.info(`${method} ${path} in ${Date.now() - start}ms`); |
| 409 | if (json) { |
| 410 | if (response.status === 204) { |
| 411 | return null; |
| 412 | } |
| 413 |
no test coverage detected