From 1e5dc8587ee9eb6e99bc368f9685f6b62a1701f3 Mon Sep 17 00:00:00 2001 From: James Smith Date: Thu, 12 Apr 2018 10:39:02 -0700 Subject: [PATCH] 3.0.0 --- CHANGELOG.md | 9 +++++++++ README.md | 40 ++++++++-------------------------------- package.json | 2 +- 3 files changed, 18 insertions(+), 33 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 923a259..68ed8e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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** diff --git a/README.md b/README.md index 21b0a1b..8fbd88b 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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? diff --git a/package.json b/package.json index c4eac0a..3bbcff1 100644 --- a/package.json +++ b/package.json @@ -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",