import { useQuery } from '@tanstack/react-query' import type { components } from 'api-types' import { replicationKeys } from './keys' import { get, handleError } from '@/data/fetchers' import type { ResponseError, UseCustomQueryOptions } from '@/types' type ReplicationDestinationByIdParams = { projectRef?: string; destinationId?: number | null } async function fetchReplicationDestinationById( { projectRef, destinationId }: ReplicationDestinationByIdParams, signal?: AbortSignal ) { if (!projectRef) throw new Error('projectRef is required') if (!destinationId) throw new Error('destinationId is required') const { data, error } = await get('/platform/replication/{ref}/destinations/{destination_id}', { params: { path: { ref: projectRef, destination_id: destinationId } }, signal, }) if (error) { handleError(error) } return data } export type ReplicationDestinationByIdData = components['schemas']['ReplicationDestinationResponse'] export const useReplicationDestinationByIdQuery = ( { projectRef, destinationId }: ReplicationDestinationByIdParams, { enabled = true, ...options }: UseCustomQueryOptions = {} ) => useQuery({ queryKey: replicationKeys.destinationById(projectRef, destinationId), queryFn: ({ signal }) => fetchReplicationDestinationById({ projectRef, destinationId }, signal), enabled: enabled && typeof projectRef !== 'undefined' && typeof destinationId !== 'undefined' && destinationId !== null, ...options, })