async-fetch 0.2.3 → 0.2.6
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 +4 -0
- package/package.json +2 -1
- package/useAsyncFetch.js +44 -31
package/README.md
CHANGED
|
@@ -77,3 +77,7 @@ The minimum requirement for the hook is a url string as the first argument. The
|
|
|
77
77
|
| data | Any | The response data. |
|
|
78
78
|
| sendRequest | Function | Function to send the request manually. |
|
|
79
79
|
| cancelRequest | Function | Function to cancel the request manually. |
|
|
80
|
+
|
|
81
|
+
### Next.js
|
|
82
|
+
|
|
83
|
+
For the hook to work properly in a Next.js project you must turn `reactStrictMode` to false in your `next.config.js`.
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "async-fetch",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.6",
|
|
4
4
|
"description": "Use fetch asynchronously for requests within React components.",
|
|
5
5
|
"main": "useAsyncFetch.js",
|
|
6
|
+
"type": "module",
|
|
6
7
|
"scripts": {
|
|
7
8
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
9
|
},
|
package/useAsyncFetch.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useState, useEffect } from "react";
|
|
2
|
-
import useInterval from "./useInterval";
|
|
2
|
+
import useInterval from "./useInterval.js";
|
|
3
3
|
|
|
4
4
|
function useAsyncFetch(url, props = {}) {
|
|
5
5
|
const {
|
|
@@ -11,9 +11,9 @@ function useAsyncFetch(url, props = {}) {
|
|
|
11
11
|
ignoreCleanup,
|
|
12
12
|
ignoreRequest,
|
|
13
13
|
timeout = 30000,
|
|
14
|
+
data: data2,
|
|
14
15
|
query,
|
|
15
16
|
params,
|
|
16
|
-
data: data2,
|
|
17
17
|
parser = "json",
|
|
18
18
|
onStart,
|
|
19
19
|
onSuccess,
|
|
@@ -24,6 +24,8 @@ function useAsyncFetch(url, props = {}) {
|
|
|
24
24
|
|
|
25
25
|
const [pending, setPending] = useState(initialPending);
|
|
26
26
|
|
|
27
|
+
const [pending2, setPending2] = useState();
|
|
28
|
+
|
|
27
29
|
const [data, setData] = useState(initialData);
|
|
28
30
|
|
|
29
31
|
const [error, setError] = useState(initialError);
|
|
@@ -44,7 +46,7 @@ function useAsyncFetch(url, props = {}) {
|
|
|
44
46
|
sendRequest();
|
|
45
47
|
}, poll);
|
|
46
48
|
|
|
47
|
-
function cancelRequest(
|
|
49
|
+
function cancelRequest() {
|
|
48
50
|
if (cancelSource?.abort) cancelSource.abort();
|
|
49
51
|
}
|
|
50
52
|
|
|
@@ -60,16 +62,16 @@ function useAsyncFetch(url, props = {}) {
|
|
|
60
62
|
|
|
61
63
|
if (typeof url !== "string") throw new Error("URL must be of type string.");
|
|
62
64
|
|
|
63
|
-
|
|
65
|
+
if (ignoreRequest !== true) {
|
|
66
|
+
const controller = new AbortController();
|
|
64
67
|
|
|
65
|
-
|
|
68
|
+
fetchProps.signal = controller.signal;
|
|
66
69
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
+
const requestTimeout = setTimeout(() => {
|
|
71
|
+
controller.abort();
|
|
72
|
+
}, timeout);
|
|
70
73
|
|
|
71
|
-
|
|
72
|
-
if (ignoreRequest !== true) {
|
|
74
|
+
try {
|
|
73
75
|
const contentType =
|
|
74
76
|
fetchProps.headers?.["Content-Type"] ||
|
|
75
77
|
fetchProps.headers?.["content-type"];
|
|
@@ -86,8 +88,10 @@ function useAsyncFetch(url, props = {}) {
|
|
|
86
88
|
|
|
87
89
|
if (!unmounted) {
|
|
88
90
|
if (onStart) onStart();
|
|
89
|
-
if (
|
|
90
|
-
|
|
91
|
+
if (pending) {
|
|
92
|
+
setPending2(true);
|
|
93
|
+
} else setPending(true);
|
|
94
|
+
setError();
|
|
91
95
|
cancelRequest();
|
|
92
96
|
setCancelSource(controller);
|
|
93
97
|
}
|
|
@@ -106,31 +110,40 @@ function useAsyncFetch(url, props = {}) {
|
|
|
106
110
|
const parsedResponse = await response[parser]();
|
|
107
111
|
|
|
108
112
|
if (!unmounted) {
|
|
109
|
-
setCancelSource();
|
|
110
|
-
setData(parsedResponse);
|
|
111
113
|
if (onSuccess) onSuccess(parsedResponse);
|
|
114
|
+
setData(parsedResponse);
|
|
115
|
+
}
|
|
116
|
+
} catch (e) {
|
|
117
|
+
if (!unmounted && e.name !== "AbortError") {
|
|
118
|
+
let error;
|
|
119
|
+
try {
|
|
120
|
+
error = e.toString().replace("Error:", "");
|
|
121
|
+
error = JSON.parse(error.trim());
|
|
122
|
+
} catch {
|
|
123
|
+
error = { response: e.toString(), text: e.toString() };
|
|
124
|
+
}
|
|
125
|
+
if (onFail) onFail(error);
|
|
126
|
+
setError(error);
|
|
127
|
+
}
|
|
128
|
+
} finally {
|
|
129
|
+
clearTimeout(requestTimeout);
|
|
130
|
+
if (!unmounted) {
|
|
131
|
+
if (onFinish) onFinish();
|
|
132
|
+
if (pending) {
|
|
133
|
+
setPending2();
|
|
134
|
+
} else setPending();
|
|
112
135
|
}
|
|
113
|
-
}
|
|
114
|
-
} catch (error) {
|
|
115
|
-
if (!unmounted && error.name !== "AbortError") {
|
|
116
|
-
let errorJson;
|
|
117
|
-
try {
|
|
118
|
-
errorJson = error.toString().replace("Error:", "");
|
|
119
|
-
errorJson = JSON.parse(errorJson.trim());
|
|
120
|
-
} catch {}
|
|
121
|
-
setError(errorJson || error);
|
|
122
|
-
if (onFail) onFail(errorJson || error);
|
|
123
|
-
}
|
|
124
|
-
} finally {
|
|
125
|
-
clearTimeout(requestTimeout);
|
|
126
|
-
if (!unmounted) {
|
|
127
|
-
if (setPending) setPending();
|
|
128
|
-
if (onFinish) onFinish();
|
|
129
136
|
}
|
|
130
137
|
}
|
|
131
138
|
}
|
|
132
139
|
|
|
133
|
-
return {
|
|
140
|
+
return {
|
|
141
|
+
pending: pending || pending2,
|
|
142
|
+
data,
|
|
143
|
+
error,
|
|
144
|
+
sendRequest,
|
|
145
|
+
cancelRequest,
|
|
146
|
+
};
|
|
134
147
|
}
|
|
135
148
|
|
|
136
149
|
export default useAsyncFetch;
|