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 { 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 = ( { projectRef, connectionString }: ExposedFunctionsVariables, { enabled = true, ...options }: UseCustomQueryOptions = {} ) => useQuery({ queryKey: privilegeKeys.exposedFunctionsAll(projectRef), queryFn: ({ signal }) => getAllExposedFunctions({ projectRef, connectionString }, signal), enabled: enabled && typeof projectRef !== 'undefined', ...options, })