mswjs / data

Data modeling and relation library for testing JavaScript applications.
https://npm.im/@mswjs/data
MIT License
822 stars 52 forks source link

REST toHandlers() PUT and POST do not work with @mswjs/http-middleware due to req.body being a string #293

Open lgibso34 opened 1 year ago

lgibso34 commented 1 year ago

I noticed this message about this lib being in maintenance mode but I hope this bug is big enough to fix or has an easy solution.

data/src/model/generateRestHandlers.ts line 200 and 217 both pass req.body to model methods that expect the data as a JS object but instead receive a string.

I have an ExpressJS server setup using MSW mock API endpoints with @mswjs/http-middleware's createMiddleware(...handlers). That middleware has these lines that always convert makes sure that req.body as a string (see issue).

I attempted to make a merge request to fix this but there were already some Jest tests failing on main and didn't know how to solve them. In this test the node-fetch fetch is catching the errors thrown by the mock server instead of the withErrors HOF and no longer passing. Ironically the POST and PUT's are having this issue. If you add fetch().catch it will catch it there but seems unintended.

Anyways, I hope we can find a way to fix this so that these libraries can work together in harmony. Thank you!

EDIT: The issue may be more on the http-middleware library to solve. Like an optional alwaysParseReqBodyAsString: boolean option or something.