generic functions for backend queries

This commit is contained in:
Aarni Halinen
2021-09-04 18:02:29 +03:00
parent d48c6a0c3e
commit efd916a8a2
13 changed files with 200 additions and 230 deletions
+27
View File
@@ -0,0 +1,27 @@
import useSWR from "swr";
import { APIPath, getBackendAPI } from "@api/backend";
function useFetchBackend<DataType>({
apiPath: path,
fallbackData,
options,
}: {
apiPath: APIPath,
fallbackData?: DataType,
options?: {
limit?: number;
auth?: boolean;
}
}): {
data?: DataType,
error?: any
} {
const fetcher = (limit: number, authenticated: boolean) => getBackendAPI<DataType>({ path, queryParams: { limit }, authenticated });
const { data, error } = useSWR([options?.limit, options?.auth], fetcher, { fallbackData });
return {
data,
error,
};
}
export default useFetchBackend;