mirror of
https://github.com/supabase/supabase.git
synced 2026-10-11 20:35:07 +03:00
* 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>
64 lines
1.7 KiB
TypeScript
64 lines
1.7 KiB
TypeScript
import { useQuery, useQueryClient, UseQueryOptions } from '@tanstack/react-query'
|
|
import { get, handleError } from 'data/fetchers'
|
|
import { API_URL } from 'lib/constants'
|
|
import { useCallback } from 'react'
|
|
import { storageKeys } from './keys'
|
|
import type { ResponseError } from 'types'
|
|
|
|
export type BucketsVariables = { projectRef?: string }
|
|
|
|
export type Bucket = {
|
|
id: string
|
|
name: string
|
|
owner: string
|
|
public: boolean
|
|
created_at: string
|
|
updated_at: string
|
|
file_size_limit: null | number
|
|
allowed_mime_types: null | string[]
|
|
}
|
|
|
|
export async function getBuckets({ projectRef }: BucketsVariables, signal?: AbortSignal) {
|
|
if (!projectRef) throw new Error('projectRef is required')
|
|
|
|
const { data, error } = await get('/platform/storage/{ref}/buckets', {
|
|
params: { path: { ref: projectRef } },
|
|
signal,
|
|
})
|
|
|
|
if (error) handleError(error)
|
|
return data as Bucket[]
|
|
}
|
|
|
|
export type BucketsData = Awaited<ReturnType<typeof getBuckets>>
|
|
export type BucketsError = ResponseError
|
|
|
|
export const useBucketsQuery = <TData = BucketsData>(
|
|
{ projectRef }: BucketsVariables,
|
|
{ enabled = true, ...options }: UseQueryOptions<BucketsData, BucketsError, TData> = {}
|
|
) =>
|
|
useQuery<BucketsData, BucketsError, TData>(
|
|
storageKeys.buckets(projectRef),
|
|
({ signal }) => getBuckets({ projectRef }, signal),
|
|
{
|
|
enabled: enabled && typeof projectRef !== 'undefined',
|
|
...options,
|
|
retry: (failureCount, error) => {
|
|
if (
|
|
typeof error === 'object' &&
|
|
error !== null &&
|
|
error.message.startsWith('Tenant config') &&
|
|
error.message.endsWith('not found')
|
|
) {
|
|
return false
|
|
}
|
|
|
|
if (failureCount < 3) {
|
|
return true
|
|
}
|
|
|
|
return false
|
|
},
|
|
}
|
|
)
|