@kawaiininja/fetch 1.0.22 → 1.0.24
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 +11 -5
- 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
|
@@ -64,6 +64,11 @@ export const useFetch = (endpoint, baseOptions = {}) => {
|
|
|
64
64
|
});
|
|
65
65
|
}
|
|
66
66
|
}, [endpoint, debug]);
|
|
67
|
+
// 🛡️ INTERNAL STABILITY: Decouple from context identity
|
|
68
|
+
const apiUrlRef = useRef(apiUrl);
|
|
69
|
+
useEffect(() => {
|
|
70
|
+
apiUrlRef.current = apiUrl;
|
|
71
|
+
}, [apiUrl]);
|
|
67
72
|
const request = useCallback(async (params = {}) => {
|
|
68
73
|
// 🛡️ RECURSION GUARD: Instant rejection of concurrent loops
|
|
69
74
|
if (isRequestingRef.current) {
|
|
@@ -72,7 +77,8 @@ export const useFetch = (endpoint, baseOptions = {}) => {
|
|
|
72
77
|
return;
|
|
73
78
|
}
|
|
74
79
|
const { url = endpoint, method = "GET", body, headers, parseAs = "auto", ...rest } = params;
|
|
75
|
-
|
|
80
|
+
// Use Ref to prevent re-creation of request when Context changes
|
|
81
|
+
const finalUrl = url.startsWith("http") ? url : apiUrlRef.current(url);
|
|
76
82
|
if (!finalUrl)
|
|
77
83
|
return;
|
|
78
84
|
isRequestingRef.current = true;
|
|
@@ -81,7 +87,7 @@ export const useFetch = (endpoint, baseOptions = {}) => {
|
|
|
81
87
|
try {
|
|
82
88
|
let token = await fetchCSRF();
|
|
83
89
|
// Merge baseOptions from Ref to maintain identity stability
|
|
84
|
-
let res = await performFetch(finalUrl, method, token, body, headers, { ...optionsRef.current, ...rest }, debug, abortRef.current.signal,
|
|
90
|
+
let res = await performFetch(finalUrl, method, token, body, headers, { ...optionsRef.current, ...rest }, debug, abortRef.current.signal, apiUrlRef.current);
|
|
85
91
|
lastStatus = res.status;
|
|
86
92
|
// Auto-retry on 403 CSRF Error
|
|
87
93
|
if (res.status === 403) {
|
|
@@ -92,7 +98,7 @@ export const useFetch = (endpoint, baseOptions = {}) => {
|
|
|
92
98
|
if (bodyClone?.code === "CSRF_ERROR" || bodyClone?.message?.includes("CSRF")) {
|
|
93
99
|
await clearCsrf();
|
|
94
100
|
token = await fetchCSRF();
|
|
95
|
-
res = await performFetch(finalUrl, method, token, body, headers, { ...optionsRef.current, ...rest }, debug, abortRef.current.signal,
|
|
101
|
+
res = await performFetch(finalUrl, method, token, body, headers, { ...optionsRef.current, ...rest }, debug, abortRef.current.signal, apiUrlRef.current);
|
|
96
102
|
lastStatus = res.status;
|
|
97
103
|
}
|
|
98
104
|
}
|
|
@@ -115,8 +121,8 @@ export const useFetch = (endpoint, baseOptions = {}) => {
|
|
|
115
121
|
isRequestingRef.current = false;
|
|
116
122
|
}
|
|
117
123
|
},
|
|
118
|
-
//
|
|
119
|
-
[endpoint, fetchCSRF, clearCsrf,
|
|
124
|
+
// apiUrl REMOVED specific dependency to stop context-based invalidations
|
|
125
|
+
[endpoint, fetchCSRF, clearCsrf, safeSet, onError, debug]);
|
|
120
126
|
useEffect(() => {
|
|
121
127
|
mounted.current = true;
|
|
122
128
|
return () => {
|