import { useQuery, UseQueryOptions } from '@tanstack/react-query' import type { components } from 'data/api' import { get, handleError } from 'data/fetchers' import type { ResponseError } from 'types' import { databaseKeys } from './keys' export type PoolingConfigurationVariables = { projectRef?: string } export type PoolingConfiguration = components['schemas']['SupavisorConfigResponse'] export async function getPoolingConfiguration( { projectRef }: PoolingConfigurationVariables, signal?: AbortSignal ) { if (!projectRef) throw new Error('Project ref is required') const { data, error } = await get(`/platform/projects/{ref}/config/supavisor`, { params: { path: { ref: projectRef } }, signal, }) if (error) handleError(error) return data } export type PoolingConfigurationData = Awaited> export type PoolingConfigurationError = ResponseError export const usePoolingConfigurationQuery = ( { projectRef }: PoolingConfigurationVariables, { enabled = true, ...options }: UseQueryOptions = {} ) => useQuery( databaseKeys.poolingConfiguration(projectRef), ({ signal }) => getPoolingConfiguration({ projectRef }, signal), { enabled: enabled && typeof projectRef !== 'undefined', ...options, } )