servie icon indicating copy to clipboard operation
servie copied to clipboard

Common.json() doesn't exist

Open zomble opened this issue 9 years ago • 2 comments

In the README on line 53 it mentions the ability to read the body as a JSON payload, but that method doesn't exist.

When I set the body as an object it immediately will JSON.stringify it and set the appropriate headers. So I guess, text() gives me a JSON payload? Is this a correct assumption, or is json() meant to do something different?

zomble avatar Jan 23 '17 05:01 zomble

Thanks for the report. I forgot to remove this from the README and will update it now, I killed the .json method in 0.0.4 (https://github.com/blakeembrey/node-servie/releases/tag/v0.0.4). The reason for removing it was that setting the body previously wouldn't efficiently count the body length (you'd need to serialise it to a string to know the JSON length). Also, the readers are slightly subjective depending on client-side vs server-side and in the long run parsing just can't exist on the base library because of this. For example, server-side will need inflation while the client-side never needs these sorts of things.

I intend to make a module for reading the body instead that works better with async rather than middleware. E.g. const body = await parse(req) as middleware is just incredibly difficult to model in most cases with type safety in TypeScript.

Edit: To clarify, killed it because the length needed to be set sometime efficiently and reading and writing are really only done once on either side. Keeping a JSON representation didn't really serve much purpose as a result (you could just do const body = await req.text().then(x => JSON.parse(x)) instead and a more complex alternative is required for server-side compression.

blakeembrey avatar Jan 23 '17 05:01 blakeembrey

Make sense. Cheers.

zomble avatar Jan 23 '17 05:01 zomble