Skip to content

Commit

Permalink
Update FetchHttpClient to make fetch function optional. (#1253)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcr-stripe committed Sep 24, 2021
1 parent b495c10 commit a1aa228
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/net/FetchHttpClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
const {HttpClient, HttpClientResponse} = require('./HttpClient');

/**
* HTTP client which uses a `fetch` function to issue requests. This fetch
* function is expected to be the Web Fetch API function or an equivalent, such
* as the function provided by the node-fetch package (https://github.com/node-fetch/node-fetch).
* HTTP client which uses a `fetch` function to issue requests.
*
* By default relies on the global `fetch` function, but an optional function
* can be passed in. If passing in a function, it is expected to match the Web
* Fetch API. As an example, this could be the function provided by the
* node-fetch package (https://github.com/node-fetch/node-fetch).
*/
class FetchHttpClient extends HttpClient {
constructor(fetchFn) {
Expand Down Expand Up @@ -36,7 +39,8 @@ class FetchHttpClient extends HttpClient {
);
url.port = port;

const fetchPromise = this._fetchFn(url.toString(), {
const fetchFn = this._fetchFn || fetch;
const fetchPromise = fetchFn(url.toString(), {
method,
headers,
body: requestData || undefined,
Expand Down

0 comments on commit a1aa228

Please sign in to comment.