Skip to content

pull-stream/pull-fetch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Fetch function for pull stream

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •