@kawaiininja/fetch 1.0.41 → 1.0.43

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.
@@ -22,6 +22,7 @@ export const useFetch = (endpoint, baseOptions = {}) => {
22
22
  if (!mounted.current)
23
23
  return;
24
24
  setTimeout(() => {
25
+ // @ts-ignore
25
26
  if (mounted.current)
26
27
  setState((prev) => ({ ...prev, ...fn(prev) }));
27
28
  }, 0);
@@ -69,16 +70,7 @@ export const useFetch = (endpoint, baseOptions = {}) => {
69
70
  entry.promise = execution;
70
71
  }
71
72
  return execution;
72
- }, [
73
- endpoint,
74
- cacheKey,
75
- fetchCSRF,
76
- apiUrl,
77
- onError,
78
- debug,
79
- safeSet,
80
- baseOptions,
81
- ]);
73
+ }, [endpoint, cacheKey, fetchCSRF, apiUrl, onError, debug, safeSet, baseOptions]);
82
74
  useEffect(() => {
83
75
  if (!enabled)
84
76
  return;
@@ -86,7 +78,7 @@ export const useFetch = (endpoint, baseOptions = {}) => {
86
78
  if (shouldFetch) {
87
79
  request();
88
80
  }
89
- }, [enabled, cacheKey, swr, staleTime]);
81
+ }, [enabled, cacheKey, swr, staleTime, request, cached?.data]);
90
82
  useEffect(() => {
91
83
  if (!baseOptions.revalidateOnFocus || !enabled)
92
84
  return;
@@ -107,16 +99,19 @@ export const useFetch = (endpoint, baseOptions = {}) => {
107
99
  abortRef.current.abort();
108
100
  };
109
101
  }, []);
110
- const methods = useMemo(() => createApiMethods({
111
- request,
112
- baseUrl: apiUrl(endpoint),
113
- safeSet,
114
- }), [request, endpoint, apiUrl, safeSet]);
115
- return useMemo(() => ({
116
- state,
117
- isLoading: state.loading,
118
- isError: !!state.error,
119
- isSuccess: !!state.data && !state.error,
120
- ...methods,
121
- }), [state, methods]);
102
+ return useMemo(() => {
103
+ const methods = createApiMethods({
104
+ request,
105
+ baseUrl: apiUrl(endpoint),
106
+ safeSet,
107
+ apiUrl,
108
+ });
109
+ return {
110
+ state,
111
+ isLoading: state.loading,
112
+ isError: !!state.error,
113
+ isSuccess: !!state.data && !state.error,
114
+ ...methods,
115
+ };
116
+ }, [state, request, endpoint, apiUrl, safeSet]);
122
117
  };
@@ -18,13 +18,13 @@ export interface ApiSurface<T = any> {
18
18
  blob: (config?: RequestOptions) => Promise<Blob | undefined>;
19
19
  upload: <R = T>(formData: FormData, config?: RequestOptions) => Promise<R | undefined>;
20
20
  }
21
- interface CreateApiSurfaceParams<T> {
22
- state: FetchState<T>;
21
+ interface CreateApiMethodsParams<T> {
23
22
  request: <R = T>(options?: RequestOptions) => Promise<R | undefined>;
24
23
  baseUrl: string;
25
24
  safeSet: (fn: (prev: FetchState<T>) => Partial<FetchState<T>>) => void;
25
+ apiUrl: (path: string) => string;
26
26
  }
27
- export declare const createApiMethods: <T = any>({ request, baseUrl, safeSet, }: Omit<CreateApiSurfaceParams<T>, "state">) => {
27
+ export declare const createApiMethods: <T = any>({ request, baseUrl, safeSet, apiUrl, }: CreateApiMethodsParams<T>) => {
28
28
  request: <R = T>(options?: RequestOptions) => Promise<R | undefined>;
29
29
  refetch: () => Promise<T | undefined>;
30
30
  setData: (value: T | null | ((prev: T | null) => T | null)) => void;
@@ -39,5 +39,4 @@ export declare const createApiMethods: <T = any>({ request, baseUrl, safeSet, }:
39
39
  blob: (config?: RequestOptions) => Promise<Blob | undefined>;
40
40
  upload: <R_4 = T>(formData: FormData, config?: RequestOptions) => Promise<R_4 | undefined>;
41
41
  };
42
- export declare const createApiSurface: <T = any>({ state, request, baseUrl, safeSet, }: CreateApiSurfaceParams<T>) => ApiSurface<T>;
43
42
  export {};
@@ -1,6 +1,4 @@
1
- import { useApiConfig } from "./useApiConfig";
2
- export const createApiMethods = ({ request, baseUrl, safeSet, }) => {
3
- const { apiUrl } = useApiConfig();
1
+ export const createApiMethods = ({ request, baseUrl, safeSet, apiUrl, }) => {
4
2
  const withJsonBody = (data) => ({
5
3
  body: data != null ? JSON.stringify(data) : undefined,
6
4
  headers: { "Content-Type": "application/json" },
@@ -77,13 +75,3 @@ export const createApiMethods = ({ request, baseUrl, safeSet, }) => {
77
75
  }),
78
76
  };
79
77
  };
80
- export const createApiSurface = ({ state, request, baseUrl, safeSet, }) => {
81
- const methods = createApiMethods({ request, baseUrl, safeSet });
82
- return {
83
- state,
84
- isLoading: state.loading,
85
- isError: !!state.error,
86
- isSuccess: !!state.data && !state.error,
87
- ...methods,
88
- };
89
- };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kawaiininja/fetch",
3
- "version": "1.0.41",
3
+ "version": "1.0.43",
4
4
  "description": "Core fetch utility for Onyx Framework",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",