28 lines
623 B
TypeScript
28 lines
623 B
TypeScript
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;
|