mf
make-fetch
npm i make-fetch
mf

make-fetch

Implement your own `fetch()` with async iterators.

by rangermauve

2.3.3 (see all)License:MITTypeScript:Built-In
npm i make-fetch
Readme

make-fetch

Implement your own fetch() with node.js streams

Usage

npm i --save make-fetch
const makeFetch = require('make-fetch')
const fetch = makeFetch(async (request) => {
  const {
    url, // String representing request URL
    headers, // An object mapping header titles to values
    referrer, // An optional string specify the referrer 
    method, // The HTTP method, will always be uppercase, default is `GET`
    body, // An optional async iterable of buffers for the request body
    signal // An optional AbortSignal that you might want to listen to for cancellation
  } = request

  return {
    statusCode: 200, // Should specify the status code to send back
    headers: { // Optional object mapping response header titles to values
      "something": "whatever"
    },
    data: asyncIterator // Required async iterable for the response body, can be empty
  }
})

const response = await fetch('myscheme://whatever/foobar')
console.log(await response.text())

Gotchas

  • The response.body is an Async Iterable of Buffer objects rather than a WHATWG ReadableStream
  • Eventually ReadableStream will become async iterable so you'll be able to iterate either normally

Downloads/wk

62

GitHub Stars

8

LAST COMMIT

1yr ago

MAINTAINERS

1

CONTRIBUTORS

3

OPEN ISSUES

1

OPEN PRs

0
VersionTagPublished
2.3.3
latest
21d ago
No alternatives found
No tutorials found
Add a tutorial