async-fetch 0.3.2 → 0.3.3
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 +29 -32
- 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.3",
|
|
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,19 +34,13 @@ 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
|
-
useEffect(
|
|
46
|
-
sendRequest("USE_CACHE");
|
|
47
|
-
}, [stringUrl, ...deps]);
|
|
41
|
+
useEffect(sendRequest, [stringUrl, ...deps]);
|
|
48
42
|
|
|
49
|
-
useInterval(
|
|
50
|
-
sendRequest();
|
|
51
|
-
}, poll);
|
|
43
|
+
useInterval(sendRequest, poll);
|
|
52
44
|
|
|
53
45
|
function cancelRequest() {
|
|
54
46
|
if (cancelSource?.abort) cancelSource.abort();
|
|
@@ -61,7 +53,7 @@ function useAsyncFetch(stringUrl, props = {}) {
|
|
|
61
53
|
}
|
|
62
54
|
}
|
|
63
55
|
|
|
64
|
-
async function sendRequest(
|
|
56
|
+
async function sendRequest() {
|
|
65
57
|
if (!stringUrl) {
|
|
66
58
|
throw new Error("URL is required.");
|
|
67
59
|
}
|
|
@@ -96,56 +88,61 @@ function useAsyncFetch(stringUrl, props = {}) {
|
|
|
96
88
|
|
|
97
89
|
if (!unmounted) {
|
|
98
90
|
if (pending) setPending2(true);
|
|
91
|
+
|
|
99
92
|
if (!pending) setPending(true);
|
|
93
|
+
|
|
100
94
|
setError();
|
|
95
|
+
|
|
101
96
|
cancelRequest();
|
|
97
|
+
|
|
102
98
|
setCancelSource(controller);
|
|
99
|
+
|
|
103
100
|
if (onStart) onStart();
|
|
104
101
|
}
|
|
105
102
|
|
|
106
|
-
const
|
|
107
|
-
|
|
108
|
-
let parsedResponse = cachedResponse;
|
|
103
|
+
const response = await fetch(url, fetchProps);
|
|
109
104
|
|
|
110
|
-
if (!
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
response: await response.text(),
|
|
119
|
-
}),
|
|
120
|
-
);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
parsedResponse = await response[parser]();
|
|
105
|
+
if (!response.ok) {
|
|
106
|
+
throw new Error(
|
|
107
|
+
JSON.stringify({
|
|
108
|
+
code: response.status,
|
|
109
|
+
text: response.statusText,
|
|
110
|
+
response: await response.text(),
|
|
111
|
+
}),
|
|
112
|
+
);
|
|
124
113
|
}
|
|
125
114
|
|
|
115
|
+
const parsedResponse = await response[parser]();
|
|
116
|
+
|
|
126
117
|
if (!unmounted) {
|
|
127
118
|
setData(parsedResponse);
|
|
119
|
+
|
|
128
120
|
if (onSuccess) onSuccess(parsedResponse);
|
|
129
|
-
if (!cachedResponse) cache.set(url, parsedResponse);
|
|
130
121
|
}
|
|
131
122
|
} catch (e) {
|
|
132
123
|
if (!unmounted && e.name !== "AbortError") {
|
|
133
124
|
let error;
|
|
125
|
+
|
|
134
126
|
try {
|
|
135
|
-
error = e.toString().replace("Error:", "");
|
|
136
|
-
error = JSON.parse(error.trim());
|
|
127
|
+
error = JSON.parse(e.toString().replace("Error:", "").trim());
|
|
137
128
|
} catch {
|
|
138
129
|
error = { response: e.toString(), text: e.toString() };
|
|
139
130
|
}
|
|
131
|
+
|
|
140
132
|
setError(error);
|
|
133
|
+
|
|
141
134
|
if (onFail) onFail(error);
|
|
142
135
|
}
|
|
143
136
|
} finally {
|
|
144
137
|
clearTimeout(requestTimeout);
|
|
138
|
+
|
|
145
139
|
if (!unmounted) {
|
|
146
140
|
if (pending) {
|
|
147
141
|
setPending2();
|
|
148
|
-
} else
|
|
142
|
+
} else {
|
|
143
|
+
setPending();
|
|
144
|
+
}
|
|
145
|
+
|
|
149
146
|
if (onFinish) onFinish();
|
|
150
147
|
}
|
|
151
148
|
}
|
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;
|