Files
supabase/apps/studio/data/database-publications/database-publications-query.ts
Joshen Lim 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

55 lines
1.6 KiB
TypeScript

import { UseQueryOptions, useQuery } from '@tanstack/react-query'
import { get, handleError } from 'data/fetchers'
import type { ResponseError } from 'types'
import { databasePublicationsKeys } from './keys'
export type DatabasePublicationsVariables = {
projectRef?: string
connectionString?: string
}
export async function getDatabasePublications(
{ projectRef, connectionString }: DatabasePublicationsVariables,
signal?: AbortSignal
) {
if (!projectRef) throw new Error('projectRef is required')
let headers = new Headers()
if (connectionString) headers.set('x-connection-encrypted', connectionString)
const { data, error } = await get('/platform/pg-meta/{ref}/publications', {
params: {
header: {
'x-connection-encrypted': connectionString!,
},
path: {
ref: projectRef,
},
},
headers,
signal,
})
if (error) handleError(error)
return data
}
export type DatabasePublicationsData = Awaited<ReturnType<typeof getDatabasePublications>>
export type DatabasePublicationsError = ResponseError
export const useDatabasePublicationsQuery = <TData = DatabasePublicationsData>(
{ projectRef, connectionString }: DatabasePublicationsVariables,
{
enabled = true,
...options
}: UseQueryOptions<DatabasePublicationsData, DatabasePublicationsError, TData> = {}
) =>
useQuery<DatabasePublicationsData, DatabasePublicationsError, TData>(
databasePublicationsKeys.list(projectRef),
({ signal }) => getDatabasePublications({ projectRef, connectionString }, signal),
{
enabled: enabled && typeof projectRef !== 'undefined',
...options,
}
)