async-fetch 0.3.2 → 0.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -4
- package/package.json +5 -3
- package/useAsyncFetch.js +28 -27
- package/useInterval.js +4 -2
- package/useCache.js +0 -23
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# useAsyncFetch
|
|
2
2
|
|
|
3
|
-
Use fetch for requests within React components.
|
|
3
|
+
Use async fetch hook for requests within React components.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -56,12 +56,11 @@ The minimum requirement for the hook is a url string as the first argument. The
|
|
|
56
56
|
| initialError | Any | Initial state for the error constant. | |
|
|
57
57
|
| deps | Array | List of dependencies to run the request on. | |
|
|
58
58
|
| poll | Number | Number of milliseconds to wait for polling requests. | |
|
|
59
|
-
| cachetime | Number | Number of milliseconds to cache responses for. | 60000 |
|
|
60
59
|
| timeout | Number | Number of milliseconds to wait before canceling the request. | 30000 |
|
|
61
60
|
| ignoreCleanup | Boolean | Whether or not the hook should cleanup on component unmount. | |
|
|
62
61
|
| ignoreRequest | Boolean | Whether or not the request should send. | |
|
|
63
|
-
| query | Object | JSON object to append to the url as
|
|
64
|
-
| params | Object | JSON object to append to the url as
|
|
62
|
+
| query | Object | JSON object to append to the url as search params. | |
|
|
63
|
+
| params | Object | JSON object to append to the url as search params. | |
|
|
65
64
|
| data | Object | JSON object to send in the request body. | |
|
|
66
65
|
| parser | String | Method used to parse the response. | "json" |
|
|
67
66
|
| onStart | Function | Callback function to call when the request starts. | |
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "async-fetch",
|
|
3
|
-
"version": "0.3.
|
|
4
|
-
"description": "Use fetch for requests within React components.",
|
|
3
|
+
"version": "0.3.4",
|
|
4
|
+
"description": "Use async fetch hook for requests within React components.",
|
|
5
5
|
"main": "useAsyncFetch.js",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"scripts": {},
|
|
@@ -17,6 +17,8 @@
|
|
|
17
17
|
],
|
|
18
18
|
"author": "Nameer Rizvi (https://github.com/nameer-rizvi)",
|
|
19
19
|
"license": "ISC",
|
|
20
|
-
"bugs":
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "https://github.com/nameer-rizvi/useAsyncFetch/issues"
|
|
22
|
+
},
|
|
21
23
|
"homepage": "https://github.com/nameer-rizvi/useAsyncFetch#readme"
|
|
22
24
|
}
|
package/useAsyncFetch.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { useState, useEffect } from "react";
|
|
2
|
-
import useCache from "./useCache.js";
|
|
3
2
|
import useInterval from "./useInterval.js";
|
|
4
3
|
|
|
5
4
|
function useAsyncFetch(stringUrl, props = {}) {
|
|
@@ -9,7 +8,6 @@ function useAsyncFetch(stringUrl, props = {}) {
|
|
|
9
8
|
initialError,
|
|
10
9
|
deps = [],
|
|
11
10
|
poll,
|
|
12
|
-
cachetime = 60000, // 1 minute.
|
|
13
11
|
timeout = 30000, // 30 seconds.
|
|
14
12
|
ignoreCleanup,
|
|
15
13
|
ignoreRequest,
|
|
@@ -36,14 +34,12 @@ function useAsyncFetch(stringUrl, props = {}) {
|
|
|
36
34
|
|
|
37
35
|
const [unmounted, setUnmounted] = useState(false);
|
|
38
36
|
|
|
39
|
-
const cache = useCache(cachetime);
|
|
40
|
-
|
|
41
37
|
useEffect(() => {
|
|
42
38
|
return cleanupRequest;
|
|
43
39
|
}, []);
|
|
44
40
|
|
|
45
41
|
useEffect(() => {
|
|
46
|
-
sendRequest(
|
|
42
|
+
sendRequest();
|
|
47
43
|
}, [stringUrl, ...deps]);
|
|
48
44
|
|
|
49
45
|
useInterval(() => {
|
|
@@ -61,7 +57,7 @@ function useAsyncFetch(stringUrl, props = {}) {
|
|
|
61
57
|
}
|
|
62
58
|
}
|
|
63
59
|
|
|
64
|
-
async function sendRequest(
|
|
60
|
+
async function sendRequest() {
|
|
65
61
|
if (!stringUrl) {
|
|
66
62
|
throw new Error("URL is required.");
|
|
67
63
|
}
|
|
@@ -96,56 +92,61 @@ function useAsyncFetch(stringUrl, props = {}) {
|
|
|
96
92
|
|
|
97
93
|
if (!unmounted) {
|
|
98
94
|
if (pending) setPending2(true);
|
|
95
|
+
|
|
99
96
|
if (!pending) setPending(true);
|
|
97
|
+
|
|
100
98
|
setError();
|
|
99
|
+
|
|
101
100
|
cancelRequest();
|
|
101
|
+
|
|
102
102
|
setCancelSource(controller);
|
|
103
|
+
|
|
103
104
|
if (onStart) onStart();
|
|
104
105
|
}
|
|
105
106
|
|
|
106
|
-
const
|
|
107
|
-
|
|
108
|
-
let parsedResponse = cachedResponse;
|
|
107
|
+
const response = await fetch(url, fetchProps);
|
|
109
108
|
|
|
110
|
-
if (!
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
response: await response.text(),
|
|
119
|
-
}),
|
|
120
|
-
);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
parsedResponse = await response[parser]();
|
|
109
|
+
if (!response.ok) {
|
|
110
|
+
throw new Error(
|
|
111
|
+
JSON.stringify({
|
|
112
|
+
code: response.status,
|
|
113
|
+
text: response.statusText,
|
|
114
|
+
response: await response.text(),
|
|
115
|
+
}),
|
|
116
|
+
);
|
|
124
117
|
}
|
|
125
118
|
|
|
119
|
+
const parsedResponse = await response[parser]();
|
|
120
|
+
|
|
126
121
|
if (!unmounted) {
|
|
127
122
|
setData(parsedResponse);
|
|
123
|
+
|
|
128
124
|
if (onSuccess) onSuccess(parsedResponse);
|
|
129
|
-
if (!cachedResponse) cache.set(url, parsedResponse);
|
|
130
125
|
}
|
|
131
126
|
} catch (e) {
|
|
132
127
|
if (!unmounted && e.name !== "AbortError") {
|
|
133
128
|
let error;
|
|
129
|
+
|
|
134
130
|
try {
|
|
135
|
-
error = e.toString().replace("Error:", "");
|
|
136
|
-
error = JSON.parse(error.trim());
|
|
131
|
+
error = JSON.parse(e.toString().replace("Error:", "").trim());
|
|
137
132
|
} catch {
|
|
138
133
|
error = { response: e.toString(), text: e.toString() };
|
|
139
134
|
}
|
|
135
|
+
|
|
140
136
|
setError(error);
|
|
137
|
+
|
|
141
138
|
if (onFail) onFail(error);
|
|
142
139
|
}
|
|
143
140
|
} finally {
|
|
144
141
|
clearTimeout(requestTimeout);
|
|
142
|
+
|
|
145
143
|
if (!unmounted) {
|
|
146
144
|
if (pending) {
|
|
147
145
|
setPending2();
|
|
148
|
-
} else
|
|
146
|
+
} else {
|
|
147
|
+
setPending();
|
|
148
|
+
}
|
|
149
|
+
|
|
149
150
|
if (onFinish) onFinish();
|
|
150
151
|
}
|
|
151
152
|
}
|
package/useInterval.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useRef, useEffect } from "react";
|
|
2
2
|
|
|
3
3
|
function useInterval(callback, poll) {
|
|
4
4
|
const callbackRef = useRef(() => {}); // noop
|
|
@@ -8,8 +8,10 @@ function useInterval(callback, poll) {
|
|
|
8
8
|
});
|
|
9
9
|
|
|
10
10
|
useEffect(() => {
|
|
11
|
-
if (typeof poll !== "number") return;
|
|
11
|
+
if (typeof poll !== "number" || poll < 1000) return;
|
|
12
|
+
|
|
12
13
|
const interval = setInterval(() => callbackRef.current(), poll);
|
|
14
|
+
|
|
13
15
|
return () => {
|
|
14
16
|
clearInterval(interval);
|
|
15
17
|
};
|
package/useCache.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import useInterval from "./useInterval.js";
|
|
2
|
-
|
|
3
|
-
const cache = {};
|
|
4
|
-
|
|
5
|
-
function useCache(cachetime) {
|
|
6
|
-
useInterval(() => {
|
|
7
|
-
for (let [key, value] of Object.entries(cache)) {
|
|
8
|
-
if (value.timestamp + cachetime < new Date().getTime()) delete cache[key];
|
|
9
|
-
}
|
|
10
|
-
}, cachetime);
|
|
11
|
-
|
|
12
|
-
function get(url) {
|
|
13
|
-
if (cachetime) return cache[url]?.response;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
function set(url, response) {
|
|
17
|
-
if (cachetime) cache[url] = { timestamp: new Date().getTime(), response };
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
return { get, set };
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export default useCache;
|