kwhitley / itty-router-extras

An assortment of delicious extras for the calorie-light itty-router.
MIT License
84 stars 12 forks source link

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

Open cdrx opened 2 years ago

cdrx commented 2 years ago

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 commented 2 years ago

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!