1.1.0 • Published 6 years ago

pull-fetch v1.1.0

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

pull-fetch

A pull-stream HTTP client for Node.js

Install

npm i pull-fetch

Usage

fetch(options)

Make an HTTP request. Options are the same as http.request. If it HTTPS it will switch the function.

Returns a pull-stream sink that reads data for the request. If the request has not body (e.g. a GET request) then function returned can be called with nothing to proceed with the response.

Then the stream returns a promise that resolves into a response object. It contains any property http.IncomingMessage has, plus source for streaming the response data as a pull-stream source

const response = await fetch({
  host: 'api.example.com',
  path: '/foobar',
  method: 'POST'
})(
  values([ 'hello', 'world' ])
)

console.log(response.headers)

collect((err, data) => {
  data = Buffer.join(data).toString()
  consle.log(data)
})(response.source)

Or with no body:

const response = await fetch('https://api.example.com/foobar')()

console.log(response.headers)

drain(x => process.stdout.write(x))(response.source)

Combining pull-streams and promises give an intuitive way to handle the requests. There is little to no overhead with Node's APIs.