Files
supabase/apps/studio/data/privileges/table-privileges-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

56 lines
1.7 KiB
TypeScript

import { UseQueryOptions, useQuery } from '@tanstack/react-query'
import type { components } from 'data/api'
import { get, handleError } from 'data/fetchers'
import type { ResponseError } from 'types'
import { privilegeKeys } from './keys'
export type TablePrivilegesVariables = {
projectRef?: string
connectionString?: string
}
export type TablePrivilege = components['schemas']['PostgresTablePrivileges']
export async function getTablePrivileges(
{ projectRef, connectionString }: TablePrivilegesVariables,
signal?: AbortSignal
) {
if (!projectRef) throw new Error('projectRef is required')
const headers = new Headers()
if (connectionString) headers.set('x-connection-encrypted', connectionString)
const { data, error } = await get('/platform/pg-meta/{ref}/table-privileges', {
params: {
path: { ref: projectRef },
// this is needed to satisfy the typescript, but it doesn't pass the actual header
header: { 'x-connection-encrypted': connectionString! },
},
signal,
headers,
})
if (error) throw handleError(error)
return data
}
export type TablePrivilegesData = Awaited<ReturnType<typeof getTablePrivileges>>
export type TablePrivilegesError = ResponseError
export const useTablePrivilegesQuery = <TData = TablePrivilegesData>(
{ projectRef, connectionString }: TablePrivilegesVariables,
{
enabled = true,
...options
}: UseQueryOptions<TablePrivilegesData, TablePrivilegesError, TData> = {}
) =>
useQuery<TablePrivilegesData, TablePrivilegesError, TData>(
privilegeKeys.tablePrivilegesList(projectRef),
({ signal }) => getTablePrivileges({ projectRef, connectionString }, signal),
{
enabled: enabled && typeof projectRef !== 'undefined',
...options,
}
)