async-fetch 0.2.4 → 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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/useAsyncFetch.js +43 -30
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "async-fetch",
3
- "version": "0.2.4",
3
+ "version": "0.2.6",
4
4
  "description": "Use fetch asynchronously for requests within React components.",
5
5
  "main": "useAsyncFetch.js",
6
6
  "type": "module",
package/useAsyncFetch.js CHANGED
@@ -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(SOURCE) {
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
- const controller = new AbortController();
65
+ if (ignoreRequest !== true) {
66
+ const controller = new AbortController();
64
67
 
65
- fetchProps.signal = controller.signal;
68
+ fetchProps.signal = controller.signal;
66
69
 
67
- const requestTimeout = setTimeout(() => {
68
- controller.abort();
69
- }, timeout);
70
+ const requestTimeout = setTimeout(() => {
71
+ controller.abort();
72
+ }, timeout);
70
73
 
71
- try {
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 (setPending) setPending(true);
90
- if (setError) setError();
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 { pending, data, error, sendRequest, cancelRequest };
140
+ return {
141
+ pending: pending || pending2,
142
+ data,
143
+ error,
144
+ sendRequest,
145
+ cancelRequest,
146
+ };
134
147
  }
135
148
 
136
149
  export default useAsyncFetch;