@kawaiininja/fetch 1.0.15 → 1.0.16

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.
@@ -4,7 +4,7 @@ import { INTERNAL_HEADER } from "../context/ApiContext";
4
4
  import { isNative } from "./platform";
5
5
  import { useApiConfig } from "./useApiConfig";
6
6
  import { useCsrf } from "./useCsrf";
7
- import { createApiSurface } from "./utils";
7
+ import { createApiMethods } from "./utils";
8
8
  /**
9
9
  * 🛡️ SESSION MEMORY VAULT
10
10
  * Caches native tokens in memory for the lifetime of the JS process.
@@ -292,5 +292,17 @@ export const useFetch = (endpoint, baseOptions = {}) => {
292
292
  };
293
293
  }, [debug, endpoint]);
294
294
  // 🏹 STABLE API SURFACE
295
- return useMemo(() => createApiSurface({ state, request, baseUrl: resolvedUrl, safeSet }), [state, request, resolvedUrl, safeSet]);
295
+ // 1. Memoize methods independently of state to prevent re-render loops
296
+ // in components that use these methods as dependencies.
297
+ const methods = useMemo(() => createApiMethods({ request, baseUrl: resolvedUrl, safeSet }), [request, resolvedUrl, safeSet]);
298
+ // 2. Merge state with methods
299
+ return useMemo(() => ({
300
+ // 🔍 state (Reactive)
301
+ state,
302
+ isLoading: state.loading,
303
+ isError: !!state.error,
304
+ isSuccess: !!state.data && !state.error,
305
+ // 🔁 core & methods (Stable)
306
+ ...methods,
307
+ }), [state, methods]);
296
308
  };
@@ -30,9 +30,26 @@ interface CreateApiSurfaceParams<T> {
30
30
  baseUrl: string;
31
31
  safeSet: (fn: (prev: FetchState<T>) => Partial<FetchState<T>>) => void;
32
32
  }
33
+ /**
34
+ * Helper to construct the STABLE methods of the API surface.
35
+ */
36
+ export declare const createApiMethods: <T = any>({ request, baseUrl, safeSet, }: Omit<CreateApiSurfaceParams<T>, "state">) => {
37
+ request: <R = T>(options?: RequestOptions) => Promise<R | undefined>;
38
+ refetch: () => Promise<T | undefined>;
39
+ setData: (value: T | null | ((prev: T | null) => T | null)) => void;
40
+ updateData: (partial: Partial<T> | ((prev: T | null) => Partial<T>)) => void;
41
+ get: <R = T>(config?: RequestOptions) => Promise<R | undefined>;
42
+ post: <D = any, R = T>(data?: D, config?: RequestOptions) => Promise<R | undefined>;
43
+ put: <D = any, R_1 = T>(data?: D, config?: RequestOptions) => Promise<R_1 | undefined>;
44
+ patch: <D = any, R_2 = T>(data?: D, config?: RequestOptions) => Promise<R_2 | undefined>;
45
+ delete: <R_3 = T>(config?: RequestOptions) => Promise<R_3 | undefined>;
46
+ json: <D = any, R_3 = T>(data?: D, config?: RequestOptions) => Promise<R_3 | undefined>;
47
+ text: (config?: RequestOptions) => Promise<string | undefined>;
48
+ blob: (config?: RequestOptions) => Promise<Blob | undefined>;
49
+ upload: <R_4 = T>(formData: FormData, config?: RequestOptions) => Promise<R_4 | undefined>;
50
+ };
33
51
  /**
34
52
  * Helper to construct the API surface object.
35
- * Extracts the massive object creation from the main hook to keep it clean.
36
53
  */
37
54
  export declare const createApiSurface: <T = any>({ state, request, baseUrl, safeSet, }: CreateApiSurfaceParams<T>) => ApiSurface<T>;
38
55
  export {};
@@ -1,8 +1,7 @@
1
1
  /**
2
- * Helper to construct the API surface object.
3
- * Extracts the massive object creation from the main hook to keep it clean.
2
+ * Helper to construct the STABLE methods of the API surface.
4
3
  */
5
- export const createApiSurface = ({ state, request, baseUrl, safeSet, }) => {
4
+ export const createApiMethods = ({ request, baseUrl, safeSet, }) => {
6
5
  // little helper for JSON methods
7
6
  const withJsonBody = (data) => ({
8
7
  body: data != null ? JSON.stringify(data) : undefined,
@@ -10,11 +9,6 @@ export const createApiSurface = ({ state, request, baseUrl, safeSet, }) => {
10
9
  parseAs: "json",
11
10
  });
12
11
  return {
13
- // 🔍 state
14
- state,
15
- isLoading: state.loading,
16
- isError: !!state.error,
17
- isSuccess: !!state.data && !state.error,
18
12
  // 🔁 core
19
13
  request,
20
14
  refetch: () => request({ url: baseUrl }),
@@ -29,7 +23,9 @@ export const createApiSurface = ({ state, request, baseUrl, safeSet, }) => {
29
23
  updateData: (partial) => safeSet((prev) => ({
30
24
  data: {
31
25
  ...prev.data,
32
- ...(typeof partial === "function" ? partial(prev.data) : partial),
26
+ ...(typeof partial === "function"
27
+ ? partial(prev.data)
28
+ : partial),
33
29
  },
34
30
  })),
35
31
  // 🗡 CRUD methods
@@ -102,3 +98,17 @@ export const createApiSurface = ({ state, request, baseUrl, safeSet, }) => {
102
98
  }),
103
99
  };
104
100
  };
101
+ /**
102
+ * Helper to construct the API surface object.
103
+ */
104
+ export const createApiSurface = ({ state, request, baseUrl, safeSet, }) => {
105
+ const methods = createApiMethods({ request, baseUrl, safeSet });
106
+ return {
107
+ // 🔍 state
108
+ state,
109
+ isLoading: state.loading,
110
+ isError: !!state.error,
111
+ isSuccess: !!state.data && !state.error,
112
+ ...methods,
113
+ };
114
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kawaiininja/fetch",
3
- "version": "1.0.15",
3
+ "version": "1.0.16",
4
4
  "description": "Core fetch utility for Onyx Framework",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",