holen icon indicating copy to clipboard operation
holen copied to clipboard

Dynamic default `type`

Open jamesplease opened this issue 7 years ago • 0 comments

Holen's default type is "json", which works for most API endpoints. However, a very common feature of APIs is the useful 204 No Content response. This is typically returned from write requests (deletes, updates, and less commonly, creates). The response body in these situations is an empty string, and trying to parse an empty string as JSON throws an error:

JSON.parse('');
// > Uncaught SyntaxError: Unexpected end of JSON input

Although it's impossible to protect against this problem with 100% certainty, there are a few things this lib can do to make it less likely to bite users.

  1. Use text as the type when the response status is 204
  2. Use json otherwise
  3. Include a section in the documentation helping people figure out what is going on when some endpoint explodes on them

I can PR this sometime soon, or someone else can grab it :v:

Here is a reference guide from a lib that implements this approach

jamesplease avatar Feb 05 '18 06:02 jamesplease