Hi. Thanks for this awesome tool. It is making my life easier. 馃榾
Given the request handler below:
rest.post('/path', (req, res, ctx) => {
// ...
}),
The req object is slightly different in the Browser and Node environments.
In Node:
req.body. In the Browser:
Have a clone of the original request in the req object. This way the handler has access to the Body mixin methods to read the data in other formats: FormData, Blob, ArrayBuffer, etc.
To mock a multipart/form-data request I had to create a custom parser. It works just fine but if we have this feature available for free I guess it makes sense to passed it to the handler.
Does this makes sense? I can submit a PR with the suggested changes.
Hi @otaciliolacerda thanks for raising this 馃槃 .
I think that the line you are referring is the following
Under the hood MSW exchange messages from worker to client through a MessageChannel using JSON.
Supporting formData on worker side I think is not possible at the moment. Maybe the body can be parsed back in the
parseBody function. Btw I think that we should support it in someway.
Hey, @otaciliolacerda. Thank you for reaching out.
The req object is slightly different in the Browser and Node environments.
Yes, this is an intentional design decision. Requests in browser and Node are inevitably two different instances with some data unavailable in one, and only present in another. See Request.
In regards to accessing request body mixins, you can only transfer text in the MessageChannel between the worker and the client. That鈥檚 why we always resolve the body to text and occasionally add some transformations like parsing it to JSON, given the right request headers.
Adding things like req.blob()/req.formData() would be artificially recreating those mixins for the sake of only seeming compatibility. I鈥檇 be careful with decisions like this one, unless we find a suitable way not to rewrite each mixing by hand.
Hi @marcosvega91 and @kettanaito,
First, thanks for the very fast response. I was not completely aware of the MessageChannel (I just read the WebWorker docs).
Now knowing that it was not an easy win like I initially thought, I agree with @kettanaito. Maybe this is a customization to my use case: I am saving some data in-memory to simulate the interactions when running on the browser for fast prototyping.
If need be, you can re-create a Blob or FormData from the req.body text in your handlers.
Yes, this is what I am currently doing. 馃榾
Most helpful comment
Hi @marcosvega91 and @kettanaito,
First, thanks for the very fast response. I was not completely aware of the MessageChannel (I just read the WebWorker docs).
Now knowing that it was not an easy win like I initially thought, I agree with @kettanaito. Maybe this is a customization to my use case: I am saving some data in-memory to simulate the interactions when running on the browser for fast prototyping.