JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2
  • Score
    100M100P100Q22226F
  • License MIT

Fetch function for pull stream

Package Exports

  • pull-fetch

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (pull-fetch) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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.