Open shgysk8zer0 opened 1 year ago
- Many devs (esp inexperienced) assume
fetch()
throws under conditions in which it does not
That means developers need to learn to look at the response before assuming it's ok, or they need to learn to correctly use your proposed configuration options. Those feel of similar complexity.
The current system of checking response.ok
means you can use the response if it isn't ok. Whereas that isn't the case with this proposal.
The acceptable
option could be split out as response.assertAcceptable()
, which is more composable as you can check response.ok
and response.assertAcceptable()
separately, and react differently to each case.
However, "does the server say the response is JSON?" seems much less useful than "does the response parse as JSON?". If I'm expecting JSON, having a response that parses as JSON seems like a stronger signal that I'm getting what I expect than taking the server's word for it.
As an extension and counter to #1679 I propose adding fetch assertions.
Example
Reasoning
fetch()
throws under conditions in which it does notsignal
orintegrity
)acceptable
)assert
it's ignored and checks still have to be made, but it's a single thing to polyfill)maxContentLength
comes to mind, probably as an additional option)integrity
as example (which is probably duplicated in functionality here), this provides additional error conditions where the response isn't what the dev expectedmaxContentLength
, this could be used to avoid excessive data usage on slow/mobile connectionsfetch()
results in an error based solely on the response + headers but can't currently throw and close