Skip to content

Commit acbf480

Browse files
committed
fix: simplify http client
The conventions in the Fetch API is that you make a request, then do something with the body: ```javascript const result = await fetch('...') const data = await result.json() ``` It doesn't do things like: ```javascript const data = await fetch.json('...') ``` 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 result = await http.post('...') for await (const datum of result.ndjson()) { } ```` It also removes the `.iterator` and `.stream` methods as they do not follow the Fetch pattern either.
1 parent 03e1dd3 commit acbf480

File tree

2 files changed

+27
-47
lines changed

2 files changed

+27
-47
lines changed

src/http.js

+16-47
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,22 @@ class HTTP {
136136
throw new HTTPError(response)
137137
}
138138

139+
response.ndjson = async function * () {
140+
const it = streamToAsyncIterator(response.body)
141+
142+
if (!isAsyncIterator(it)) {
143+
throw new Error('Can\'t convert fetch body into a Async Iterator:')
144+
}
145+
146+
for await (const chunk of ndjson(it)) {
147+
if (options.transform) {
148+
yield options.transform(chunk)
149+
} else {
150+
yield chunk
151+
}
152+
}
153+
}
154+
139155
return response
140156
}
141157

@@ -183,52 +199,6 @@ class HTTP {
183199
options (resource, options = {}) {
184200
return this.fetch(resource, merge(this.opts, options, { method: 'OPTIONS' }))
185201
}
186-
187-
/**
188-
* @param {string | URL | Request} resource
189-
* @param {APIOptions} options
190-
* @returns {Promise<ReadableStream<Uint8Array>>}
191-
*/
192-
async stream (resource, options = {}) {
193-
const res = await this.fetch(resource, merge(this.opts, options))
194-
195-
return res.body
196-
}
197-
198-
/**
199-
* @param {string | URL | Request} resource
200-
* @param {APIOptions} options
201-
* @returns {AsyncGenerator<Uint8Array, void, any>}
202-
*/
203-
async * iterator (resource, options = {}) {
204-
const res = await this.fetch(resource, merge(this.opts, options))
205-
const it = streamToAsyncIterator(res.body)
206-
207-
if (!isAsyncIterator(it)) {
208-
throw new Error('Can\'t convert fetch body into a Async Iterator:')
209-
}
210-
211-
for await (const chunk of it) {
212-
yield chunk
213-
}
214-
}
215-
216-
/**
217-
* @param {string | URL | Request} resource
218-
* @param {APIOptions} options
219-
* @returns {AsyncGenerator<Object, void, any>}
220-
*/
221-
ndjson (resource, options = {}) {
222-
const source = ndjson(this.iterator(resource, merge(this.opts, options)))
223-
if (options.transform) {
224-
return (async function * () {
225-
for await (const chunk of source) {
226-
yield options.transform(chunk)
227-
}
228-
})()
229-
}
230-
return source
231-
}
232202
}
233203

234204
/**
@@ -309,7 +279,6 @@ const isAsyncIterator = (obj) => {
309279

310280
HTTP.HTTPError = HTTPError
311281
HTTP.TimeoutError = TimeoutError
312-
HTTP.ndjson = ndjson
313282
HTTP.streamToAsyncIterator = streamToAsyncIterator
314283

315284
/**

test/http.spec.js

+11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const toStream = require('it-to-stream')
77
const delay = require('delay')
88
const AbortController = require('abort-controller')
99
const drain = require('it-drain')
10+
const all = require('it-all')
1011
const { isBrowser, isWebWorker } = require('../src/env')
1112

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

31+
it('parses the response as ndjson', async function () {
32+
const res = await HTTP.post('http://localhost:3000', {
33+
body: '{}\n{}'
34+
})
35+
36+
const entities = await all(res.ndjson())
37+
38+
expect(entities).to.deep.equal([{}, {}])
39+
})
40+
3041
it.skip('should handle errors in streaming bodies', async function () {
3142
if (isBrowser || isWebWorker) {
3243
// streaming bodies not supported by browsers

0 commit comments

Comments
 (0)