Skip to content
This repository was archived by the owner on Aug 3, 2023. It is now read-only.

Commit 8238d48

Browse files
committed
Support responseType as a function
1 parent f9723f1 commit 8238d48

File tree

4 files changed

+62
-7
lines changed

4 files changed

+62
-7
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,10 @@ hook to transform the data before it is passed into `children`.
327327
The content type of the response body. Defaults to `"json"`, unless the response has a 204 status code,
328328
in which case it will be `"text"`. Valid values are the methods on [Body](https://developer.mozilla.org/en-US/docs/Web/API/Body).
329329
330+
Alternatively, you may specify a function that returns a string. The function will be called with one
331+
argument: `response`. This allows you to dynamically specify the response type based on information
332+
about the response, such as its status code.
333+
330334
```jsx
331335
// If you have an endpoint that just returns raw text, you could, for instance, convert it into
332336
// an object using `responseType` and `transformData`.
@@ -344,6 +348,27 @@ in which case it will be `"text"`. Valid values are the methods on [Body](https:
344348
</Fetch>
345349
```
346350
351+
```jsx
352+
// Some "enterprisey" endpoints return text stack traces anytime that they error. A function
353+
// `responseType` can protect you against this.
354+
<Fetch
355+
url="/countries/2"
356+
responseType={response => {
357+
// Only parse as JSON when the request's code is not an error code, and it is
358+
// not 204 No Content.
359+
return response.ok && response.status !== 204 ? 'json' : 'text';
360+
}}
361+
transformData={countryName => {
362+
return {
363+
countryName
364+
};
365+
}}>
366+
{({ data }) => {
367+
<div>{data.countryName}</div>;
368+
}}
369+
</Fetch>
370+
```
371+
347372
##### `requestName`
348373
349374
A name to give this request, which can help with debugging purposes. The request name is

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
"rollup-plugin-uglify": "^2.0.1"
8282
},
8383
"dependencies": {
84-
"fetch-dedupe": "^2.0.0",
84+
"fetch-dedupe": "^2.1.0",
8585
"prop-types": "^15.6.0"
8686
}
8787
}

src/fetch.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -334,12 +334,9 @@ Fetch.propTypes = {
334334
onResponse: PropTypes.func,
335335
beforeFetch: PropTypes.func,
336336
afterFetch: PropTypes.func,
337-
responseType: PropTypes.oneOf([
338-
'json',
339-
'text',
340-
'blob',
341-
'arrayBuffer',
342-
'formData'
337+
responseType: PropTypes.oneOfType([
338+
PropTypes.func,
339+
PropTypes.oneOf(['json', 'text', 'blob', 'arrayBuffer', 'formData'])
343340
]),
344341
transformResponse: PropTypes.func,
345342
lazy: PropTypes.bool,

test/index.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,39 @@ describe('successful requests', () => {
230230
}, networkTimeout);
231231
});
232232

233+
test('it accepts a custom `responseType` as a function, and calls afterFetch with the right arguments', done => {
234+
fetchMock.get(
235+
'/test/succeeds/secondpls',
236+
new Promise(resolve => {
237+
resolve(successfulResponse());
238+
})
239+
);
240+
241+
expect.assertions(2);
242+
const afterFetchMock = jest.fn();
243+
244+
const wrapper = mount(
245+
<Fetch
246+
url="/test/succeeds/secondpls"
247+
afterFetch={afterFetchMock}
248+
responseType={() => 'text'}
249+
/>
250+
);
251+
252+
setTimeout(() => {
253+
expect(afterFetchMock).toHaveBeenCalledTimes(1);
254+
expect(afterFetchMock).toBeCalledWith(
255+
expect.objectContaining({
256+
url: '/test/succeeds/secondpls',
257+
error: null,
258+
didUnmount: false,
259+
data: 'hi'
260+
})
261+
);
262+
done();
263+
}, networkTimeout);
264+
});
265+
233266
test('`transformData` is used to transform the response data', done => {
234267
fetchMock.get(
235268
'/test/succeeds/third',

0 commit comments

Comments
 (0)