-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Description
In other help section i have mention ways of posting a FormData, Blob, and a File
Some solutions involves adding same extra dependency on the same thing that node-fetch also depends on and other solutions involves more hacky stuff to get the internals of what node-fetch is using internally.
like:
import fetch, { Headers, Request, Response } from 'node-fetch'
// Get the internals fetch depends on
const { constructor: Blob } = await new Response().blob()
const { constructor: FormData } = await new Response(new URLSearchParams).formData()
const fd = new FormData(); fd.set('a', new Blob())
const { constructor: File } = fd.get('a')
const file = new File(['hello world'], 'sample.txt', { type: 'text/plain' })
fd.set('a', file)
fetch(url, { method: 'POST', body: fd })it feels very hacky and unnecessary to go around the problem of using the same dependency as node-fetch is using.
It also requires top level await to work...
I think that we should expose this 3 classes as well so you can just do:
import fetch, { Headers, Request, Response, FormData, Blob, File } from 'node-fetch'
const file = new File(['hello world'], 'sample.txt', { type: 'text/plain' })
const fd = new FormData()
fd.set('a', file)
fetch(url, { method: 'POST', body: fd })it would also be worth exposing the fetch-blob/from.js utility functions that turns a file into a Blob or a File without actually reading the hole content into memory...
i think this would simplify the developers experiences and also making it easier to keep things up to date and avoiding dedupe and using different versions
For the v4 milestone, would it also be worth exposing the whatwg Readable/Wrtiable Stream as well?
(So that we take care of exposing nodes builtin whatwg stream whenever possible and falling back to web-streams-polyfill otherwise)
import { stream } from 'node-fetch'
const { TransformStream, ReadableStream, WritableStream, ... } = streammaybe it's too much exposure?
it is also possible to do the same hack here (but you only get ReadableStream)
const ReadableStream = new Response('').body.constructor
const rs = new ReadableStream({ /* ... */ })
fetch(url, { method: 'POST', body: rs })it would be nice to have the same instances of streams so they can be piped to...
piping from a native ReadableStream to a polyfilled WritableStream don't work well together...