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

json() does not always return valid JSON content #25

Open
cdrx opened this issue Sep 16, 2022 · 1 comment
Open

json() does not always return valid JSON content #25

cdrx opened this issue Sep 16, 2022 · 1 comment

Comments

@cdrx
Copy link

cdrx commented Sep 16, 2022

I think json() does not return a the correct Response type when passed a string.

This works:

router.get("/test", (request) => {
  return json({
     hello: world
  });
});

as it correctly returns {"hello": "world"} with a valid JSON content type header.

But this:

router.get("/test", (request) => {
  return json("hello world");
});

returns hello world (without quotations) and a text/plain content type header. If you try to parse that JSON it will fail. It should return "hello world", with quotations, which will parse correctly.

I think this is because createResponseType(...) only calls JSON.stringify() if the supplied parameter is an object.

@kwhitley
Copy link
Owner

Great catch and investigation! I'm in the middle of rewriting extras (in TS) into itty-router core and will add a test case to cover this!

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

No branches or pull requests

2 participants