@kawaiininja/fetch 1.0.44 → 1.0.46

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.
@@ -7,7 +7,10 @@ const MAX_RETRIES = 3;
7
7
  * Helper to prepare headers with Auth and CSRF
8
8
  */
9
9
  const prepareHeaders = async (token, headers, isNative, isInternal, debug) => {
10
+ const requestId = crypto.randomUUID();
10
11
  const config = {
12
+ "X-Request-ID": requestId,
13
+ "Idempotency-Key": requestId,
11
14
  ...(headers || {}),
12
15
  ...INTERNAL_HEADER,
13
16
  };
@@ -79,7 +82,8 @@ export const performFetch = async (url, method, token, body, headers, rest, debu
79
82
  };
80
83
  export const parseResponse = async (res, parseAs) => {
81
84
  const type = res.headers.get("content-type") || "";
82
- if (parseAs === "json" || (parseAs === "auto" && type.includes("application/json")))
85
+ if (parseAs === "json" ||
86
+ (parseAs === "auto" && type.includes("application/json")))
83
87
  return res.json();
84
88
  if (parseAs === "text")
85
89
  return res.text();
@@ -1,7 +1,7 @@
1
1
  import { useCallback, useEffect, useMemo, useRef, useState } from "react";
2
2
  import { useApiConfig } from "./useApiConfig";
3
3
  import { useCsrf } from "./useCsrf";
4
- import { getCache, isStale, setCache, setPromise } from "./useFetch.cache";
4
+ import { getCache, isStale, setCache } from "./useFetch.cache";
5
5
  import { parseResponse, performFetch } from "./useFetch.executor";
6
6
  import { createApiMethods } from "./utils";
7
7
  export const useFetch = (endpoint, baseOptions = {}) => {
@@ -21,11 +21,8 @@ export const useFetch = (endpoint, baseOptions = {}) => {
21
21
  const safeSet = useCallback((fn) => {
22
22
  if (!mounted.current)
23
23
  return;
24
- setTimeout(() => {
25
- // @ts-ignore
26
- if (mounted.current)
27
- setState((prev) => ({ ...prev, ...fn(prev) }));
28
- }, 0);
24
+ // Immediate update to prevent stale closures and lost updates
25
+ setState((prev) => ({ ...prev, ...fn(prev) }));
29
26
  }, []);
30
27
  const request = useCallback(async (params = {}) => {
31
28
  const isRead = !params.method || params.method === "GET";
@@ -65,7 +62,9 @@ export const useFetch = (endpoint, baseOptions = {}) => {
65
62
  }
66
63
  })();
67
64
  if (isRead) {
68
- setPromise(cacheKey, execution);
65
+ const entry = getCache(cacheKey);
66
+ if (entry)
67
+ entry.promise = execution;
69
68
  }
70
69
  return execution;
71
70
  }, [
@@ -76,7 +75,7 @@ export const useFetch = (endpoint, baseOptions = {}) => {
76
75
  onError,
77
76
  debug,
78
77
  safeSet,
79
- JSON.stringify(baseOptions),
78
+ baseOptions,
80
79
  ]);
81
80
  useEffect(() => {
82
81
  if (!enabled)
@@ -85,7 +84,7 @@ export const useFetch = (endpoint, baseOptions = {}) => {
85
84
  if (shouldFetch) {
86
85
  request();
87
86
  }
88
- }, [enabled, cacheKey, swr, staleTime, request]);
87
+ }, [enabled, cacheKey, swr, staleTime, request, cached?.data]);
89
88
  useEffect(() => {
90
89
  if (!baseOptions.revalidateOnFocus || !enabled)
91
90
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kawaiininja/fetch",
3
- "version": "1.0.44",
3
+ "version": "1.0.46",
4
4
  "description": "Core fetch utility for Onyx Framework",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",