mirror of
https://github.com/supabase/supabase.git
synced 2026-10-12 04:45:08 +03:00
* Add custom types for queries, mutations and infinite queries. * Migrate all queries to use the new type. * Migrate all infinite queries to useCustomInfiniteQueryOptions. * Migrate all mutations to use useCustomMutationOptions. * Add type to all imports in `types` folder.
58 lines
1.9 KiB
TypeScript
58 lines
1.9 KiB
TypeScript
import { useQuery } from '@tanstack/react-query'
|
|
|
|
import { DEFAULT_PLATFORM_APPLICATION_NAME } from '@supabase/pg-meta/src/constants'
|
|
import type { components } from 'data/api'
|
|
import { get, handleError } from 'data/fetchers'
|
|
import type { ResponseError, UseCustomQueryOptions } from 'types'
|
|
import { privilegeKeys } from './keys'
|
|
|
|
export type ColumnPrivilegesVariables = {
|
|
projectRef?: string
|
|
connectionString?: string | null
|
|
}
|
|
|
|
export type ColumnPrivilege = components['schemas']['PostgresColumnPrivileges']
|
|
|
|
export async function getColumnPrivileges(
|
|
{ projectRef, connectionString }: ColumnPrivilegesVariables,
|
|
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}/column-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!,
|
|
'x-pg-application-name': DEFAULT_PLATFORM_APPLICATION_NAME,
|
|
},
|
|
},
|
|
signal,
|
|
headers,
|
|
})
|
|
|
|
if (error) handleError(error)
|
|
return data
|
|
}
|
|
|
|
export type ColumnPrivilegesData = Awaited<ReturnType<typeof getColumnPrivileges>>
|
|
export type ColumnPrivilegesError = ResponseError
|
|
|
|
export const useColumnPrivilegesQuery = <TData = ColumnPrivilegesData>(
|
|
{ projectRef, connectionString }: ColumnPrivilegesVariables,
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: UseCustomQueryOptions<ColumnPrivilegesData, ColumnPrivilegesError, TData> = {}
|
|
) =>
|
|
useQuery<ColumnPrivilegesData, ColumnPrivilegesError, TData>({
|
|
queryKey: privilegeKeys.columnPrivilegesList(projectRef),
|
|
queryFn: ({ signal }) => getColumnPrivileges({ projectRef, connectionString }, signal),
|
|
enabled: enabled && typeof projectRef !== 'undefined',
|
|
...options,
|
|
})
|