react-fetch-hook
Advanced tools
Comparing version 1.2.4 to 1.3.0
@@ -25,3 +25,11 @@ "use strict"; | ||
return function (path, options) { | ||
return fetch(path, options).then(typeof formatter === "function" && formatter || defaultFormatter); | ||
var _ref = options || {}, | ||
preventCallFetch = _ref.preventCallFetch, | ||
otherOptions = _objectWithoutProperties(_ref, ["preventCallFetch"]); | ||
if (preventCallFetch) { | ||
return Promise.resolve(); | ||
} | ||
return fetch(path, otherOptions).then(typeof formatter === "function" && formatter || defaultFormatter); | ||
}; | ||
@@ -28,0 +36,0 @@ }; |
{ | ||
"name": "react-fetch-hook", | ||
"version": "1.2.4", | ||
"version": "1.3.0", | ||
"description": "React fetch hook", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -54,3 +54,3 @@ # react-fetch-hook | ||
``` | ||
### Custom formatter | ||
You can pass *formatter* prop for using custom formatter function. Default is used *response => response.json()* formatter. | ||
@@ -63,1 +63,11 @@ ```javascript | ||
``` | ||
### Prevent call `fetch` | ||
For prevent call fetch you can pass *preventCallFetch* prop: | ||
```javascript | ||
const {authToken} = useContext(authTokenContext); | ||
const { isLoading, data } = useFetch("https://swapi.co/api/people/1", { | ||
preventCallFetch: !authToken //don't call request, if haven't authToken | ||
}); | ||
``` |
@@ -83,2 +83,25 @@ import React from "react"; | ||
it("call with url, options with preventCallFetch", async () => { | ||
fetch.mockResponse(JSON.stringify({ data: "12345" })); | ||
const options = { | ||
headers: { | ||
Accept: "application/json, application/xml, text/plain, text/html, *.*", | ||
"Content-Type": "application/json; charset=utf-8" | ||
} | ||
}; | ||
const Component = () => { | ||
const result = useFetch("https://google.com", { ...options, preventCallFetch: true }); | ||
return <div>{result.data}</div>; | ||
}; | ||
const { container, rerender } = render(<Component />); | ||
await wait(() => { | ||
rerender(<Component />); | ||
expect(fetch.mock.calls.length).toEqual(0); | ||
}); | ||
}); | ||
it("error on throw error", async () => { | ||
@@ -85,0 +108,0 @@ fetch.mockReject(new Error("fake error message")); |
@@ -12,3 +12,3 @@ // @flow | ||
path: RequestInfo, | ||
options?: { ...RequestOptions, formatter?: Response => Promise<T> } | ||
options?: { ...RequestOptions, formatter?: Response => Promise<T>, preventCallFetch?: boolean } | ||
): TUseFetchResult<T> { | ||
@@ -22,3 +22,7 @@ const defaultFormatter = response => { | ||
const fetchInstance = formatter => (path, options) => { | ||
return fetch(path, options).then((typeof formatter === "function" && formatter) || defaultFormatter); | ||
const { preventCallFetch, ...otherOptions } = options || {}; | ||
if (preventCallFetch) { | ||
return Promise.resolve(); | ||
} | ||
return fetch(path, otherOptions).then((typeof formatter === "function" && formatter) || defaultFormatter); | ||
}; | ||
@@ -25,0 +29,0 @@ if (options) { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20129
383
72
19