Skip to content

Commit

Permalink
3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesplease committed Apr 12, 2018
1 parent fcb5299 commit 1e5dc85
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 33 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

### v3.0.0 (2018/4/12)

**Breaking Changes**

- When an attempt to call `response[responseType]()` errors, then `response.data`
will be set as `null`. This can be useful if you specify the `responseType` as
`json`, and the backend returns malformed JSON (such as an empty string or
plain text). Instead of this error going uncaught, the fetch will work as expected.

### v2.1.1 (2018/3/24)

**Bug Fixes**
Expand Down
40 changes: 8 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ Note that `init` is optional, as with `global.fetch()`.
The third option is `dedupeOptions`, and it is also optional. This is an object with three attributes:

* `responseType` *(String|Function)*: Any of the methods from [the Body mixin](https://developer.mozilla.org/en-US/docs/Web/API/Body).
The default is `"json"`, unless the response status code is `"204"`, in which case `"text"` will be used to prevent
an error.
The default is `"json"`, unless the response status code is `"204"`, in which case `"text"` will be used.

If a function is passed, then it will be passed the `response` object. This lets you dynamically determine the
response type based on information about the response, such as the status code.
Expand Down Expand Up @@ -213,41 +212,18 @@ Wipe the cache of in-flight requests.
### FAQ & Troubleshooting

##### An empty response body is throwing an error, what gives?
##### Why is `response.data` set to `null` sometimes?

Empty text strings are not valid JSON.
If the response cannot be interpreted as the `responseType`, then it will be set as `null`.

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

Consequently, using `json` as the `responseType` when a response's body is empty will cause an
Error to be thrown. To avoid this, we recommend using `text` in these situations instead.

APIs generally use empty bodies in conjunction with a 204 status code for responses
of "write" requests (deletes, updates, and less commonly creates). For this reason, the default
behavior of `responseType` is `"json"` except in situations when a 204 code is returned, in which
case `"text"` will be used instead.
There are two common situations for this:

If your API returns empty bodies with other codes, then you have two options. The first is to
pass a function as `responseType`. This lets you specify the `responseType` based on the `response`.
- The backend returns an empty string when you specify `responseType: 'json'`

For instance, if your backend returns JSON for successful responses, but text stack traces otherwise,
then you might do:

```js
const dedupeOptions = {
responseType(response) {
// 204 status code = no body, so treat it as text
// >= 400 status codes = stack traces, so also treat them as text
return (response.ok && response.status !== 204) ? 'json' : 'text';
}
}
```
- The backend returns a raw text string when you specify `responseType: 'json'`

If your API is exceptionally unreliable, then you can always specify the `responseType` as `"text"`
and try/catch the `JSON.parse` in your application code.
You can use the `responseType` option to have fine-grained control over the parsing of the
response body from the server.

##### Why is `responseType` even an option?

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fetch-dedupe",
"version": "2.1.1",
"version": "3.0.0",
"description": "A thin wrapper around fetch that prevents duplicate requests.",
"main": "lib/index.js",
"module": "es/index.js",
Expand Down

0 comments on commit 1e5dc85

Please sign in to comment.