import { useQuery, UseQueryOptions } from '@tanstack/react-query' import { get } from 'data/fetchers' import type { ResponseError } from 'types' import { databaseKeys } from './keys' import type { components } from 'data/api' export type BackupsVariables = { projectRef?: string } export type DatabaseBackup = components['schemas']['Backup'] export async function getBackups({ projectRef }: BackupsVariables, signal?: AbortSignal) { if (!projectRef) throw new Error('Project ref is required') const { data, error } = await get(`/platform/database/{ref}/backups`, { params: { path: { ref: projectRef } }, signal, }) if (error) throw error return data } export type BackupsData = Awaited> export type BackupsError = ResponseError export const useBackupsQuery = ( { projectRef }: BackupsVariables, { enabled = true, ...options }: UseQueryOptions = {} ) => useQuery( databaseKeys.backups(projectRef), ({ signal }) => getBackups({ projectRef }, signal), { enabled: enabled && typeof projectRef !== 'undefined', ...options } )