Skip to content

Commit b214107

Browse files
committed
fix: improve response parsing for multipart and URL-encoded forms
1 parent 327889d commit b214107

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ You can also use all native [`fetch()` settings](https://developer.mozilla.org/e
571571
| refetchOnFocus | `boolean` | `false` | When set to `true`, automatically revalidates (refetches) data when the browser window regains focus. **Note: This bypasses the cache and always makes a fresh network request** to ensure users see the most up-to-date data when they return to your application from another tab or window. Particularly useful for applications that display real-time or frequently changing data, but should be used judiciously to avoid unnecessary network traffic. |
572572
| refetchOnReconnect | `boolean` | `false` | When set to `true`, automatically revalidates (refetches) data when the browser regains internet connectivity after being offline. **This uses background revalidation to silently update data** without showing loading states to users. Helps ensure your application displays fresh data after network interruptions. Works by listening to the browser's `online` event. |
573573
| logger | `Logger` | `null` | You can additionally specify logger object with your custom logger to automatically log the errors to the console. It should contain at least `error` and `warn` functions. |
574-
| fetcher | `CustomFetcher` | `undefined` | A custom fetcher async function. By default, the native `fetch()` is used. If you use your own fetcher, default response parsing e.g. `await response.json()` call will be skipped. Your fetcher should return data. |
574+
| fetcher | `CustomFetcher` | `undefined` | A custom fetcher async function. By default, the native `fetch()` is used. If you use your own fetcher, default response parsing e.g. `await response.json()` call will be skipped. Your fetcher should return response object / data directly. |
575575

576576
> 📋 **Additional Settings Available:**
577577
> The table above shows the most commonly used settings. Many more advanced configuration options are available and documented in their respective sections below, including:

src/response-parser.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,19 @@ export async function parseResponseData<
5555
try {
5656
if (mimeType.includes(APPLICATION_JSON) || mimeType.includes('+json')) {
5757
data = await response.json(); // Parse JSON response
58-
} else if (mimeType.includes('multipart/form-data')) {
59-
data = await response.formData(); // Parse as FormData
60-
} else if (mimeType.includes(APPLICATION_CONTENT_TYPE + 'octet-stream')) {
61-
data = await response.blob(); // Parse as blob
6258
} else if (
63-
mimeType.includes(APPLICATION_CONTENT_TYPE + 'x-www-form-urlencoded')
59+
(mimeType.includes('multipart/form-data') || // Parse as FormData
60+
mimeType.includes(
61+
APPLICATION_CONTENT_TYPE + 'x-www-form-urlencoded', // Handle URL-encoded forms
62+
)) &&
63+
typeof response.formData === FUNCTION
64+
) {
65+
data = await response.formData();
66+
} else if (
67+
mimeType.includes(APPLICATION_CONTENT_TYPE + 'octet-stream') &&
68+
typeof response.blob === FUNCTION
6469
) {
65-
data = await response.formData(); // Handle URL-encoded forms
70+
data = await response.blob(); // Parse as blob
6671
} else if (mimeType.startsWith('text/')) {
6772
data = await response.text(); // Parse as text
6873
} else {

0 commit comments

Comments
 (0)