Files
supabase/apps/studio/data/database/backups-query.ts
Kevin Grüneberg 4532286e04 fix: align with API types (#34821)
* fix: align with API types

* Update new-project.constants.ts
2025-04-08 17:17:35 +08:00

43 lines
1.4 KiB
TypeScript

import { useQuery, UseQueryOptions } from '@tanstack/react-query'
import type { components } from 'data/api'
import { get, handleError } from 'data/fetchers'
import { useIsOrioleDb } from 'hooks/misc/useSelectedProject'
import type { ResponseError } from 'types'
import { databaseKeys } from './keys'
export type BackupsVariables = {
projectRef?: string
}
export type DatabaseBackup = components['schemas']['BackupsResponse']['backups'][number]
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) handleError(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> = {}
) => {
// [Joshen] Check for specifically false to account for project not loaded yet
const isOrioleDb = useIsOrioleDb()
return useQuery<BackupsData, BackupsError, TData>(
databaseKeys.backups(projectRef),
({ signal }) => getBackups({ projectRef }, signal),
{ enabled: enabled && isOrioleDb === false && typeof projectRef !== 'undefined', ...options }
)
}