Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-fetch-hook

Package Overview
Dependencies
Maintainers
1
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-fetch-hook - npm Package Compare versions

Comparing version 1.2.4 to 1.3.0

10

dist/useFetch.js

@@ -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 @@ };

2

package.json
{
"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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc