diff --git a/README.md b/README.md index 86df65c49..81bce0053 100644 --- a/README.md +++ b/README.md @@ -158,6 +158,18 @@ var helper = client.paginate( ) ``` +#### Custom Fetch + +To use a custom `fetch()` you just have to specify it in the configuration and make it compatible with the [standard Web API Specification of the Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API). + +```javascript +const customFetch = require('./customFetch') +const client = new faunadb.Client({ + secret: 'YOUR_FAUNADB_SECRET', + fetch: customFetch, +}) +``` + ## Client Development Run `yarn` to install dependencies. diff --git a/src/Client.js b/src/Client.js index 31a4b03c5..ebbb1bcbe 100644 --- a/src/Client.js +++ b/src/Client.js @@ -3,7 +3,6 @@ var APIVersion = '2.7' var btoa = require('btoa-lite') -var fetch = require('cross-fetch') var errors = require('./errors') var query = require('./query') var values = require('./values') @@ -52,6 +51,8 @@ var parse = require('url-parse') * Callback that will be called after every completed request. * @param {?boolean} options.keepAlive * Configures http/https keepAlive option (ignored in browser environments) + * @param {?fetch} options.fetch + * a fetch compatible [API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) for making a request */ function Client(options) { var isNodeEnv = typeof window === 'undefined' @@ -64,6 +65,7 @@ function Client(options) { observer: null, keepAlive: true, headers: {}, + fetch: undefined, }) var isHttps = opts.scheme === 'https' @@ -77,6 +79,7 @@ function Client(options) { this._observer = opts.observer this._lastSeen = null this._headers = opts.headers + this._fetch = opts.fetch || require('cross-fetch') if (isNodeEnv && opts.keepAlive) { this._keepAliveEnabledAgent = new (isHttps @@ -215,7 +218,7 @@ Client.prototype._performRequest = function( options = defaults(options, {}) const secret = options.secret || this._secret - return fetch(url.href, { + return this._fetch(url.href, { agent: this._keepAliveEnabledAgent, body: body, headers: util.removeNullAndUndefinedValues({ @@ -248,17 +251,10 @@ function secretHeader(secret) { } function responseHeadersAsObject(response) { - var responseHeaders = response.headers - var headers = {} + let headers = {} - if (typeof responseHeaders.forEach === 'function') { - responseHeaders.forEach(function(value, name) { - headers[name] = value - }) - } else { - responseHeaders.entries().forEach(function(pair) { - headers[pair[0]] = pair[1] - }) + for (const [key, value] of response.headers.entries()) { + headers[key] = value } return headers diff --git a/test/client.test.js b/test/client.test.js index 1724d0473..706bb840d 100644 --- a/test/client.test.js +++ b/test/client.test.js @@ -111,6 +111,18 @@ describe('Client', () => { return expect(resultWithoutOptions).toEqual(resultWithOptions) }) + + test('uses custom fetch', async function() { + const fetch = jest.fn(() => + Promise.resolve({ + headers: new Set(), + text: () => Promise.resolve('{ "success": "Ok" }'), + }) + ) + const client = util.getClient({ fetch }) + await client.ping() + expect(fetch).toBeCalled() + }) }) function assertHeader(headers, name) {