added useApi and useApiQuery to work seamlessly with SWR (tbc)

This commit is contained in:
dswbx
2024-12-12 10:37:52 +01:00
parent 43ec075a32
commit 9d9aa7b7a5
10 changed files with 72 additions and 24 deletions

View File

@@ -6,5 +6,6 @@ export {
useBaseUrl
} from "./ClientProvider";
export { useApi, useApiQuery } from "./use-api";
export { useAuth } from "./schema/auth/use-auth";
export { Api } from "../../Api";

View File

@@ -0,0 +1,18 @@
import type { Api } from "Api";
import type { FetchPromise } from "modules/ModuleApi";
import useSWR, { type SWRConfiguration } from "swr";
import { useClient } from "ui/client/ClientProvider";
export const useApi = () => {
const client = useClient();
return client.api;
};
export const useApiQuery = <T = any>(
fn: (api: Api) => FetchPromise<T>,
options?: SWRConfiguration & { enabled?: boolean }
) => {
const api = useApi();
const p = fn(api);
return useSWR<T>(options?.enabled === false ? null : p.getKey(), () => p, options);
};