import { useMutation } from '@tanstack/react-query' import { components } from 'api-types' import type { TableSyncCopyConfig } from './create-destination-pipeline-mutation' import { handleError, post } from '@/data/fetchers' import type { ResponseError, UseCustomMutationOptions } from '@/types' type ValidatePipelineParams = { projectRef: string sourceId: number publicationName: string maxFillMs?: number maxTableSyncWorkers?: number maxCopyConnectionsPerTable?: number invalidatedSlotBehavior?: 'error' | 'recreate' tableSyncCopy: TableSyncCopyConfig } type ValidatePipelineResponse = components['schemas']['ValidatePipelineResponse'] async function validatePipeline( { projectRef, sourceId, publicationName, maxFillMs, maxTableSyncWorkers, maxCopyConnectionsPerTable, invalidatedSlotBehavior, tableSyncCopy, }: ValidatePipelineParams, signal?: AbortSignal ): Promise { if (!projectRef) throw new Error('projectRef is required') if (!sourceId) throw new Error('sourceId is required') const batchConfig = maxFillMs !== undefined ? { max_fill_ms: maxFillMs } : undefined const config = { publication_name: publicationName, max_table_sync_workers: maxTableSyncWorkers, max_copy_connections_per_table: maxCopyConnectionsPerTable, invalidated_slot_behavior: invalidatedSlotBehavior, table_sync_copy: tableSyncCopy, batch: batchConfig, } const { data, error } = await post('/platform/replication/{ref}/pipelines/validate', { params: { path: { ref: projectRef } }, body: { source_id: sourceId, config: config as components['schemas']['ValidateReplicationPipelineBody']['config'], }, signal, }) if (error) handleError(error) return data as ValidatePipelineResponse } type ValidatePipelineData = Awaited> export const useValidatePipelineMutation = ( options?: Omit< UseCustomMutationOptions, 'mutationFn' > ) => { return useMutation({ mutationFn: (vars) => validatePipeline(vars), ...options, }) }