Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fetch option #214

Merged
merged 3 commits into from
Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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'
Expand All @@ -64,6 +65,7 @@ function Client(options) {
observer: null,
keepAlive: true,
headers: {},
fetch: undefined,
})
var isHttps = opts.scheme === 'https'

Expand All @@ -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')
BrunoQuaresma marked this conversation as resolved.
Show resolved Hide resolved

if (isNodeEnv && opts.keepAlive) {
this._keepAliveEnabledAgent = new (isHttps
Expand Down Expand Up @@ -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({
Expand Down
12 changes: 12 additions & 0 deletions test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we want a test that exercises headers as an Iterator as is the case when using browser fetch

Copy link
Contributor Author

@BrunoQuaresma BrunoQuaresma Jan 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Iterator is returned by the entries(). The headers options are type of Headers[] which have a similar API with the Headers object. I used Set() to avoid extra mocking.

Headers ref: https://developer.mozilla.org/en-US/docs/Web/API/Headers/Headers

text: () => Promise.resolve('{ "success": "Ok" }'),
})
)
const client = util.getClient({ fetch })
await client.ping()
expect(fetch).toBeCalled()
})
})

function assertHeader(headers, name) {
Expand Down