Skip to content

Commit

Permalink
Merge pull request #214 from fauna/fe-236-add-fetch-option
Browse files Browse the repository at this point in the history
Add fetch option
  • Loading branch information
Bruno Quaresma authored Jan 17, 2020
2 parents db3ad4a + ba552d7 commit a83acc1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 8 additions & 12 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')

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 Expand Up @@ -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
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(),
text: () => Promise.resolve('{ "success": "Ok" }'),
})
)
const client = util.getClient({ fetch })
await client.ping()
expect(fetch).toBeCalled()
})
})

function assertHeader(headers, name) {
Expand Down

0 comments on commit a83acc1

Please sign in to comment.