@kawaiininja/fetch 1.0.46 → 1.0.48

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.
@@ -27,11 +27,22 @@ export const useFetch = (endpoint, baseOptions = {}) => {
27
27
  const request = useCallback(async (params = {}) => {
28
28
  const isRead = !params.method || params.method === "GET";
29
29
  const currentCache = getCache(cacheKey);
30
- if (isRead && currentCache?.promise)
30
+ if (isRead && currentCache?.promise) {
31
+ // 🔌 TAP-IN: If a request is already flying (e.g. from StrictMode or another component),
32
+ // we must attach OUR state updater to it, otherwise we'll stay in 'loading: true' forever.
33
+ currentCache.promise.then((data) => safeSet(() => ({ data, status: 200, loading: false })), (err) => safeSet(() => ({
34
+ error: err.message || "Network Error",
35
+ status: null,
36
+ loading: false,
37
+ })));
31
38
  return currentCache.promise;
39
+ }
32
40
  if (!currentCache?.data)
33
41
  safeSet(() => ({ loading: true, error: null }));
34
42
  const execution = (async () => {
43
+ const reqId = cacheKey; // Simple ID for logging
44
+ if (debug)
45
+ console.log(`[useFetch] 🚀 Starting ${reqId}`);
35
46
  try {
36
47
  const token = await fetchCSRF();
37
48
  const finalUrl = apiUrl(params.url || endpoint);
@@ -41,17 +52,28 @@ export const useFetch = (endpoint, baseOptions = {}) => {
41
52
  throw new Error(parsed?.message || res.statusText);
42
53
  if (isRead)
43
54
  setCache(cacheKey, parsed);
55
+ if (debug)
56
+ console.log(`[useFetch] ✅ Success ${reqId}`, {
57
+ mounted: mounted.current,
58
+ data: parsed,
59
+ });
44
60
  safeSet(() => ({ data: parsed, status: res.status, loading: false }));
45
61
  return parsed;
46
62
  }
47
63
  catch (err) {
48
64
  if (err.name !== "AbortError") {
65
+ if (debug)
66
+ console.error(`[useFetch] ❌ Error ${reqId}`, err);
49
67
  const msg = err.message || "Network error";
50
68
  safeSet(() => ({ error: msg, status: null, loading: false }));
51
69
  if (onError)
52
70
  onError(msg, null);
53
71
  throw err;
54
72
  }
73
+ else {
74
+ if (debug)
75
+ console.warn(`[useFetch] 🛑 Aborted ${reqId}`);
76
+ }
55
77
  }
56
78
  finally {
57
79
  if (isRead) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kawaiininja/fetch",
3
- "version": "1.0.46",
3
+ "version": "1.0.48",
4
4
  "description": "Core fetch utility for Onyx Framework",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",