generic functions for backend queries
This commit is contained in:
@@ -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;
|
||||
Reference in New Issue
Block a user