@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.
@@ -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
- // Ensure valid config structure
18
- const baseUrl = config?.baseUrl || "";
19
- const version = config?.version || "v1";
20
- const debug = !!config?.debug;
21
- const onError = config?.onError;
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.
@@ -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
- const finalUrl = url.startsWith("http") ? url : apiUrl(url);
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, apiUrl);
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, apiUrl);
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
- // baseOptions REMOVED from dependencies to prevent infinite request recreation
119
- [endpoint, fetchCSRF, clearCsrf, apiUrl, safeSet, onError, debug]);
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 () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kawaiininja/fetch",
3
- "version": "1.0.22",
3
+ "version": "1.0.24",
4
4
  "description": "Core fetch utility for Onyx Framework",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",