async-fetch 0.2.9 → 0.3.0
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/package.json +3 -7
- package/useAsyncFetch.js +9 -15
package/package.json
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "async-fetch",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Use fetch for requests within React components.",
|
|
5
5
|
"main": "useAsyncFetch.js",
|
|
6
6
|
"type": "module",
|
|
7
|
-
"scripts": {
|
|
8
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
9
|
-
},
|
|
7
|
+
"scripts": {},
|
|
10
8
|
"repository": {
|
|
11
9
|
"type": "git",
|
|
12
10
|
"url": "git+https://github.com/nameer-rizvi/useAsyncFetch.git"
|
|
@@ -19,8 +17,6 @@
|
|
|
19
17
|
],
|
|
20
18
|
"author": "Nameer Rizvi (https://github.com/nameer-rizvi)",
|
|
21
19
|
"license": "ISC",
|
|
22
|
-
"bugs":
|
|
23
|
-
"url": "https://github.com/nameer-rizvi/useAsyncFetch/issues"
|
|
24
|
-
},
|
|
20
|
+
"bugs": "https://github.com/nameer-rizvi/useAsyncFetch/issues",
|
|
25
21
|
"homepage": "https://github.com/nameer-rizvi/useAsyncFetch#readme"
|
|
26
22
|
}
|
package/useAsyncFetch.js
CHANGED
|
@@ -2,7 +2,7 @@ import { useState, useEffect } from "react";
|
|
|
2
2
|
import useCache from "./useCache.js";
|
|
3
3
|
import useInterval from "./useInterval.js";
|
|
4
4
|
|
|
5
|
-
function useAsyncFetch(
|
|
5
|
+
function useAsyncFetch(requestURL, props = {}) {
|
|
6
6
|
const {
|
|
7
7
|
initialPending,
|
|
8
8
|
initialData,
|
|
@@ -62,14 +62,16 @@ function useAsyncFetch(path, props = {}) {
|
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
async function sendRequest(constant) {
|
|
65
|
-
if (!
|
|
65
|
+
if (!requestURL) {
|
|
66
66
|
throw new Error("URL is required.");
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
if (typeof
|
|
69
|
+
if (typeof requestURL !== "string") {
|
|
70
70
|
throw new Error("URL must be of type string.");
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
+
const url = new URL(requestURL);
|
|
74
|
+
|
|
73
75
|
if (ignoreRequest !== true) {
|
|
74
76
|
const controller = new AbortController();
|
|
75
77
|
|
|
@@ -80,12 +82,7 @@ function useAsyncFetch(path, props = {}) {
|
|
|
80
82
|
}, timeout);
|
|
81
83
|
|
|
82
84
|
try {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
if (query || params) {
|
|
86
|
-
if (!path.endsWith("?")) q += "?";
|
|
87
|
-
q += new URLSearchParams(query || params);
|
|
88
|
-
}
|
|
85
|
+
if (query || params) url.search = new URLSearchParams(query || params);
|
|
89
86
|
|
|
90
87
|
const contentType =
|
|
91
88
|
fetchProps.headers?.["Content-Type"] ||
|
|
@@ -98,18 +95,15 @@ function useAsyncFetch(path, props = {}) {
|
|
|
98
95
|
}
|
|
99
96
|
|
|
100
97
|
if (!unmounted) {
|
|
101
|
-
if (pending)
|
|
102
|
-
|
|
103
|
-
} else setPending(true);
|
|
98
|
+
if (pending) setPending2(true);
|
|
99
|
+
if (!pending) setPending(true);
|
|
104
100
|
setError();
|
|
105
101
|
cancelRequest();
|
|
106
102
|
setCancelSource(controller);
|
|
107
103
|
if (onStart) onStart();
|
|
108
104
|
}
|
|
109
105
|
|
|
110
|
-
const
|
|
111
|
-
|
|
112
|
-
const cachedResponse = constant === "USE_CACHE" && cache.get(url);
|
|
106
|
+
const cachedResponse = constant === "USE_CACHE" && cache.get(url.href);
|
|
113
107
|
|
|
114
108
|
let parsedResponse = cachedResponse;
|
|
115
109
|
|