Files
supabase/apps/studio/data/storage/storage-archive-query.ts
Ivan Vasilov 8b657165b5 chore: Migrate to use custom type for ReactQuery queries and mutations (#40073)
* Add custom types for queries, mutations and infinite queries.

* Migrate all queries to use the new type.

* Migrate all infinite queries to useCustomInfiniteQueryOptions.

* Migrate all mutations to use useCustomMutationOptions.

* Add type to all imports in `types` folder.
2025-11-03 13:18:13 +01:00

41 lines
1.3 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { get, handleError } from 'data/fetchers'
import type { ResponseError, UseCustomQueryOptions } from 'types'
import { storageKeys } from './keys'
type StorageArchiveVariables = { projectRef?: string }
async function fetchStorageArchive({ projectRef }: StorageArchiveVariables, signal?: AbortSignal) {
if (!projectRef) throw new Error('projectRef is required')
const { data, error } = await get('/platform/storage/{ref}/archive', {
params: { path: { ref: projectRef } },
signal,
})
if (error) {
if ((error as ResponseError)?.message?.includes('Storage archive not found')) {
return { fileUrl: undefined }
} else {
handleError(error)
}
}
return data
}
export type StorageArchiveData = Awaited<ReturnType<typeof fetchStorageArchive>>
export const useStorageArchiveQuery = <TData = StorageArchiveData>(
{ projectRef }: StorageArchiveVariables,
{
enabled = true,
...options
}: UseCustomQueryOptions<StorageArchiveData, ResponseError, TData> = {}
) =>
useQuery<StorageArchiveData, ResponseError, TData>({
queryKey: storageKeys.archive(projectRef),
queryFn: ({ signal }) => fetchStorageArchive({ projectRef }, signal),
enabled: enabled && typeof projectRef !== 'undefined',
...options,
})