Files
supabase/apps/studio/data/database/backups-query.ts
2024-03-04 20:48:22 +08:00

38 lines
1.2 KiB
TypeScript

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<ReturnType<typeof getBackups>>
export type BackupsError = ResponseError
export const useBackupsQuery = <TData = BackupsData>(
{ projectRef }: BackupsVariables,
{ enabled = true, ...options }: UseQueryOptions<BackupsData, BackupsError, TData> = {}
) =>
useQuery<BackupsData, BackupsError, TData>(
databaseKeys.backups(projectRef),
({ signal }) => getBackups({ projectRef }, signal),
{ enabled: enabled && typeof projectRef !== 'undefined', ...options }
)