leoek / fetch-to-curl

Convert javascript fetch requests to curl
MIT License
67 stars 13 forks source link

Feature Request: support body from ReadableStream #55

Open cdaringe opened 5 months ago

cdaringe commented 5 months ago

Problem

fetchToCurl(new Request(myUrl, { method: 'POST', body: someReadableStream }) doesn't work, and yields --data-binary={}

Discussion

It's easy to get into wanting this case when dealing with Request instances from common node tools. For example, I'm using msw, which... by the time a request is exiting the VM and you're in a proxy handler, body is converted to a stream just from node itself.

Here's a dummy example of what we could do:

async function reqWithStreamToBodyString(req) {
  const reader = req.clone().body.getReader();
  let result = '';
  while (true) {
    const { value, done } = await reader.read();
    if (done) break;
    result += new TextDecoder("utf-8").decode(value);
  }
  return JSON.stringify(result);
}

Then, rather than a naive if (typeof body === 'object') { ... }, we could first do a if (typeof req.body?.getReader === 'function) { ... } and do the needful

leoek commented 5 months ago

Hi @cdaringe

This sounds indeed like a useful addition, however I'd like to keep the fetchToCurl function synchronous for now. And I assume that implementing this would require it to be async right?

However if we hide this feature behind a flag or configuration option it shouldn't break any code which relies on it returning the string immediately. Are you interested in opening a PR?

Best regards Leo

cdaringe commented 5 months ago

Yes to all!

cdaringe commented 4 months ago

So I started working on it, and i hit a few snags. I ended up wanting

  1. TS (#56 ) enough,
  2. a single entrypoint (e.g. just fetchToCurl)
  3. support for node and web streams

...that I just forked.

https://github.com/cdaringe/fetch-to-curl-ts

With that said, i'm happy to close the fork iff you're interested in the totality of the changes!

I just kinda got to hackin... and I either was gonna send you a massive PR, a wave a PRs, or this fork, and all were kinda overwhelming feeling (but i wanted stuff now)!

Sorry for the noise. Lemme know what you think.

-CD

leoek commented 4 months ago

I've not forgotten about this and will check it out :)