Files
supabase/apps/studio/data/storage/s3-access-key-query.ts
Joshen LimandIvan Vasilov bb6349f34f Second round of wrapping RQ errors with handleError (#26428)
* First round of wrapping RQ errors with handleError

* Remove the throw before the handleError usage.

* Make the handling of an API error more versatile. Add logging in Sentry if the error is of unknown type.

* Remove throwing of the handleError function.

* Add return type to the handleError function to be never so that we're sure it always throws.

* Second round of wrapping RQ errors with handleError

* Temp fix in delete credential mutation, and fix loading state

---------

Co-authored-by: Ivan Vasilov <vasilov.ivan@gmail.com>
2024-05-21 15:51:11 +08:00

44 lines
1.4 KiB
TypeScript

import { UseQueryOptions, useQuery } from '@tanstack/react-query'
import { get, handleError } from 'data/fetchers'
import { ResponseError } from 'types'
import { storageCredentialsKeys } from './s3-access-key-keys'
type StorageCredentialsVariables = { projectRef?: string }
async function fetchStorageCredentials(
{ projectRef }: StorageCredentialsVariables,
signal?: AbortSignal
) {
if (!projectRef) throw new Error('projectRef is required')
const { data, error } = await get('/platform/storage/{ref}/credentials', {
params: { path: { ref: projectRef } },
signal,
})
if (error) handleError(error)
// Generated types by openapi are wrong so we need to cast it.
return data as unknown as {
data: {
id: string
created_at: string
access_key: string
description: string
}[]
}
}
export type StorageCredentialsData = Awaited<ReturnType<typeof fetchStorageCredentials>>
export const useStorageCredentialsQuery = <TData = StorageCredentialsData>(
{ projectRef }: StorageCredentialsVariables,
{ enabled = true, ...options }: UseQueryOptions<StorageCredentialsData, ResponseError, TData> = {}
) =>
useQuery<StorageCredentialsData, ResponseError, TData>(
storageCredentialsKeys.credentials(projectRef),
({ signal }) => fetchStorageCredentials({ projectRef }, signal),
{ enabled: enabled && typeof projectRef !== 'undefined', ...options }
)