@kawaiininja/fetch 1.0.52 → 1.0.53

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,6 +7,8 @@ export interface ApiConfig {
7
7
  version?: string;
8
8
  onError?: (error: string, status: number | null) => void;
9
9
  debug?: boolean;
10
+ timeout?: number;
11
+ retries?: number;
10
12
  }
11
13
  export interface ApiContextValue {
12
14
  baseUrl: string;
@@ -14,6 +16,8 @@ export interface ApiContextValue {
14
16
  apiUrl: (path: string) => string;
15
17
  onError?: (error: string, status: number | null) => void;
16
18
  debug: boolean;
19
+ timeout: number;
20
+ retries: number;
17
21
  }
18
22
  export declare const ApiContext: React.Context<ApiContextValue | null>;
19
23
  export declare function ApiProvider({ config, children, }: {
@@ -23,8 +23,17 @@ export function ApiProvider({ config, children, }) {
23
23
  baseUrl: config.baseUrl,
24
24
  version: config.version || "v1",
25
25
  debug: !!config.debug,
26
+ timeout: config.timeout || 5000,
27
+ retries: config.retries ?? 3,
26
28
  apiUrl,
27
29
  onError: (e, s) => configRef.current.onError?.(e, s),
28
- }), [config.baseUrl, config.version, config.debug, apiUrl]);
30
+ }), [
31
+ config.baseUrl,
32
+ config.version,
33
+ config.debug,
34
+ config.timeout,
35
+ config.retries,
36
+ apiUrl,
37
+ ]);
29
38
  return _jsx(ApiContext.Provider, { value: value, children: children });
30
39
  }
@@ -7,5 +7,7 @@ export interface SSSOptions extends RequestOptions {
7
7
  dedupeKey?: string;
8
8
  staleTime?: number;
9
9
  enabled?: boolean;
10
+ timeout?: number;
11
+ retries?: number;
10
12
  }
11
13
  export declare const useFetch: <T = any>(endpoint: string, baseOptions?: SSSOptions) => ApiSurface<T>;
@@ -5,11 +5,12 @@ 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 = {}) => {
8
- const { apiUrl, onError, debug } = useApiConfig();
8
+ const { apiUrl, onError, debug, timeout: globalTimeout, retries: globalRetries, } = useApiConfig();
9
9
  const { fetchCSRF } = useCsrf();
10
10
  const abortRef = useRef(new AbortController());
11
11
  const mounted = useRef(true);
12
- const { enabled = true, swr = false, staleTime = 300000 } = baseOptions;
12
+ const { enabled = true, swr = false, staleTime = 300000, timeout = globalTimeout, retries = globalRetries, } = baseOptions;
13
+ const mergedOptions = { ...baseOptions, timeout, retries };
13
14
  const cacheKey = useMemo(() => baseOptions.dedupeKey || endpoint, [endpoint, baseOptions.dedupeKey]);
14
15
  const cached = getCache(cacheKey);
15
16
  const [state, setState] = useState({
@@ -46,7 +47,7 @@ export const useFetch = (endpoint, baseOptions = {}) => {
46
47
  try {
47
48
  const token = await fetchCSRF();
48
49
  const finalUrl = apiUrl(params.url || endpoint);
49
- const res = await performFetch(finalUrl, params.method || "GET", token, params.body, params.headers, { ...baseOptions, ...params }, debug, abortRef.current.signal, apiUrl);
50
+ const res = await performFetch(finalUrl, params.method || "GET", token, params.body, params.headers, { ...mergedOptions, ...params }, debug, abortRef.current.signal, apiUrl);
50
51
  const parsed = await parseResponse(res, params.parseAs || "auto");
51
52
  if (!res.ok)
52
53
  throw new Error(parsed?.message || res.statusText);
@@ -127,7 +128,7 @@ export const useFetch = (endpoint, baseOptions = {}) => {
127
128
  // 🛡️ STRICT MODE FIX: Do not abort requests on unmount.
128
129
  // In React 18 Strict Mode, the first mount starts the request, then unmounts immediately.
129
130
  // If we abort here, the shared promise (used by the second mount) dies, leaving the app in 'loading: true'.
130
- // abortRef.current.abort();..
131
+ // abortRef.current.abort();
131
132
  };
132
133
  }, []);
133
134
  const methods = createApiMethods({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kawaiininja/fetch",
3
- "version": "1.0.52",
3
+ "version": "1.0.53",
4
4
  "description": "Core fetch utility for Onyx Framework",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",