Skip to content

Commit

Permalink
fix: making .request return the entire response
Browse files Browse the repository at this point in the history
  • Loading branch information
leftieFriele committed Nov 12, 2024
1 parent 920867c commit 97180e3
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions lib/http-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Metrics from '@metrics/client';
* @typedef HttpClientRequestOptionsAdditions
* @property {AbortSignal} [signal]
*
* @typedef {Pick<import('undici').Dispatcher.ResponseData, 'body' | 'headers' | 'trailers' | 'statusCode'>} HttpClientResponse
* @typedef {import('undici').Dispatcher.ResponseData} HttpClientResponse
*
* @typedef {import('undici').Dispatcher.DispatchOptions & HttpClientRequestOptionsAdditions} HttpClientRequestOptions
*
Expand All @@ -27,9 +27,13 @@ import Metrics from '@metrics/client';
* @property {Number} [timeout=500] - Circuit breaker: How long, in milliseconds, a request can maximum take. Requests exceeding this limit counts against tripping the circuit.
**/

/**
* Client for making HTTP requests, comes with a built-in circuit breaker functionality to prevent
* performing denial of service attacks when target URL is down.
*/
export default class HttpClient {
#agent;
#breaker;
/** @type {import('opossum')} */ #breaker;
#breakerCounter;
#logger;
#clientName;
Expand Down Expand Up @@ -61,7 +65,6 @@ export default class HttpClient {
this.#throwOn400 = throwOn400;
this.#throwOn500 = throwOn500;

// TODO; Can we avoid bind here in a nice way?????
this.#breaker = new Opossum(this.#request.bind(this), {
errorThresholdPercentage: threshold,
resetTimeout: reset,
Expand Down Expand Up @@ -151,9 +154,10 @@ export default class HttpClient {
}

const stopRequestTimer = this.#requestDuration.timer();
const { statusCode, headers, trailers, body } = await request({
const response = await request({
...options,
});
const { statusCode, body } = response;

stopRequestTimer({
labels: {
Expand Down Expand Up @@ -200,12 +204,7 @@ export default class HttpClient {
throw createError(statusCode);
}

return {
statusCode,
headers,
trailers,
body,
};
return response;
}

/**
Expand Down

0 comments on commit 97180e3

Please sign in to comment.