Skip to content

Commit

Permalink
fix: simplify http client (#35)
Browse files Browse the repository at this point in the history
The conventions in the Fetch API is that you make a request, then do something
with the response:

```javascript
const response = await fetch('...')
const data = await response.json()
```

It doesn't do things like:

```javascript
const data = await fetch.json('...') // what method would this use?
```

This PR brings our API more inline with Fetch so where we used to do:

```javascript
for await (const datum of http.ndjson('...')) { // what method does this use?

}
```

We now do the more idiomatic:

```javascript
const response = await http.post('...')

for await (const datum of response.ndjson()) {

}
```

It also removes the `.iterator` and `.stream` methods as they do not
follow the Fetch pattern either though they can be added to the response
object if they are useful.

BREAKING CHANGE:

- The `.ndjson`, `.stream` and `.iterator` methods have been removed
- An `.ndjson` async generator function has been added to the response which
  does the same thing the `.ndjson` instance method used to

Old:

```javascript
for await (const datum of http.ndjson('http://...')) {

}
```

New:

```javascript
const response = await http.post('http://...')

for await (const datum of response.ndjson()) {

}
```
  • Loading branch information
achingbrain authored Apr 9, 2020
1 parent 03e1dd3 commit 05c4c5d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 47 deletions.
63 changes: 16 additions & 47 deletions src/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,22 @@ class HTTP {
throw new HTTPError(response)
}

response.ndjson = async function * () {
const it = streamToAsyncIterator(response.body)

if (!isAsyncIterator(it)) {
throw new Error('Can\'t convert fetch body into a Async Iterator:')
}

for await (const chunk of ndjson(it)) {
if (options.transform) {
yield options.transform(chunk)
} else {
yield chunk
}
}
}

return response
}

Expand Down Expand Up @@ -183,52 +199,6 @@ class HTTP {
options (resource, options = {}) {
return this.fetch(resource, merge(this.opts, options, { method: 'OPTIONS' }))
}

/**
* @param {string | URL | Request} resource
* @param {APIOptions} options
* @returns {Promise<ReadableStream<Uint8Array>>}
*/
async stream (resource, options = {}) {
const res = await this.fetch(resource, merge(this.opts, options))

return res.body
}

/**
* @param {string | URL | Request} resource
* @param {APIOptions} options
* @returns {AsyncGenerator<Uint8Array, void, any>}
*/
async * iterator (resource, options = {}) {
const res = await this.fetch(resource, merge(this.opts, options))
const it = streamToAsyncIterator(res.body)

if (!isAsyncIterator(it)) {
throw new Error('Can\'t convert fetch body into a Async Iterator:')
}

for await (const chunk of it) {
yield chunk
}
}

/**
* @param {string | URL | Request} resource
* @param {APIOptions} options
* @returns {AsyncGenerator<Object, void, any>}
*/
ndjson (resource, options = {}) {
const source = ndjson(this.iterator(resource, merge(this.opts, options)))
if (options.transform) {
return (async function * () {
for await (const chunk of source) {
yield options.transform(chunk)
}
})()
}
return source
}
}

/**
Expand Down Expand Up @@ -309,7 +279,6 @@ const isAsyncIterator = (obj) => {

HTTP.HTTPError = HTTPError
HTTP.TimeoutError = TimeoutError
HTTP.ndjson = ndjson
HTTP.streamToAsyncIterator = streamToAsyncIterator

/**
Expand Down
11 changes: 11 additions & 0 deletions test/http.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const toStream = require('it-to-stream')
const delay = require('delay')
const AbortController = require('abort-controller')
const drain = require('it-drain')
const all = require('it-all')
const { isBrowser, isWebWorker } = require('../src/env')

describe('http', function () {
Expand All @@ -27,6 +28,16 @@ describe('http', function () {
await expect(res).to.eventually.be.rejectedWith(/aborted/)
})

it('parses the response as ndjson', async function () {
const res = await HTTP.post('http://localhost:3000', {
body: '{}\n{}'
})

const entities = await all(res.ndjson())

expect(entities).to.deep.equal([{}, {}])
})

it.skip('should handle errors in streaming bodies', async function () {
if (isBrowser || isWebWorker) {
// streaming bodies not supported by browsers
Expand Down

0 comments on commit 05c4c5d

Please sign in to comment.