Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix res.json() to accept non-object input #426

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

JTBrinkmann
Copy link

Just like the express.js documentation, the tinyhttp documentation says

Sends a JSON response. This method sends a response (with the correct Content-Type header) that is the parameter converted to a JSON string using JSON.stringify().

The body can be any kind of JSON, including object, array, string, boolean, number, or null.

However, only Objects (incl. Arrays), null and undefined are currently supported. Also, the current implementation has an undocumented special handling of Strings, simply assuming they are JSON strings instead of using JSON.stringify() (as the documentation explicitly says and as express.js does; i would argue: as users would reasonably expect).

In short: neither res.json(true) nor res.json(123) works yet.

This PR fixes the current implementation to also support inputs of type boolean and number. It does not remove the special handling of string inputs, as this would be a breaking change (though IMO that should be done for the next major version).

As it should pass linter tests, this PR also replaces any types. This breaks the signature res.send(body: string, encoding: BufferEncoding, cb?: () => void), which I can only imagine being not much of a big deal.

Only the send package is affected.

As a positive side effect, this PR reduces the code size.

@aarontravass
Copy link
Member

Please add test cases

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants