@kawaiininja/fetch 1.0.21 → 1.0.23
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/dist/context/ApiContext.js +22 -5
- package/dist/hooks/useFetch.js +25 -7
- package/package.json +1 -1
|
@@ -14,11 +14,28 @@ export const ApiContext = createContext(null);
|
|
|
14
14
|
* Constructs full API URLs from relative paths using the baseUrl.
|
|
15
15
|
*/
|
|
16
16
|
export function ApiProvider({ config, children }) {
|
|
17
|
-
//
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
17
|
+
// 🛡️ STABILITY: Hash the config to detect REAL changes vs identity shifts
|
|
18
|
+
const configHash = useMemo(() => {
|
|
19
|
+
try {
|
|
20
|
+
return JSON.stringify({
|
|
21
|
+
baseUrl: config?.baseUrl,
|
|
22
|
+
version: config?.version,
|
|
23
|
+
debug: config?.debug,
|
|
24
|
+
// We can't stringify functions, so we use their existence/identity
|
|
25
|
+
hasOnError: !!config?.onError,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return Math.random();
|
|
30
|
+
}
|
|
31
|
+
}, [config?.baseUrl, config?.version, config?.debug, config?.onError]);
|
|
32
|
+
// Extract stable values based on the hash
|
|
33
|
+
const { baseUrl, version, debug, onError } = useMemo(() => ({
|
|
34
|
+
baseUrl: config?.baseUrl || "",
|
|
35
|
+
version: config?.version || "v1",
|
|
36
|
+
debug: !!config?.debug,
|
|
37
|
+
onError: config?.onError,
|
|
38
|
+
}), [configHash]);
|
|
22
39
|
/**
|
|
23
40
|
* 🛡️ HYPER-STABLE URL CONSTRUCTOR
|
|
24
41
|
* Memoized independently of the context value to prevent downstream loop invalidation.
|
package/dist/hooks/useFetch.js
CHANGED
|
@@ -15,14 +15,21 @@ export const useFetch = (endpoint, baseOptions = {}) => {
|
|
|
15
15
|
const abortRef = useRef(new AbortController());
|
|
16
16
|
const mounted = useRef(true);
|
|
17
17
|
const isRequestingRef = useRef(false);
|
|
18
|
-
// 🛡️ STABILITY:
|
|
19
|
-
// We use a Ref to store the options so they don't trigger the request callback recreation
|
|
18
|
+
// 🛡️ STABILITY: Options hashing to prevent unnecessary effect triggers from object literals
|
|
20
19
|
const optionsRef = useRef(baseOptions);
|
|
20
|
+
const optionsHash = useMemo(() => {
|
|
21
|
+
try {
|
|
22
|
+
return JSON.stringify(baseOptions);
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
return Math.random();
|
|
26
|
+
}
|
|
27
|
+
}, [baseOptions]);
|
|
21
28
|
useEffect(() => {
|
|
22
29
|
if (debug)
|
|
23
|
-
console.log(`[useFetch] [${endpoint}]
|
|
30
|
+
console.log(`[useFetch] [${endpoint}] ⚙️ baseOptions updated`);
|
|
24
31
|
optionsRef.current = baseOptions;
|
|
25
|
-
}, [
|
|
32
|
+
}, [optionsHash, endpoint, debug]);
|
|
26
33
|
if (debug)
|
|
27
34
|
console.log(`[useFetch] [${endpoint}] 🔄 Hook Render`);
|
|
28
35
|
const [state, setState] = useState({
|
|
@@ -31,20 +38,31 @@ export const useFetch = (endpoint, baseOptions = {}) => {
|
|
|
31
38
|
error: null,
|
|
32
39
|
status: null,
|
|
33
40
|
});
|
|
41
|
+
// 🛡️ RECURSION GUARD: Prevent synchronous state-update cascades
|
|
42
|
+
const isUpdatingRef = useRef(false);
|
|
34
43
|
const safeSet = useCallback((fn) => {
|
|
35
|
-
if (mounted.current)
|
|
44
|
+
if (!mounted.current)
|
|
45
|
+
return;
|
|
46
|
+
if (isUpdatingRef.current)
|
|
47
|
+
return; // Block synchronous recursion
|
|
48
|
+
isUpdatingRef.current = true;
|
|
49
|
+
try {
|
|
36
50
|
setState((prev) => {
|
|
37
51
|
const updates = fn(prev);
|
|
38
52
|
if (debug) {
|
|
39
53
|
console.groupCollapsed(`[useFetch] [${endpoint}] 🔄 State Update`);
|
|
40
|
-
console.log("Current State:", prev);
|
|
41
54
|
console.log("Updates:", updates);
|
|
42
|
-
console.trace("Stack Trace");
|
|
43
55
|
console.groupEnd();
|
|
44
56
|
}
|
|
45
57
|
return { ...prev, ...updates };
|
|
46
58
|
});
|
|
47
59
|
}
|
|
60
|
+
finally {
|
|
61
|
+
// Use a microtask to release the lock after the current execution frame
|
|
62
|
+
Promise.resolve().then(() => {
|
|
63
|
+
isUpdatingRef.current = false;
|
|
64
|
+
});
|
|
65
|
+
}
|
|
48
66
|
}, [endpoint, debug]);
|
|
49
67
|
const request = useCallback(async (params = {}) => {
|
|
50
68
|
// 🛡️ RECURSION GUARD: Instant rejection of concurrent loops
|