sindresorhus / ky

🌳 Tiny & elegant JavaScript HTTP client based on the browser Fetch API
MIT License
11.83k stars 341 forks source link

fix: Add a condition to verify that the `accept` of the request header starts with `'text'` #557

Closed alstn2468 closed 5 months ago

alstn2468 commented 5 months ago

Fixed #556

[Example]

const instance = ky.create({});

// URL returns string 'OK'
instance.get(URL).text();

[Error Message]

VM21639:1 Uncaught (in promise) SyntaxError: Unexpected token 'O', "OK" is not valid JSON

Same as when useJSON.parse('OK')

[Temporary solution]

instance.get(URL, { parseJson: (text) => text }).text();

[Description]

For json type, if there is no JSON type of response, an exception occurs when invoking the JSON.parse function. The text method of Response has been modified to return a string as it is in the response. I think we can discuss which values we should return (whether we return empty strings like 204) I think it's an error to have a parsing error when the user didn't directly call the json method.

2024.01.17 It was modified from using the try catch block to confirm the accpet of the request header. If the type is json and you can't parse it with JSON and you don't want to process it, I'd like your opinion.

alstn2468 commented 5 months ago

@sindresorhus I think I need to add a test code for Ky instance, so it would be nice if you could give me your opinion on which one to add.

alstn2468 commented 5 months ago

Oh I just remembered something, and I think this issuse might be a problem with hooks applied to instance. I'll check a little more and close the PR.