Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

1.6 KB
Raw Blame
import { useQuery } from '@tanstack/react-query'

import {
  getExposedFunctions,
  type ExposedFunction,
  type ExposedFunctionsError,
  type ExposedFunctionsVariables,
} from './exposed-functions-infinite-query'
import { privilegeKeys } from './keys'
import type { UseCustomQueryOptions } from '@/types'

// The API Docs panel renders every function without virtualization, so we fetch
// the full grant-status list in a single request rather than paginating.
const ALL_EXPOSED_ENTITIES_LIMIT = 100_000

export type ExposedFunctionsAllData = ExposedFunction[]

async function getAllExposedFunctions(
  { projectRef, connectionString }: ExposedFunctionsVariables,
  signal?: AbortSignal
): Promise<ExposedFunctionsAllData> {
  const { functions } = await getExposedFunctions(
    { projectRef, connectionString, page: 0, limit: ALL_EXPOSED_ENTITIES_LIMIT },
    signal
  )
  return functions
}

/**
 * Returns the grant status (`granted` | `custom` | `revoked`) for every database
 * function, used to filter unexposed functions out of the autogenerated Data API
 * docs.
 */
export const useExposedFunctionsQuery = <TData = ExposedFunctionsAllData>(
  { projectRef, connectionString }: ExposedFunctionsVariables,
  {
    enabled = true,
    ...options
  }: UseCustomQueryOptions<ExposedFunctionsAllData, ExposedFunctionsError, TData> = {}
) =>
  useQuery<ExposedFunctionsAllData, ExposedFunctionsError, TData>({
    queryKey: privilegeKeys.exposedFunctionsAll(projectRef),
    queryFn: ({ signal }) => getAllExposedFunctions({ projectRef, connectionString }, signal),
    enabled: enabled && typeof projectRef !== 'undefined',
    ...options,
  })