Files
supabase/apps/studio/data/config/pgbouncer-enabled-query.ts
Joshen LimandIvan Vasilov 163263c3c5 First round of wrapping RQ errors with handleError (#26384)
* 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.

---------

Co-authored-by: Ivan Vasilov <vasilov.ivan@gmail.com>
2024-05-17 16:30:55 +08:00

39 lines
1.4 KiB
TypeScript

import { useQuery, UseQueryOptions } from '@tanstack/react-query'
import { get, handleError } from 'data/fetchers'
import { IS_PLATFORM } from 'lib/constants'
import type { ResponseError } from 'types'
import { configKeys } from './keys'
type PgBouncerVariables = {
projectRef?: string
}
// This API call describes whether PgBouncer is still present on the instance. Eventually PgBouncer will be removed.
export async function getPgBouncerStatus({ projectRef }: PgBouncerVariables, signal?: AbortSignal) {
if (projectRef === undefined) throw new Error('Project ref is required')
const { data, error } = await get('/platform/projects/{ref}/config/pgbouncer/status', {
params: { path: { ref: projectRef } },
})
if (error) handleError(error)
return data
}
export type PgBouncerStatusData = Awaited<ReturnType<typeof getPgBouncerStatus>>
export type PgBouncerStatusError = ResponseError
export const usePgBouncerStatus = <TData = PgBouncerStatusData>(
{ projectRef }: PgBouncerVariables,
{ enabled, ...options }: UseQueryOptions<PgBouncerStatusData, PgBouncerStatusError, TData> = {}
) =>
useQuery<PgBouncerStatusData, PgBouncerStatusError, TData>(
configKeys.pgBouncerStatus(projectRef),
({ signal }) => getPgBouncerStatus({ projectRef }, signal),
{
enabled: enabled && IS_PLATFORM && typeof projectRef !== 'undefined',
...options,
}
)